Script to Search and Delete Files from Remote Machines
I need to delete some files from remote machines (eg: all files ending with error.doc). I have got a script for the same. But the problem is it will delete only the files I specify, like if I mention error.doc it will delete only that particular file, it will not delete newerror.doc. Can you please modify the following script for the same. Also I need this script has to pick the machine names one by one from a text file and perform the task
****************START OF SCRIPT**********************
Option Explicit
Dim strComputer, strFileName, strExtension, boolFileFound, strFileList, objWMIService, colFileList, objFile, boolPinged, objFSO
strComputer = InputBox("Please enter the computer IP or hostname that you want to look for a file on:", "Computer to search", "")
strFileName = InputBox("Please enter the file name (no extension) to search for:", "File name to search for", "")
strExtension = InputBox("Please enter the extension of the file name to search for:", "File name extension to search for", "")
boolPinged = Ping(strComputer)
If boolPinged = "True" Then
strFileList = "Files found and deleted on" & strComputer & vbCrLf
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' OR Drive = 'd:' OR Drive = 'e:') AND FileName = '" & strFileName & "' AND Extension = '" & strExtension & "'")
On Error Resume Next
For Each objFile In colFileList
If Err Then
MsgBox "No files were found."
WScript.Quit
End If
strFileList = strFileList & objFile.Name & vbCrLf
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(objFile.Name)
Next
MsgBox strFileList
Else
MsgBox "Could not ping " & strComputer
End If
WScript.Quit
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
*************** END OF SCRIPT****************************
Re: Script to Search and Delete Files from Remote Machines
"binuthomas" <[email protected]> wrote in message
news:[email protected]...
>
> I need to delete some files from remote machines (eg: all files ending
> with error.doc). I have got a script for the same. But the problem is
> it will delete only the files I specify, like if I mention error.doc it
> will delete only that particular file, it will not delete newerror.doc.
> Can you please modify the following script for the same. Also I need
> this script has to pick the machine names one by one from a text file
> and perform the task
>
> ****************START OF SCRIPT**********************
>
> Option Explicit
> Dim strComputer, strFileName, strExtension, boolFileFound, strFileList,
> objWMIService, colFileList, objFile, boolPinged, objFSO
>
> strComputer = InputBox("Please enter the computer IP or hostname that
> you want to look for a file on:", "Computer to search", "")
> strFileName = InputBox("Please enter the file name (no extension) to
> search for:", "File name to search for", "")
> strExtension = InputBox("Please enter the extension of the file name to
> search for:", "File name extension to search for", "")
> boolPinged = Ping(strComputer)
>
> If boolPinged = "True" Then
> strFileList = "Files found and deleted on" & strComputer & vbCrLf
>
>
> Set objWMIService =
> GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer
> & "\root\cimv2")
>
> Set colFileList = objWMIService.ExecQuery _
> ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' OR Drive =
> 'd:' OR Drive = 'e:') AND FileName = '" & strFileName & "' AND Extension
> = '" & strExtension & "'")
>
> On Error Resume Next
>
> For Each objFile In colFileList
>
> If Err Then
> MsgBox "No files were found."
>
> WScript.Quit
>
> End If
>
> strFileList = strFileList & objFile.Name & vbCrLf
>
> Set objFSO =
> CreateObject("Scripting.FileSystemObject")
>
> objFSO.DeleteFile(objFile.Name)
>
> Next
>
> MsgBox strFileList
>
> Else
> MsgBox "Could not ping " & strComputer
> End If
> WScript.Quit
>
> Function Ping(strComputer)
> Dim objShell, boolCode
> Set objShell = CreateObject("WScript.Shell")
> boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0,
> True)
> If boolCode = 0 Then
> Ping = True
> Else
> Ping = False
> End If
> End Function
>
> *************** END OF SCRIPT****************************
This is a standard maintenance task that can be performed with very
little coding, using standard Command Prompt commands, e.g. like so:
01. @echo off
02. echo.
03. set /p PC=Please enter the IP or hostname that you want to look for a
file on:
04. set /p Name=Please enter the file name (no extension) to search for:
05. set /p Ext=Please enter the extension of the file name to search for:
06. if "%Name%"=="" goto :eof
07. if "%PC%"=="" goto :eof
08. if "%Ext%"=="" goto :eof
09.
10. ping %PC% | find /i "bytes=" > nul && goto Action
11. echo Cannot reach PC "%PC%".
12. goto Exit
13.
14. :Action
15. set found=false
16. echo del /s "\\%PC%\c$\%name%.%ext%"
17. echo del /s "\\%PC%\d$\%name%.%ext%"
18. rem del /s "\\%PC%\c$\%name%.%ext%" | find /i "deleted" > nul && set
found=true
19. rem del /s "\\%PC%\d$\%name%.%ext%" | find /i "deleted" > nul && set
found=true
20. if %found%==true (
21. echo Files found and deleted on %PC%
22. ) else (
23. echo No files found on %PC%
24. )
25.
26. :Exit
27. echo Press the Space Bar to close this window.
28. pause > nul
Please note:
- In its current form the batch file will run in demo mode. To activate
it, delete lines 16 and 17 and remove the word "rem" at the
beginning of lines 18 and 19.
- You will find that WMI will be very, very slow when executing
the VB Script you propose. The above batch file should be
considerably faster. However, if you want really good speed
then you should combine this batch file with psexec.exe. Post
again if you need further details on this option.
Re: Script to Search and Delete Files from Remote Machines
Try with LIKE Operator instaed:
....
Set colFileList = objWMIService.ExecQuery("SELECT * FROM CIM_Datafile WHERE
(Drive = 'c:' OR Drive = 'd:' OR Drive = 'e:') AND FileName LIKE '%error'
AND Extension = '" & strExtension & "'")
See:
http://www.microsoft.com/technet/scr...2003/like.mspx
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> I need to delete some files from remote machines (eg: all files ending
> with error.doc). I have got a script for the same. But the problem is
> it will delete only the files I specify, like if I mention error.doc
> it will delete only that particular file, it will not delete
> newerror.doc. Can you please modify the following script for the same.
> Also I need this script has to pick the machine names one by one from
> a text file and perform the task
>
> ****************START OF SCRIPT**********************
>
> Option Explicit
> Dim strComputer, strFileName, strExtension, boolFileFound,
> strFileList,
> objWMIService, colFileList, objFile, boolPinged, objFSO
> strComputer = InputBox("Please enter the computer IP or hostname that
> you want to look for a file on:", "Computer to search", "")
> strFileName = InputBox("Please enter the file name (no extension) to
> search for:", "File name to search for", "")
> strExtension = InputBox("Please enter the extension of the file name
> to
> search for:", "File name extension to search for", "")
> boolPinged = Ping(strComputer)
> If boolPinged = "True" Then strFileList = "Files found and deleted on"
> & strComputer & vbCrLf
>
> Set objWMIService =
> GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer
> & "\root\cimv2")
>
> Set colFileList = objWMIService.ExecQuery _
> ("SELECT * FROM CIM_Datafile WHERE (Drive = 'c:' OR Drive =
> 'd:' OR Drive = 'e:') AND FileName = '" & strFileName & "' AND
> Extension
> = '" & strExtension & "'")
> On Error Resume Next
>
> For Each objFile In colFileList
>
> If Err Then
> MsgBox "No files were found."
> WScript.Quit
>
> End If
>
> strFileList = strFileList & objFile.Name & vbCrLf
>
> Set objFSO =
> CreateObject("Scripting.FileSystemObject")
> objFSO.DeleteFile(objFile.Name)
>
> Next
>
> MsgBox strFileList
>
> Else
> MsgBox "Could not ping " & strComputer
> End If
> WScript.Quit
> Function Ping(strComputer)
> Dim objShell, boolCode
> Set objShell = CreateObject("WScript.Shell")
> boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0,
> True)
> If boolCode = 0 Then
> Ping = True
> Else
> Ping = False
> End If
> End Function
> *************** END OF SCRIPT****************************
>
> http://forums.techarena.in
>
Re: Script to Search and Delete Files from Remote Machines
Are you referring to psexec.exe?
"binuthomas" <[email protected]> wrote in message
news:[email protected]...
>
> Can Anyone Help Me To Do This Using Pstools.....?
>
>
> --
> binuthomas
> ------------------------------------------------------------------------
> binuthomas's Profile: http://forums.techarena.in/member.php?userid=49768
> View this thread: http://forums.techarena.in/showthread.php?t=970921
>
> http://forums.techarena.in
>
Re: Script to Search and Delete Files from Remote Machines
"Shay Levi" <[email protected]> wrote in message
news:[email protected]...
>
>
> 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.
Re: Script to Search and Delete Files from Remote Machines
You don't need PSTools, you can do it from your own machine using WMIC:
This command runs against the comuters specified in the /node param (delimited
by a comma), it finds all files names ending with the word 'error' that have
the doc extension, the results are redirected to a log file. Launch CMD and
type:
WMIC /node:Computer1,Computer1 path cim_datafile WHERE "(Drive = 'c:' OR
Drive = 'd:' OR Drive = 'e:') AND FileName LIKE '%error' AND Extension ='doc'"
delete > c:\delete.log
You can get more info on WMIC here:
WMIC - Take Command-line Control over WMI
http://technet.microsoft.com/en-us/l.../bb742610.aspx
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> Can Anyone Help Me To Do This Using Pstools.....?
>
> http://forums.techarena.in
>
Re: Script to Search and Delete Files from Remote Machines
Just to make sure we're on the right path:
Create a directory under C:, name it 'test', then create 3 text files with
the default name (e.g 'new text doc...').
Now run this in CMD:
WMIC path cim_datafile WHERE "Drive = 'c:' AND path='\\test\\' AND FileName
LIKE 'new%' AND Extension ='txt'" delete
You should see similar output like below, which shows that the files were
deleted using LIKE:
C:>WMIC path cim_datafile WHERE "Drive = 'c:' AND path='\\test\\' AND FileName
LIKE 'new%' AND Extension ='txt'" delete
Deleting instance \\computerName\root\cimv2:CIM_DataFile.Name="c:\\test\\new
text document (2).txt"
Instance deletion successful.
Deleting instance \\computerName\root\cimv2:CIM_DataFile.Name="c:\\test\\new
text document (3).txt"
Instance deletion successful.
Deleting instance \\computerName\root\cimv2:CIM_DataFile.Name="c:\\test\\new
text document.txt"
Instance deletion successful.
Right?
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> HI SHAY,
>
> i tried to use LIKE.bt dint work. can you just try the same n give me
> the script.
>
> http://forums.techarena.in
>
Re: Script to Search and Delete Files from Remote Machines
Nice catch! The domain I'm in is not DST compliant yet. We hope to resolve
it soon.
Thanks for the performance check :)
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> "Shay Levi" <[email protected]> wrote in message
> news:[email protected]...
>
>> 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.
>
Re: Script to Search and Delete Files from Remote Machines
Put this in a bat file and run from CMD:
for /f %%a in (computers.txt) do (
WMIC /node:%%a path cim_datafile WHERE "(Drive = 'c:' OR Drive = 'd:' OR
Drive = 'e:') AND FileName LIKE '%error' AND Extension ='doc'" > c:\logs\%%a.log
)
Make sure you have a folder named 'logs' on C drive, each computer will have
its own log file in that directory.
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> there was some prob wit my LIKE. now i got it. thanks dude. now i need
> one more favor. i need my script to pick machine names from a text
> file. how does it possible..
>
> thanks in advance
>
> http://forums.techarena.in
>
Re: Script to Search and Delete Files from Remote Machines
thanks shay,
i was trying to run the batch file. but got the following error.
D:\test1>for /F %a in (computers.txt) do (WMIC /node:%a path cim_datafile WHERE
"(Drive = 'c:' OR Drive = 'd:' OR Drive = 'e:') AND FileName LIKE '\logs\%a.log
)
D:\test1>(WMIC /node:binupc_blr path cim_datafile WHERE "(Drive = 'c:' OR Drive
= 'd:' OR Drive = 'e:') AND FileName LIKE '\logs\binupc_blr.log )
Node - BINUPC_BLR
ERROR:
Code = 0x80041017
Description = Invalid query
Facility = WMI
[QUOTE=Shay Levi;3745023]Put this in a bat file and run from CMD:
for /f %%a in (computers.txt) do (
WMIC /node:%%a path cim_datafile WHERE "(Drive = 'c:' OR Drive = 'd:' OR
Drive = 'e:') AND FileName LIKE '%error' AND Extension ='doc'" > c:\logs\%%a.log
)
Make sure you have a folder named 'logs' on C drive, each computer will have
its own log file in that directory.
---
Shay Levi
Re: Script to Search and Delete Files from Remote Machines
add the path component ane leave the extension as is, for example:
WMIC /node:binupc_blr path cim_datafile WHERE "(Drive = 'c:' OR Drive = 'd:'
OR Drive = 'e:') AND path ='\\logs\\' AND FileName LIKE 'binupc_blr%' AND
extension ='log'"
---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
> thanks shay,
> i was trying to run the batch file. but got the following error.
> D:\test1>for /F %a in (computers.txt) do (WMIC /node:%a path
> cim_datafile WHERE
> "(Drive = 'c:' OR Drive = 'd:' OR Drive = 'e:') AND FileName LIKE
> '\logs\%a.log
> )
> D:\test1>(WMIC /node:binupc_blr path cim_datafile WHERE "(Drive = 'c:'
> OR Drive
> = 'd:' OR Drive = 'e:') AND FileName LIKE '\logs\binupc_blr.log )
> Node - BINUPC_BLR
> ERROR:
> Code = 0x80041017
> Description = Invalid query
> Facility = WMI
> Shay Levi;3745023 Wrote:
>
>> Put this in a bat file and run from CMD:
>>
>> for /f %%a in (computers.txt) do (
>> WMIC /node:%%a path cim_datafile WHERE "(Drive = 'c:' OR Drive = 'd:'
>> OR
>> Drive = 'e:') AND FileName LIKE '%error' AND Extension ='doc'" >
>> c:\logs\%%a.log
>> )
>> Make sure you have a folder named 'logs' on C drive, each computer
>> will
>> have
>> its own log file in that directory.
>> ---
>> Shay Levi
> http://forums.techarena.in
>
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
>
Re: Script to Search and Delete Files from Remote Machines
On May 20, 9:53 am, Shay Levi <[email protected]> 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/
Re: Script to Search and Delete Files from Remote Machines
On May 20, 10:54 am, Tom Lavedas <[email protected]> wrote:
> On May 20, 9:53 am, Shay Levi <[email protected]> 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/
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 <[email protected]> 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/
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
>
Re: Script to Search and Delete Files from Remote Machines
On May 20, 12:47 pm, Tom Lavedas <[email protected]> wrote:
> On May 20, 10:54 am, Tom Lavedas <[email protected]> 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/
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" <[email protected]> wrote in message
> news:[email protected]...
> >
> >
> > 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.
>
>
>
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" <[email protected]> wrote in message
news:[email protected]...
> 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" <[email protected]> wrote in message
>> news:[email protected]...
>> >
>> >
>> > 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.
>>
>>
>>