Hello I'm new here I need a script to check, if a folder exist and then run a file from the folder. If not, it should extract a ZIP file into a specific location. I can only tell you to see this: Extract files from ZIP file with VBScript. Script to check if folder exists, if not, run a exe file.
I have a folder with many files. It looks like this file1.txt, newFile1.txt, file2.txt, newFile2.txt, file3.txt, newFile3.txt, file4.txt, newFile4.txt, ....
I have a code that generates the newFilei.txt . I want to write a vbscript that checks if a newFile exists in this folder or not. I tried this
but this didnt work. any ideas ?
KJohnKJohn
1 Answer
Edit: The COM object made this very simple.
Or, if you wanted your code to work
And, pulling it all together. RichRich
Not the answer you're looking for? Browse other questions tagged vbscript or ask your own question.
Hey, Scripting Guy! How can I check to see if a particular file exists and, if it does, exit the script?
— TO
— TO
![Vbscript to check if folder exists and delete Vbscript to check if folder exists and delete](/uploads/1/2/5/5/125570353/134021240.png)
Hey, TO. There are at least two ways to check for the existence of a file (that’s the real key here; exiting the script is easy). One of these approaches uses the FileSystemObject and the other uses WMI, and we’re going to show you both methods today. Why? Well, that way if anyone comes by and asks us to help them with something we can say, “Wow, we’d like to, but we have to finish this column, and because we’re showing people two scripts, well, that could take awhile.”
OK, so that’s only partly true: there are actually a couple valid reasons why you might choose one approach over the other. People generally find the FileSystemObject a bit easier to script; the only problem is that the FileSystemObject is designed to work solely on the local computer. WMI scripts tend to be a tad bit more complicated (although, as you’ll see, they’re still pretty easy), but WMI scripts can work with remote computers just as easily as they can the local computer. What does that mean? It means that, if you’re working on the local computer, you might want to use the FileSystemObject. If you’re working against a remote computer you need to use WMI.
In the grand tradition of the Scripting Guys let’s do the easy one first. Let’s take a look at a FileSystemObject script that checks for the existence of the file C:ScriptsTest.txt. If the file is found the script exits; if the file is not found then the script displays a message to that effect.
Here’s the script:
We told you it was easy. We begin by creating an instance of the Scripting.FileSystemObject and then call the FileExists method, passing the method the complete path to the file in question. If FileExists returns True that means the file was found; at that point we then use Wscript.Quit to exit the script. If FileExists is not found we then use this line of code to echo back our failure to find the file:
That’s pretty much all we have to do.
Incidentally, it’s a good idea to use the FileExists method any time you’re working with the FileSystemObject. The FileSystemObject is very powerful, but also very sensitive: it just falls to pieces when things go wrong. Worst of all, even using On Error Resume Next won’t help; if the FileSystemObject tries to work with a non-existent file or folder your scripts are likely to crash even if you use On Error Resume Next. The way to work around that problem? Just use FileExists (or its companion method, FolderExists) before trying to bind to a file or folder.
OK, well, that’s all for today – oh, that’s right, we said we’d show you two scripts, didn’t we? (What were we thinking?) As we noted, you can also check for the existence of a file using WMI. The nice thing about using WMI is that the same script can run against remote computers as well as local computers; all you have to do is change the name of the computer and have at it.
Here’s a WMI script that checks for the existence of C:ScriptsTest.txt and, if the file is found, exits:
Like we said, a tad bit more complicated, but nothing that you shouldn’t be able to handle. We begin by setting the value of the variable strComputer to a dot; in the wonderful world of WMI the dot represents the local computer. But didn’t we say that this same script could be used against remote computers? Yes, we did, and that’s a simple enough task: just set the value of strComputer to the name of that remote computer. For example, this line of code will cause the script to run against the computer atl-fs-01:
![Folder Folder](/uploads/1/2/5/5/125570353/820559070.png)
Next we connect to the WMI service, then use the ExecQuery method to select all instances of the CIM_Datafile class where the Name property is equal to C:ScriptsTest.txt. And don’t panic, you haven’t suddenly been afflicted with double vision. In WMI the is a reserved character; that means any time a appears in a query the character must be “escaped,” which simply means prefacing the with a second . Thus a file path like C:ScriptsTest.txt gets written out as C:ScriptsTest.txt. Hey, as long as it works, right?
Note. By the way, this could be worse. For example, suppose we had a path like atl-fs-01publicscripts. That would get written out like so: atl-fs-01publicscripts, with every single doubled up. |
Our query returns a collection consisting of all the files on the computer named C:ScriptsTest.txt. (Needless to say, there can either be one such file or no such files.) To determine whether or not the file was found we check the Count property of the returned collection. If Count is greater than 0 that means the file was found; in turn we call Wscript.Quit and exit the script. If the Count is not greater than 0 than no such file could be found and we echo a message to that effect.
So there you have it: not one but two ways to determine whether a file exists and, if it does, exit the script. Wow, no doubt someday people will be saying, “Where were you on the day the Scripting Guys showed us two ways to determine whether a file exists?” Until then, just use whichever script makes the most sense for you.