VBScript down the way with the Dir command _vbs

Source: Internet
Author: User

Ask:

Hey, scripting guy!. I want to do something that looks very simple: Call the dir command in a script. But it doesn't seem to work. How to realize my idea?

--CR, Mexico City, Mexican

For:

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

%comspec%

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.