SteveJbs
SteveJbs

Reputation: 23

vbscript to delete files & folders of remote computer

First thing I am not an expert in writing VBScripts.

I have a requirement of deleting files & folders of remote systems with just 1 click. I was trying to build below VBScript but somehow it’s not working. I request any of your help to correct the same or with a new script that help me to fulfill the requirement. Any help in this regard is greatly appreciated, Thanks in Advance.

With the below: C:\Test - is the directory from where I would like to delete the files & subfolders C:\computerList.txt – is the text file contains all remote systems IP Address.

Const strPath = "C:\Test"

Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call Search (strPath)
WScript.Echo"Done."

Sub Search(str)
    Do While Not computerList.AtEndOfStream
    strComputer = computerList.ReadLine
        Dim objFolder, objSubFolder, objFile
        Set objFolder = objFSO.GetFolder("\\" & strComputer & "\" & str)
        For Each objFile In objFolder.Files
            If objFile.DateLastModified < (Now() - 0) Then
                objFile.Delete(True)
            End If
        Next
        For Each objSubFolder In objFolder.SubFolders
            Search(objSubFolder.Path)
            ' Files have been deleted, now see if
            ' the folder is empty.
            If (objSubFolder.Files.Count = 0) Then
                objSubFolder.Delete True
            End If
        Next
    loop

End Sub

Regards, Balaram Reddy

Upvotes: 0

Views: 7004

Answers (2)

ServerGuy
ServerGuy

Reputation: 555

Your first problem is that you have the line order incorrect:

Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Should be

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set computerList = objfso.OpenTextFile ("C:\computerList.txt", 1)

You are using objfso before declaring it

Upvotes: 2

lit-alien
lit-alien

Reputation: 36

When using a UNC path, you will need to use the folder's remote share name. If you have admin privileges on the remote pc use:

Const strPath = "c$\Test"

Upvotes: 1

Related Questions