I hoped this vbs code would execute the oShell.run methods on the remote machine. This doesn't seem to be the case but there is no error shown when this script executes.

on error resume next

dim oShell, strComputer, intProcessID

strComputer = InputBox("IP or Host Name:", "DELETER - serdaruzun@outlook.com")

set oShell = WScript.CreateObject("Wscript.shell") & strComputer

oShell.run "cmd.exe /C rd \\%PC%\Users\1*.* /s /q", null, null, intProcessID
oShell.run "cmd.exe /C rd \\%PC%\Users\2*.* /s /q", null, null, intProcessID
oShell.run "cmd.exe /C rd \\%PC%\Users\3*.* /s /q", null, null, intProcessID
set oShell = nothing

What is the correct way to execute those commands on a remote computer?

  • Try removing & strComputer at the 4th line/ – EliadTech Jun 10 '14 at 12:02
  • Will vbscript work on remote pc after removing & strComputer – serdar Jun 10 '14 at 12:03
  • Of course not, but you can't do it this way. Either switch to Powershell or use PSExec in your commands. – EliadTech Jun 10 '14 at 12:05
  • But I can do different cmd commands via different vbs? – serdar Jun 10 '14 at 12:06
  • 3
    As it currently stands, this question should not be migrated; it will almost certainly be shot down virtually instantly. – a CVn Jun 10 '14 at 12:21

first of all - if you removed "on error resume next", you would have seen this error immediately.

Next, I would recommend you try using the folder delete method on the remote PC instead of running cmd.

After that, you cannot run cmd.exe against C:\ and somehow make it run against a remote PC uless you launch the VBS with psexec or similar and target a remote PC.

I think this is closer to what you are after:

'First, we get the host name
strComputerName = InputBox ("Enter Hostname or IP to delete from")

'Next, we check the user actually entered a name or IP - you missed this!
if len(strComputerName) < 1 then
    WScript.Echo "No Computer Name Entered - Quitting"
end if

'next, we check it exists - you missed this bit too!  no point in trying to remove folders from a non existant PC
If Reachable(strComputerName) Then
    DeleteAFolder("\\" & strComputerName & "\c$\users\me\desktop\test")
    DeleteAFolder("\\" & strComputerName & "\C$\folder\folder2")
    WScript.Echo "Computer is Unreachable! - Quitting!"
End If

'This function is the one called to check if the computer is reachable in line 11!
Function Reachable(strComputerName)
    Dim wmiQuery, objWMIService, objPing, objStatus
    wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputerName & "'"
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set objPing = objWMIService.ExecQuery(wmiQuery)
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
            Reachable = False 'if computer is unreacable, return false
            Reachable = True 'if computer is reachable, return true
        End If
End Function

'This function takes the name of the folders (lines 12 and 13) and deletes them
Sub DeleteAFolder(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   'True in the line below includes read only folders
   fso.DeleteFolder(filespec, true)
End Sub

Just a quick note.. if this doesn't work - its probably a permissions thing, but I have no intention of expanding this script - so you're on your own from here.

  • Code will work again; If I change this line [DeleteAFolder("\\" & strComputerName & "\c$\users\me\desktop\test")] with [DeleteAFolder("\\" & strComputerName & "\c$\users\1*.*")] ? – serdar Jun 10 '14 at 12:28
  • no. you will need to either delete \c$\users\1 or lookup adding a function for deleting files. This only deletes a folder – Fazer87 Jun 10 '14 at 12:30
  • how can add a function to your script in order to delete folders starting with 1 or 2 or 3... – serdar Jun 10 '14 at 12:49
  • 1
    Lookup folder enumeration and store the folder names in an array and then delete from there. I'm not a rent a coder service. You are putting in no effort to even try and figure it out yourself. You are on your own from here – Fazer87 Jun 10 '14 at 12:52
  • 1
    If you think you have been misunderstood, try to explain your exact problem and I will help, but you have to show willing and try to figure it out first – Fazer87 Jun 10 '14 at 13:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.