Discussion:
New to WMIC
(too old to reply)
RayRedSox
2009-10-20 22:08:06 UTC
Permalink
I'm trying to write a batch file using WMIC to delete files out of the
windows\temp folder on a remote server. So far I've done the following,
but it keeps saying invalid query:

WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
AND Extension ='tmp'" delete

I'm taking precautions in case the windows temp folder is not on the C
drive, hence the reason for the %WINDIR%. Can I use parentheses in WMIC
command?

Thank you.
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile: http://forums.techarena.in/members/146555.htm
View this thread: http://forums.techarena.in/server-scripting/1260851.htm

http://forums.techarena.in
Tom Lavedas
2009-10-21 13:20:12 UTC
Permalink
Post by RayRedSox
I'm trying to write a batch file using WMIC to delete files out of the
windows\temp folder on a remote server. So far I've done the following,
WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
AND  Extension ='tmp'" delete
I'm taking precautions in case the windows temp folder is not on the C
drive, hence the reason for the %WINDIR%. Can I use parentheses in WMIC
command?
Thank you.
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
View this thread:http://forums.techarena.in/server-scripting/1260851.htm
http://forums.techarena.in
The SQL syntax is VERY particular. The PATH specification must be just
that, it cannot contain the drive letter. Plus, your original was not
escaping all of the backslashes. This is the way it worked for me ...

set tmp=%WINDIR:~2%\
set qry="drive='%WINDIR:~0,2%' and path='%tmp:\=\\%' and
extension='tmp'"
WMIC /node:'sup24app01' path cim_datafile WHERE %qry% delete

Note that I also had to enclose the machine ID in single quote marks
for it to work for me.
_____________________
Tom Lavedas
Tom Lavedas
2009-10-21 14:21:49 UTC
Permalink
Post by Tom Lavedas
Post by RayRedSox
I'm trying to write a batch file using WMIC to delete files out of the
windows\temp folder on a remote server. So far I've done the following,
WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
AND  Extension ='tmp'" delete
I'm taking precautions in case the windows temp folder is not on the C
drive, hence the reason for the %WINDIR%. Can I use parentheses in WMIC
command?
Thank you.
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
View this thread:http://forums.techarena.in/server-scripting/1260851.htm
http://forums.techarena.in
The SQL syntax is VERY particular. The PATH specification must be just
that, it cannot contain the drive letter.  Plus, your original was not
escaping all of the backslashes.  This is the way it worked for me ...
set tmp=%WINDIR:~2%\
set qry="drive='%WINDIR:~0,2%' and path='%tmp:\=\\%' and
extension='tmp'"
WMIC /node:'sup24app01' path cim_datafile WHERE %qry% delete
Note that I also had to enclose the machine ID in single quote marks
for it to work for me.
_____________________
Tom Lavedas
It occurred to me just after I posted that this approach still has a
fatal flaw. That is that the WINDIR variable needs to be determined
for the target machine. The way it is written, it is accessing the
local machine's environment, not the target machine. So I tried this
query to get the information from the target machine ...

set compname=sup24app01
set qry="name='windir'"
WMIC /node:'%compname%' environment WHERE %qry% get 'VariableValue' /
value

For my local machine test (I don't have domain admin privileges here),
the result was ...

VariableValue=%SystemRoot%

Which is not helpful. So I altered the query to request the
SystemRoot value instead and received a 'No Instance(s) Available.'
for my troubles. I don't know why this happened and I have not yet
figured out how to work around this problem. Maybe someone else does.
_____________________
Tom Lavedas
Tom Lavedas
2009-10-21 17:36:15 UTC
Permalink
Post by Tom Lavedas
Post by Tom Lavedas
Post by RayRedSox
I'm trying to write a batch file using WMIC to delete files out of the
windows\temp folder on a remote server. So far I've done the following,
WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
AND  Extension ='tmp'" delete
I'm taking precautions in case the windows temp folder is not on the C
drive, hence the reason for the %WINDIR%. Can I use parentheses in WMIC
command?
Thank you.
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
View this thread:http://forums.techarena.in/server-scripting/1260851.htm
http://forums.techarena.in
The SQL syntax is VERY particular. The PATH specification must be just
that, it cannot contain the drive letter.  Plus, your original was not
escaping all of the backslashes.  This is the way it worked for me ...
set tmp=%WINDIR:~2%\
set qry="drive='%WINDIR:~0,2%' and path='%tmp:\=\\%' and
extension='tmp'"
WMIC /node:'sup24app01' path cim_datafile WHERE %qry% delete
Note that I also had to enclose the machine ID in single quote marks
for it to work for me.
_____________________
Tom Lavedas
It occurred to me just after I posted that this approach still has a
fatal flaw.  That is that the WINDIR variable needs to be determined
for the target machine.  The way it is written, it is accessing the
local machine's environment, not the target machine.  So I tried this
query to get the information from the target machine ...
set compname=sup24app01
set qry="name='windir'"
WMIC /node:'%compname%' environment WHERE %qry% get 'VariableValue' /
value
For my local machine test (I don't have domain admin privileges here),
the result was ...
  VariableValue=%SystemRoot%
Which is not helpful.  So I altered the query to request the
SystemRoot value instead and received a 'No Instance(s) Available.'
for my troubles.  I don't know why this happened and I have not yet
figured out how to work around this problem.  Maybe someone else does.
_____________________
Tom Lavedas
I think I have a viable solution now using the Win32_OperatingSystem
class instead ...

@echo off
setlocal
set compname=sup24app01
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%compname%' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%' and path='%_path:\=\\%' and extension='tmp'"
WMIC /node:'%compname%' path cim_datafile WHERE %qry% delete

I did run into a 'Generic Error' when the procedure tried to delete a
file that was attached to a running process. I think the only way
around that is to get a list of the matching files and then work
through the list one file at a time, deleting them in turn.
Otherwise, the process is aborted at the first error, which may miss
files listed after the one in use. That is an exercise best left to
the student ;)
_____________________
Tom Lavedas
Pegasus [MVP]
2009-10-21 18:11:45 UTC
Permalink
Post by Tom Lavedas
Post by Tom Lavedas
Post by RayRedSox
I'm trying to write a batch file using WMIC to delete files out of the
windows\temp folder on a remote server. So far I've done the following,
WMIC /node:sup24app01 path cim_datafile WHERE "path='%WINDIR%\\TEMP\\'
AND Extension ='tmp'" delete
I'm taking precautions in case the windows temp folder is not on the C
drive, hence the reason for the %WINDIR%. Can I use parentheses in WMIC
command?
Thank you.
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
View this
thread:http://forums.techarena.in/server-scripting/1260851.htm
http://forums.techarena.in
The SQL syntax is VERY particular. The PATH specification must be just
that, it cannot contain the drive letter. Plus, your original was not
escaping all of the backslashes. This is the way it worked for me ...
set tmp=%WINDIR:~2%\
set qry="drive='%WINDIR:~0,2%' and path='%tmp:\=\\%' and
extension='tmp'"
WMIC /node:'sup24app01' path cim_datafile WHERE %qry% delete
Note that I also had to enclose the machine ID in single quote marks
for it to work for me.
_____________________
Tom Lavedas
It occurred to me just after I posted that this approach still has a
fatal flaw. That is that the WINDIR variable needs to be determined
for the target machine. The way it is written, it is accessing the
local machine's environment, not the target machine. So I tried this
query to get the information from the target machine ...
set compname=sup24app01
set qry="name='windir'"
WMIC /node:'%compname%' environment WHERE %qry% get 'VariableValue' /
value
For my local machine test (I don't have domain admin privileges here),
the result was ...
VariableValue=%SystemRoot%
Which is not helpful. So I altered the query to request the
SystemRoot value instead and received a 'No Instance(s) Available.'
for my troubles. I don't know why this happened and I have not yet
figured out how to work around this problem. Maybe someone else does.
_____________________
Tom Lavedas
I think I have a viable solution now using the Win32_OperatingSystem
class instead ...

@echo off
setlocal
set compname=sup24app01
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%compname%' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%' and path='%_path:\=\\%' and extension='tmp'"
WMIC /node:'%compname%' path cim_datafile WHERE %qry% delete

I did run into a 'Generic Error' when the procedure tried to delete a
file that was attached to a running process. I think the only way
around that is to get a list of the matching files and then work
through the list one file at a time, deleting them in turn.
Otherwise, the process is aborted at the first error, which may miss
files listed after the one in use. That is an exercise best left to
the student ;)
_____________________
Tom Lavedas

=============

I tried your routine on a Win2000 machine. It works very well. Unfortunately
it is very slow, taking 8 seconds to execute. The VB Script I suggested
takes 1 second, but only when I remove the error I have discovered just now.
Tom Lavedas
2009-10-21 20:09:51 UTC
Permalink
{snip}
Post by Pegasus [MVP]
I tried your routine on a Win2000 machine. It works very well. Unfortunately
it is very slow, taking 8 seconds to execute. The VB Script I suggested
takes 1 second, but only when I remove the error I have discovered just now.
But doesn't your approach require the SystemRoot\Temp folder to be
shared? FSO can use the UNC, but unless a share permission to delete
files is granted, it can't do anything can it. The only way it would
be possible would be to be using domain admin credentials and go in
through the admin share (C$, D$, etc.). In fact, your script does not
even seem to be constructing a UNC - or am I missing something?

BTW, I worked out an approach that deletes the files one by one using
WMIC ...

@echo off
setlocal
set _compname=%computername%

for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%_compname%' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a

set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%' and path='%_path:\=\\%' and extension='tmp'"

for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%_compname%' datafile WHERE %qry% get 'name' /value`
) do call :sub %%a
goto :EOF

:sub
set fn=%*
WMIC /node:'%_compname%' datafile WHERE "name='%fn:\=\\%'" delete

Of course, it stills requires domain admin credentials.
Pegasus [MVP]
2009-10-21 21:14:17 UTC
Permalink
{snip}
Post by Pegasus [MVP]
I tried your routine on a Win2000 machine. It works very well.
Unfortunately
it is very slow, taking 8 seconds to execute. The VB Script I suggested
takes 1 second, but only when I remove the error I have discovered just now.
But doesn't your approach require the SystemRoot\Temp folder to be
shared? FSO can use the UNC, but unless a share permission to delete
files is granted, it can't do anything can it. The only way it would
be possible would be to be using domain admin credentials and go in
through the admin share (C$, D$, etc.). In fact, your script does not
even seem to be constructing a UNC - or am I missing something?

==========

My script relies on the admin shares C$, D$, except that it uses C: instead
of C$ due to the mistake I made. Seeing that the OP appears to treat this
newsgroup much like a candyshop where he can hold out his hand and get any
amount of free code/candy without even saying "Thank you", I will let him
work out the correction to my mistake by himself if he wants to use the
code. It did not take me much time to cobble it together but I suspect that
you put a fair bit of effort into your solution.
RayRedSox
2009-10-21 22:12:11 UTC
Permalink
Thank you for your answer...I acutally tried your approach and i
worked, but I have one follow up question. I'm actually getting th
server name from a servers.txt file (rather than hardcoding it into th
compname variable in your example...so I tried the following, but I ge
invalid query:

Thanks a lot for your input. I appreciate it.



for /f %%b in (servers.txt) do (
@echo off
setlocal
set compname='%%b'
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%%b' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%'AND path='%_path:\=\\%' AND extension='xsl'"
WMIC /node:'%%b' path cim_datafile WHERE %qry% delete

--
RayRedSo
-----------------------------------------------------------------------
RayRedSox's Profile: http://forums.techarena.in/members/146555.ht
View this thread: http://forums.techarena.in/server-scripting/1260851.ht

http://forums.techarena.i
Tom Lavedas
2009-10-22 13:06:52 UTC
Permalink
Thank you for your answer...I acutally tried your approach and it
worked, but I have one follow up question. I'm actually getting the
server name from a servers.txt file (rather than hardcoding it into the
compname variable in your example...so I tried the following, but I get
Thanks a lot for your input. I appreciate it.
for /f %%b in (servers.txt) do (
@echo off
setlocal
set compname='%%b'
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%%b' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=%_WinDir:~0,2%
set _path=%_WinDir:~2%\temp\
set qry="drive='%_drv%'AND path='%_path:\=\\%' AND extension='xsl'"
WMIC /node:'%%b' path cim_datafile WHERE %qry% delete)
--
RayRedSox
------------------------------------------------------------------------
RayRedSox's Profile:http://forums.techarena.in/members/146555.htm
View this thread:http://forums.techarena.in/server-scripting/1260851.htm
http://forums.techarena.in
Look up delayed expansion in the SET statement's documentation
(SET /?).

Then the procedure would be something like this ...

@echo off
setlocal enabledelayedexpansion
for /f %%b in (servers.txt) do (
for /f "usebackq tokens=2 delims==" %%a in (
`WMIC /node:'%%b' os get 'WindowsDirectory' /value`
) do set _WinDir=%%a
set _drv=!_WinDir:~0,2!
set _path=!_WinDir:~2!\temp\
set qry="drive='!_drv!' AND path='!_path:\=\\!' AND
extension='xsl'"
WMIC /node:'%%b' path cim_datafile WHERE !qry! delete
)
_____________________
Tom Lavedas

Loading...