Re: Script to Search and Delete Files from Remote Machines
- From: Shay Levi <no@xxxxxxxx>
- Date: Wed, 21 May 2008 10:40:07 +0000 (UTC)
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...@xxxxxxxx> wrote:
I don't mind sitting and writing a LOOONG VBScript version of theI don't know WMIC syntax, but I know that the percent sign has special
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
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/
.
- References:
- Re: Script to Search and Delete Files from Remote Machines
- From: Tom Lavedas
- Re: Script to Search and Delete Files from Remote Machines
- Prev by Date: WMI Remote win32_process fails with Insufficient privilege
- Next by Date: Re: Script to Search and Delete Files from Remote Machines
- Previous by thread: Re: Script to Search and Delete Files from Remote Machines
- Next by thread: Re: Script to Search and Delete Files from Remote Machines
- Index(es):
Relevant Pages
|