Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Script to Search and Delete Files from Remote Machines

  1. #16
    Shay Levi Guest

    Re: Script to Search and Delete Files from Remote Machines


    I don't mind sitting and writing a LOOONG VBScript version of the process.
    I prefer the short version which basically perform what you want.
    If you insist doing it in vbscript, update this thread :)

    Just a reminder, the below example reads a text file (computers.txt, line
    by line) and pass it to WMIC.
    WMIC, search for every file on C drive that have a name like *error.doc and
    deletes it, in addition a log file is created
    for each computer, the log contains the deleted files (path) in c:\logs (make
    sure it exists).

    for /f %%a in (computers.txt) do (
    WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    )




    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > shay,
    >
    > finally i made one script and able to delete file using LIKE. But i
    > need a few modification in this.i will store all computer names in a
    > text file, so the script has to pick machines one by one. And i need
    > seperate logs for all machines with details from where it has deleted
    > all the files. can you please help me. here is my script.
    >
    > Dim strComputer, strExtension
    >
    > strComputer = "."
    >
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    > "\root\cimv2")
    >
    > Set colFiles = objWMIService.ExecQuery _
    > ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' ) AND FileName
    > LIKE '%error' AND Extension = 'doc" & strExtension & "'")
    > For Each objFile in colFiles
    > objFile.Delete
    > Next
    > thanks in advance
    >
    > binu
    >
    > http://forums.techarena.in
    >




  2. #17
    Tom Lavedas Guest

    Re: Script to Search and Delete Files from Remote Machines

    On May 20, 9:53 am, Shay Levi <n...@addre.ss> wrote:
    > I don't mind sitting and writing a LOOONG VBScript version of the process.
    > I prefer the short version which basically perform what you want.
    > If you insist doing it in vbscript, update this thread :)
    >
    > Just a reminder, the below example reads a text file (computers.txt, line
    > by line) and pass it to WMIC.
    > WMIC, search for every file on C drive that have a name like *error.doc and
    > deletes it, in addition a log file is created
    > for each computer, the log contains the deleted files (path) in c:\logs (make
    > sure it exists).
    >
    > for /f %%a in (computers.txt) do (
    > WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    > '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    > )
    >
    > ---
    > Shay Levi
    > $cript Fanatichttp://scriptolog.blogspot.com
    >
    > > shay,

    >
    > > finally i made one script and able to delete file using LIKE. But i
    > > need a few modification in this.i will store all computer names in a
    > > text file, so the script has to pick machines one by one. And i need
    > > seperate logs for all machines with details from where it has deleted
    > > all the files. can you please help me. here is my script.

    >
    > > Dim strComputer, strExtension

    >
    > > strComputer = "."

    >
    > > Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    > > "\root\cimv2")

    >
    > > Set colFiles = objWMIService.ExecQuery _
    > > ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' ) AND FileName
    > > LIKE '%error' AND Extension = 'doc" & strExtension & "'")
    > > For Each objFile in colFiles
    > > objFile.Delete
    > > Next
    > > thanks in advance

    >
    > > binu

    >
    > >http://forums.techarena.in


    I don't know WMIC syntax, but I know that the percent sign has special
    meaning in command line statements. Therefore, they must be 'escaped'
    by doubling them when they are part of the underlying statement being
    executed and not part of the command line statement. That is ...

    for /f %%a in (computers.txt) do (
    WMIC /node:%%a path cim_datafile WHERE "Drive = 'c:' AND FileName
    LIKE ^
    '%%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    )

    The line wrap in the long WMIC parameter string will also need to be
    escaped with the carat character (^), unless it is confined to a
    single line. Also, there seems to be an opening double quote
    missing in the WHERE clause.

    Having said that, I think the better approach would be to modify the
    VBS script that the OP posted (that he says works). The FSO part can
    be made fairly simple ...

    Dim strComputer, strExtension, oLog, colFiles, objWMIService, _
    sCompNameFile, sLogPath

    sCompNameFile = "D:\Someplace\CompNames.txt
    sLogPath = "D:\Someplace\logs\"
    strExtension = "doc"

    CreateObject("Scripting.FileSystemObject")

    for each strComputer in .OpenTextFile(sCompNameFile, 1).ReadAll
    set oLog = .OpenTextFile(sLogPath & strComputer & ".log", 2, true)

    Set objWMIService = GetObject("winmgmts:\\" & strComputer _
    & "\root\cimv2")

    Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:') " _
    & "AND FileName LIKE '%error' AND Extension = '" _
    & strExtension & "'")

    For Each objFile in colFiles
    objFile.Delete
    oLog.writeline objFile.Name ' or Path for full pathspec
    Next
    oLog.Close
    set oLog = Nothing
    Set colFiles = Nothing
    Set objWMIService = Nothing
    Next

    end with ' FSO

    The script is missing all forms of error handling, which with remote
    computers that might be on-line can cause problems. Normally a Ping
    function is used to check for the availability of the computer, in
    such cases. A google search for IsConnectible should turn up a lot of
    info on such applications.

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

  3. #18
    Tom Lavedas Guest

    Re: Script to Search and Delete Files from Remote Machines

    On May 20, 10:54 am, Tom Lavedas <tglba...@cox.net> wrote:
    > On May 20, 9:53 am, Shay Levi <n...@addre.ss> wrote:
    >
    >
    >
    > > I don't mind sitting and writing a LOOONG VBScript version of the process.
    > > I prefer the short version which basically perform what you want.
    > > If you insist doing it in vbscript, update this thread :)

    >
    > > Just a reminder, the below example reads a text file (computers.txt, line
    > > by line) and pass it to WMIC.
    > > WMIC, search for every file on C drive that have a name like *error.doc and
    > > deletes it, in addition a log file is created
    > > for each computer, the log contains the deleted files (path) in c:\logs (make
    > > sure it exists).

    >
    > > for /f %%a in (computers.txt) do (
    > > WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    > > '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    > > )

    >
    > > ---
    > > Shay Levi
    > > $cript Fanatichttp://scriptolog.blogspot.com

    >
    > > > shay,

    >
    > > > finally i made one script and able to delete file using LIKE. But i
    > > > need a few modification in this.i will store all computer names in a
    > > > text file, so the script has to pick machines one by one. And i need
    > > > seperate logs for all machines with details from where it has deleted
    > > > all the files. can you please help me. here is my script.

    >
    > > > Dim strComputer, strExtension

    >
    > > > strComputer = "."

    >
    > > > Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    > > > "\root\cimv2")

    >
    > > > Set colFiles = objWMIService.ExecQuery _
    > > > ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' ) AND FileName
    > > > LIKE '%error' AND Extension = 'doc" & strExtension & "'")
    > > > For Each objFile in colFiles
    > > > objFile.Delete
    > > > Next
    > > > thanks in advance

    >
    > > > binu

    >
    > > >http://forums.techarena.in

    >
    > I don't know WMIC syntax, but I know that the percent sign has special
    > meaning in command line statements. Therefore, they must be 'escaped'
    > by doubling them when they are part of the underlying statement being
    > executed and not part of the command line statement. That is ...
    >
    > for /f %%a in (computers.txt) do (
    > WMIC /node:%%a path cim_datafile WHERE "Drive = 'c:' AND FileName
    > LIKE ^
    > '%%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    > )
    >
    > The line wrap in the long WMIC parameter string will also need to be
    > escaped with the carat character (^), unless it is confined to a
    > single line. Also, there seems to be an opening double quote
    > missing in the WHERE clause.
    >
    > Having said that, I think the better approach would be to modify the
    > VBS script that the OP posted (that he says works). The FSO part can
    > be made fairly simple ...
    >
    > Dim strComputer, strExtension, oLog, colFiles, objWMIService, _
    > sCompNameFile, sLogPath
    >
    > sCompNameFile = "D:\Someplace\CompNames.txt
    > sLogPath = "D:\Someplace\logs\"
    > strExtension = "doc"
    >
    > CreateObject("Scripting.FileSystemObject")
    >
    > for each strComputer in .OpenTextFile(sCompNameFile, 1).ReadAll
    > set oLog = .OpenTextFile(sLogPath & strComputer & ".log", 2, true)
    >
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer _
    > & "\root\cimv2")
    >
    > Set colFiles = objWMIService.ExecQuery _
    > ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:') " _
    > & "AND FileName LIKE '%error' AND Extension = '" _
    > & strExtension & "'")
    >
    > For Each objFile in colFiles
    > objFile.Delete
    > oLog.writeline objFile.Name ' or Path for full pathspec
    > Next
    > oLog.Close
    > set oLog = Nothing
    > Set colFiles = Nothing
    > Set objWMIService = Nothing
    > Next
    >
    > end with ' FSO
    >
    > The script is missing all forms of error handling, which with remote
    > computers that might be on-line can cause problems. Normally a Ping
    > function is used to check for the availability of the computer, in
    > such cases. A google search for IsConnectible should turn up a lot of
    > info on such applications.
    >
    > Tom Lavedas
    > ===========http://members.cox.net/tglbatch/wsh/


    There is at least one error in the code ...

    for each strComputer in .OpenTextFile(sCompNameFile, 1).ReadAll

    I forgot the Split() to parse the file ...

    for each strComputer in Split(.OpenTextFile(sCompNameFile,
    1).ReadAll, vbCRLF)

    all on one line.

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

  4. #19
    binuthomas Guest

    Re:Thanks Shay

    i will be thankful to you if you can write a vbscript for the same ... :-)




    [QUOTE=Shay Levi;3747804]I don't mind sitting and writing a LOOONG VBScript version of the process.
    I prefer the short version which basically perform what you want.
    If you insist doing it in vbscript, update this thread :)

    Just a reminder, the below example reads a text file (computers.txt, line
    by line) and pass it to WMIC.
    WMIC, search for every file on C drive that have a name like *error.doc and
    deletes it, in addition a log file is created
    for each computer, the log contains the deleted files (path) in c:\logs (make
    sure it exists).

    for /f %%a in (computers.txt) do (
    WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    )




    ---
    Shay Levi

  5. #20
    Shay Levi Guest

    Re: Script to Search and Delete Files from Remote Machines


    Hi

    I said it in a previous thread and forgot to mention it again, the command
    should be saved in a batch file.



    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > On May 20, 9:53 am, Shay Levi <n...@addre.ss> wrote:
    >
    >> I don't mind sitting and writing a LOOONG VBScript version of the
    >> process.
    >> I prefer the short version which basically perform what you want.
    >> If you insist doing it in vbscript, update this thread :)
    >> Just a reminder, the below example reads a text file (computers.txt,
    >> line
    >> by line) and pass it to WMIC.
    >> WMIC, search for every file on C drive that have a name like
    >> *error.doc and
    >> deletes it, in addition a log file is created
    >> for each computer, the log contains the deleted files (path) in
    >> c:\logs (make
    >> sure it exists).
    >> for /f %%a in (computers.txt) do (
    >> WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    >> '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    >> )
    >> ---
    >> Shay Levi
    >> $cript Fanatichttp://scriptolog.blogspot.com
    >>> shay,
    >>>
    >>> finally i made one script and able to delete file using LIKE. But i
    >>> need a few modification in this.i will store all computer names in a
    >>> text file, so the script has to pick machines one by one. And i need
    >>> seperate logs for all machines with details from where it has
    >>> deleted all the files. can you please help me. here is my script.
    >>>
    >>> Dim strComputer, strExtension
    >>>
    >>> strComputer = "."
    >>>
    >>> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    >>> "\root\cimv2")
    >>>
    >>> Set colFiles = objWMIService.ExecQuery _
    >>> ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' ) AND FileName
    >>> LIKE '%error' AND Extension = 'doc" & strExtension & "'")
    >>> For Each objFile in colFiles
    >>> objFile.Delete
    >>> Next
    >>> thanks in advance
    >>> binu
    >>>
    >>> http://forums.techarena.in
    >>>

    > I don't know WMIC syntax, but I know that the percent sign has special
    > meaning in command line statements. Therefore, they must be 'escaped'
    > by doubling them when they are part of the underlying statement being
    > executed and not part of the command line statement. That is ...
    >
    > for /f %%a in (computers.txt) do (
    > WMIC /node:%%a path cim_datafile WHERE "Drive = 'c:' AND FileName
    > LIKE ^
    > '%%error' AND Extension ='doc'" delete > c:\logs\%%a.log )
    >
    > The line wrap in the long WMIC parameter string will also need to be
    > escaped with the carat character (^), unless it is confined to a
    > single line. Also, there seems to be an opening double quote
    > missing in the WHERE clause.
    >
    > Having said that, I think the better approach would be to modify the
    > VBS script that the OP posted (that he says works). The FSO part can
    > be made fairly simple ...
    >
    > Dim strComputer, strExtension, oLog, colFiles, objWMIService, _
    > sCompNameFile, sLogPath
    > sCompNameFile = "D:\Someplace\CompNames.txt
    > sLogPath = "D:\Someplace\logs\"
    > strExtension = "doc"
    > CreateObject("Scripting.FileSystemObject")
    >
    > for each strComputer in .OpenTextFile(sCompNameFile, 1).ReadAll
    > set oLog = .OpenTextFile(sLogPath & strComputer & ".log", 2, true)
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer _
    > & "\root\cimv2")
    > Set colFiles = objWMIService.ExecQuery _
    > ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:') " _
    > & "AND FileName LIKE '%error' AND Extension = '" _
    > & strExtension & "'")
    > For Each objFile in colFiles
    > objFile.Delete
    > oLog.writeline objFile.Name ' or Path for full pathspec
    > Next
    > oLog.Close
    > set oLog = Nothing
    > Set colFiles = Nothing
    > Set objWMIService = Nothing
    > Next
    > end with ' FSO
    >
    > The script is missing all forms of error handling, which with remote
    > computers that might be on-line can cause problems. Normally a Ping
    > function is used to check for the availability of the computer, in
    > such cases. A google search for IsConnectible should turn up a lot of
    > info on such applications.
    >
    > Tom Lavedas
    > ===========
    > http://members.cox.net/tglbatch/wsh/




  6. #21
    Shay Levi Guest

    Re: Script to Search and Delete Files from Remote Machines


    Hi

    Tom already gave it to you. Anyway, here's my version:



    ForReading = 1 'Opens a file for reading only
    ForWriting = 2 'Opens a file for write
    ComputersFile = "d:\scripts\temp\computers.txt"
    LogFilesPath = "d:\log\"
    strExtension = "doc"

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oComputersFile = oFSO.OpenTextFile(ComputersFile,ForReading,False)

    Do Until oComputersFile.AtEndOfStream
    strComputer = oComputersFile.ReadLine
    'Wscript.Echo strComputer

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM CIM_Datafile WHERE Drive='c:' AND FileName LIKE '%error'
    AND Extension = '" & strExtension & "'")

    Set oLogFile = oFSO.OpenTextFile(LogFilePath & strComputer & ".log", ForWriting,
    True)

    For Each objFile in colFiles
    oLogFile.WriteLine objFile.Name
    objFile.Delete
    Next

    oLogFile.Close


    Loop

    oComputersFile.Close
    Set oComputersFile = Nothing
    Set oLogFile = Nothing
    Set objWMIService = Nothing


    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > i will be thankful to you if you can write a vbscript for the same ...
    > :-)
    >
    > Shay Levi;3747804 Wrote:
    >
    >> I don't mind sitting and writing a LOOONG VBScript version of the
    >> process.
    >> I prefer the short version which basically perform what you want.
    >> If you insist doing it in vbscript, update this thread :)
    >> Just a reminder, the below example reads a text file (computers.txt,
    >> line
    >> by line) and pass it to WMIC.
    >> WMIC, search for every file on C drive that have a name like
    >> *error.doc
    >> and
    >> deletes it, in addition a log file is created
    >> for each computer, the log contains the deleted files (path) in
    >> c:\logs
    >> (make
    >> sure it exists).
    >> for /f %%a in (computers.txt) do (
    >> WMIC /node:%%a path cim_datafile WHERE Drive = 'c:' AND FileName LIKE
    >> '%error' AND Extension ='doc'" delete > c:\logs\%%a.log
    >> )
    >> ---
    >> Shay Levi

    > http://forums.techarena.in
    >




  7. #22
    Tom Lavedas Guest

    Re: Script to Search and Delete Files from Remote Machines

    On May 20, 12:47 pm, Tom Lavedas <tglba...@cox.net> wrote:
    > On May 20, 10:54 am, Tom Lavedas <tglba...@cox.net> wrote:

    {snip}

    Found another error, so I'll post a corrected (but still untested)
    version ...

    Dim strComputer, strExtension, oLog, objFile, colFiles, _
    objWMIService, sCompNameFile, sLogPath

    sCompNameFile = "D:\Someplace\CompNames.txt
    sLogPath = "D:\Someplace\logs\"
    strExtension = "doc"

    With CreateObject("Scripting.FileSystemObject")

    for each strComputer in _
    Split(.OpenTextFile(sCompNameFile, 1).ReadAll)
    set oLog = .OpenTextFile(sLogPath & strComputer & ".log", 2, true)

    Set objWMIService = GetObject("winmgmts:\\" & strComputer _
    & "\root\cimv2")

    Set colFiles = objWMIService.ExecQuery _
    ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:') " _
    & "AND FileName LIKE '%error' AND Extension = '" _
    & strExtension & "'")

    For Each objFile in colFiles
    objFile.Delete
    oLog.writeline objFile.Name ' or Path for full pathspec
    Next
    oLog.Close
    set oLog = Nothing
    Set colFiles = Nothing
    Set objWMIService = Nothing
    Next

    end with ' FSO

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

  8. #23
    binuthomas Guest

    Thanks to Tom & Shay

    Hi ,

    Thanks for your help. I have made few changes in the script and it is working fine for me.

    Thanks a bunch...!

    Binu

  9. #24
    Rich_Patterson Guest

    Re: Script to Search and Delete Files from Remote Machines

    Hey Peg man.

    I did the bat thing and it does run much faster. The question that I have
    is it possible to have the bat reference a text file with a list of pc's that
    I want this file removed from? Also, it would be helpful if you use a real
    email address.

    "Pegasus (MVP)" wrote:

    >
    > "Shay Levi" <no@addre.ss> wrote in message
    > news:89228ed22c12e8ca87d90c8f4b4c@news.microsoft.com...
    > >
    > >
    > > You don't need PSTools, you can do it from your own machine using WMIC:

    >
    > No, the OP does not need psexec.exe. It's just that the job runs
    > much faster with psexec than with WMI. Here are some actual
    > measurements:
    >
    > Partition searched: C:
    > No. of files on C: 5500
    > Time needed to delete files: 120 seconds (using a VB Script)
    > Time needed to delete files: 50 seconds (using psexec.exe)
    >
    > The difference is actually less than I thought but it is still
    > substantial. If your salary was more than doubled you
    > would think the rise was substantial too . . .
    >
    > By the way, you're consistently posting in the future. Either your
    > PC time is incorrect or you have a problem with your time zone/
    > daylight saving time settings.
    >
    >
    >


  10. #25
    Pegasus \(MVP\) Guest

    Re: Script to Search and Delete Files from Remote Machines

    I think I answered this question several months ago. Whenever it
    was, it has longe since disappeared from the horizon of my newsreader,
    so you really need to post the batch file you're using.

    Sorry, I won't publish my real EMail address. There are two reasons:
    a) This is a public forum. If we correspond privately then other readers
    can't follow our discussion and there will be no peer review.
    b) I have no desire to have my EMail address harvested by spammers.


    "Rich_Patterson" <RichPatterson@discussions.microsoft.com> wrote in message
    news:30B938FC-A9DC-4AD2-A48F-F399DF46B76A@microsoft.com...
    > Hey Peg man.
    >
    > I did the bat thing and it does run much faster. The question that I have
    > is it possible to have the bat reference a text file with a list of pc's
    > that
    > I want this file removed from? Also, it would be helpful if you use a
    > real
    > email address.
    >
    > "Pegasus (MVP)" wrote:
    >
    >>
    >> "Shay Levi" <no@addre.ss> wrote in message
    >> news:89228ed22c12e8ca87d90c8f4b4c@news.microsoft.com...
    >> >
    >> >
    >> > You don't need PSTools, you can do it from your own machine using WMIC:

    >>
    >> No, the OP does not need psexec.exe. It's just that the job runs
    >> much faster with psexec than with WMI. Here are some actual
    >> measurements:
    >>
    >> Partition searched: C:
    >> No. of files on C: 5500
    >> Time needed to delete files: 120 seconds (using a VB Script)
    >> Time needed to delete files: 50 seconds (using psexec.exe)
    >>
    >> The difference is actually less than I thought but it is still
    >> substantial. If your salary was more than doubled you
    >> would think the rise was substantial too . . .
    >>
    >> By the way, you're consistently posting in the future. Either your
    >> PC time is incorrect or you have a problem with your time zone/
    >> daylight saving time settings.
    >>
    >>
    >>




Page 2 of 2 FirstFirst 12

Similar Threads

  1. Script to search for and delete all files with extension xxx
    By Mike62 in forum Windows Server Help
    Replies: 3
    Last Post: 19-03-2012, 02:48 PM
  2. Script needed to Delete Files in a Directory Based on Date
    By adaher008 via WinServerKB.com in forum Windows Server Help
    Replies: 11
    Last Post: 02-01-2012, 01:44 AM
  3. Looking for a script to delete files older than 14 days
    By Michael Kapangama in forum Windows Server Help
    Replies: 3
    Last Post: 27-09-2008, 03:56 PM
  4. Delete files with logoff/on script
    By Pine Le in forum Windows Security
    Replies: 1
    Last Post: 03-06-2008, 11:19 PM
  5. Need a script to delete temporary ASP.NET files
    By Mak66 in forum Windows Server Help
    Replies: 2
    Last Post: 13-02-2008, 07:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,699,512.49637 seconds with 16 queries