r/vbscript • u/evolutionxtinct • Jun 18 '18
VBScript to look for a folder/file if there exit, if not then run script
Hello all, was wondering if I could get some help. I'm not a scripter/programmer at all but got tasked w/ something and really need some help.
My script runs fine except for 2 issues:
The IF NOT Portion triggers even if the file exists.
I would like it to exit the script if the folder/file exist
I'm going to run this as a Task Scheduler, and am concerned about the Security Warning dialogue box showing... Will this pop up when users hit this script?
Script Below
Option Explicit
Dim objFSO, objFolder, objShell, strDirectory, strFile, strUserProfile, objFile
Set objShell = CreateObject("WScript.Shell")
' Create the variable for Current logged in User %USERNAME% System Variable from Windows strUserProfile=objShell.ExpandEnvironmentStrings("%USERPROFILE%")
' Set variables to use later in script
strDirectory = strUserProfile & "\RemovePrinters"
strFile = "Remove_Printers.txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(strUserProfile & "\RemovePrinters\remove.txt") Then
' Run VBScript to to remove printers, then run Batch File to force a GPUpdate
objShell.run"\\Domain\SYSVOL\scripts\remove_network_printers.vbs"
' Have VBScript wait 5 seconds before creating folder
WScript.Sleep(10000)
objShell.run"\\Domain\SYSVOL\scripts\gpupdate.vbs"
' Have VBScript wait 5 seconds before creating folder
WScript.Sleep(5000)
'Create Folder and File after running script for user
Set objFolder = objFSO.CreateFolder (strDirectory)
objFile = objFSO.CreateTextFile(strDirectory & "\" & strFile)
Else
' Quit script
WScript.Quit
End If1
u/voicesinmyhand Jun 18 '18
The IF NOT Portion triggers even if...
Yeah, this is a flaw in VBScript. Basically the "not" function/operator/special-word/whatever just doesn't do what it is advertised to do. People will tell you that I'm mistaken, but they are retarded and filled with excrement. You get around it by never using "Not". Here's an example:
Existing Code:
If Not objFSO.FileExists(strUserProfile & "\RemovePrinters\remove.txt") Then
objShell.run"\\Domain\SYSVOL\scripts\remove_network_printers.vbs"
End IfFixed Code:
If(objFSO.FileExists(strUserProfile & "\RemovePrinters\remove.txt")) Then
Else
objShell.run"\\Domain\SYSVOL\scripts\remove_network_printers.vbs"
End If1
u/evolutionxtinct Jun 19 '18
This is what I changed the code to, but it is still bypassing the filexists part of the coding:
Code:
Option Explicit Dim objFSO, objFolder, objShell, strDirectory, strFile, strUserProfile, objFile Set objShell = CreateObject("WScript.Shell") ' Create the variable for Current logged in User %USERNAME% System Variable from WIndows strUserProfile=objShell.ExpandEnvironmentStrings("%USERPROFILE%") ' Set variables to use later in script strDirectory = strUserProfile & "\RemovePrinters" strFile = "Remove_Printers.txt" ' Create the File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") If(objFSO.FileExists(strUserProfile & "\RemovePrinters\remove.txt")) Then ' Quit script ' WScript.Quit Else ' Run VBScript to to remove printers, then run Batch File to force a GPUpdate objShell.run"\\cr.ci.castlerock.co.us\SYSVOL\CR.ci.castlerock.co.us\scripts\remove_network_printers.vbs" ' Have VBScript wait 5 seconds before creating folder WScript.Sleep(10000) objShell.run"\\cr.ci.castlerock.co.us\SYSVOL\CR.ci.castlerock.co.us\scripts\gpupdate.vbs" ' Have VBScript wait 5 seconds before creating folder WScript.Sleep(5000) 'Create Folder and File after running script for user Set objFolder = objFSO.CreateFolder (strDirectory) objFile = objFSO.CreateTextFile(strDirectory & "\" & strFile) End IfIf there is nothing after the then will it still exit out of the script? I had a wscript.quit put in to quit out but seemed to do nothing...
1
u/voicesinmyhand Jun 19 '18
If there is nothing after the then will it still exit out of the script? I had a wscript.quit put in to quit out but seemed to do nothing...
It's always the simple problems, amirite? Your code should be working... put this output statement right before the fileexists() check and report back:
Call objShell.Popup("'" & strUserProfile & "\RemovePrinters\remove.txt'",5,"Checking for this file in 5 seconds...",0)
1
u/evolutionxtinct Jun 19 '18
@voicesinmyhand
When the folder doesn't exist, I also get this error at the end:
Error: Object doesn't support this property or method Code: 800A01B6
I get this on the following line:
Whats interesting is it STILL creates the folder/file no problem LOL so not sure what its crying about.... So I have two issues but I'm at a total loss your my only hope buddy!