How Can I Create a Folder on a Remote Computer?

How Can I Create a Folder on a Remote Computer?

  • Comments 5
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I create a folder on a remote computer using a script?

-- AN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AN. Life is full of mysteries. To name one, what possessed somebody to think that the world was clamoring for Clamato juice, an unholy concoction combining tomato juice and clam juice? (As long as we’re on the subject: clam juice?) To name another, why is it that neither the FileSystemObject nor WMI provides a straightforward way to create a folder on a remote computer?

To be honest, we don’t know the answer to either of these questions. However, we do know the answer to the question “Can I create a folder on a remote computer using a script?” The answer is yes, although you have to settle for using a bit of a workaround to accomplish that feat.

As we noted, neither the FileSystemObject nor WMI provides a way to create a folder on a remote computer. The FileSystemObject has a method for creating folders, but this object is pretty much restricted to working on the local computer. WMI has methods for copying folders, for moving folders, for renaming folders, and for doing just about everything else you can think of with folders … except, of course, for creating folders. So what can you do about that?

Well, what you can do is take advantage of WMI’s ability to create a process on a remote computer. Using WMI, you can connect to a remote machine (assuming you have local administrator rights, of course) and then run any piece of software that’s installed on that machine. For example, suppose you have a burning desire to run Calculator on the remote computer atl-ws-01. This code will do the trick:

strComputer = "atl-ws-01"
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create _
    ("calc.exe", Null, Null, intProcessID)

If the remote machine is running Windows 2000, you’ll see Calculator pop up onscreen. If the remote machine is running Windows XP or Windows Server 2003, Calculator will still run, but you won’t actually see anything onscreen; for security reasons, on XP and 2003 processes started remotely run in a hidden window. But if you call up the Task Manager, you’ll see that Calculator is alive and well.

So how does that help you? Well, as you probably know there’s an easy way to create a folder on the local computer; just type something like this at the command prompt:

md c:\newfolder

That command will create a new folder (md being short for make directory) named C:\Newfolder. So how do we create a new folder on a remote computer? One way is to simply run the md command on the remote computer. We can do that by:

Using WMI to connect to the Win32_Process class on the remote computer.

Using the Create method to create a process that runs the command md c:\newfolder.

Can we really do that? You bet; in fact, here’s a script that creates a new folder on a remote computer named atl-ws-01:

strComputer = "atl-ws-01"
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create _
    ("cmd.exe /c md c:\newfolder", Null, Null, intProcessID)

Note the four parameters passed to the Create method. These are:

Parameter

Description

cmd.exe /c md c:\newfolder

The command-line command for creating a new folder. Note that we have to include the cmd.exe in the command. That’s because md part of cmd.exe; it’s not a standalone executable file. Basically what we’re doing here is telling WMI to start cmd.exe and then, after the shell; is running, execute the md command.

Null

Used to set the working folder for the process. Because that’s irrelevant to our needs, we just pass a Null parameter.

Null

Represents startup configuration for the process. Again, we don’t care about startup configuration information for this script, so we send another Null parameter.

intProcessID

An “out” parameter that stores the Process ID for the process we create. Because the md command takes only a second or so to complete we don’t have much use for the Process ID. However, suppose we were performing a task that would take several minutes to complete. By knowing the Process ID, we could do things like monitor the progress of the task, and thus be notified when the process actually completed.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>What if I need to stick a variable in the command:</p> <p>objWMIService.Create(&quot;cmd.exe /c schtasks.exe /create &quot; &amp; strServer &amp; &quot;/u username /p password/tn ftp_drtrial /sc daily /st 10:30:00 /tr C:\ftp_drtrial.bat&quot;)</p> <p>It doesn&#39;t like that strServer and I have checked it&#39;s presence with a wscript.echo strServer prior to this.</p>

  • <p>I attempted to use this code but ran into a problem. If the folder you want to create is to reside inside a folder with a name that has a space in it (like program files or bad food), it won&#39;t work. For example, if you want to create a folder named test within the programs file folder (c:\program file\test) the space between program and file will prohib you. Surrounding the path with single or double quotes also does not work. Any feedback on how to do it would be appreciated.</p>

  • <p>Same issue as Surge. &nbsp;Won&#39;t work with space, gives VBScript compilation error : Expected &quot;)&quot;</p>

  • <p>Surhe, Jivin, three quotes do work:</p> <p>(&quot;cmd.exe /c md &quot;&quot;d:\new folder&quot;&quot;&quot;, Null, Null, intProcessID)</p>

  • <p>What if I dont have access to the remote machine but I have the credential of the remote machine?</p>