Hello, CR. From your email, you want to do a simple job that looks something like the following:
Set Objshell = CreateObject ("Wscript.Shell") Objshell.run ("dir"), 1, TRUE
However, instead of getting a list of all the files in the current folder, you get a message to the system cannot find the file specified(the specified file cannot be found). Why is that?
The reason for this is this: there is actually no file called "Dir" on your computer. You can search for dir.exe or dir.com; you can't find them. In fact, dir is an internal command of the command line shell (cmd.exe or Command.exe, depending on the version of Windows that is running). In other words, the dir command can only be used in a command-line window. To prove this, open the Command Line window, enter dir, and then press ENTER. You should see a list of all the files and folders in the current directory. Now open the Run dialog box, enter dir , and then press ENTER. You will see an error message that looks like this:
But that doesn't mean you're unlucky. There is actually a way to invoke the Dir command in a script; but you have to be smart enough. Since dir is an internal command, you can only invoke command-line surgery and then pass dir as a command-line argument to it. Let's look at a script that exploits this technique, and then explain how it works:
Set Objshell = CreateObject ("Wscript.Shell") Objshell.run ("%comspec%/k dir"), 1, TRUE
The first line of the script simply creates an instance of the WSH Shell object and then uses the Run method to invoke the dir command on the second line. Note, however, that we do not specify dir directly, but instead specify the %comspec%/k dir. The command string can be decomposed into the following:
/k
dir