Solved

Use WMIC command to delete folder, subfolders and/or files

Posted on 2010-11-22
3
2,853 Views
Last Modified: 2012-05-10
I have successfully used the command below in a batch file to get WMIC to delete *.tmp files from the %WINDIR%\TEMP folder on all named workstations on the network running XP. I haven't tested other versions but I assume that, given the age of WMIC, it'll work in later versions of Windows.

WMIC /node:Computer01,Computer02,Computer03 path cim_datafile WHERE "path='%%WINDIR%%\\TEMP\\' AND  Extension ='tmp'" delete

Open in new window


Additionally, I would like to delete some Start menu folders (and some shortcuts if possible) on all the computers on the network named in the "node" portion of the command. I'm a little rusty on the WMIC commands; is there a way to specify the path to the All Users' Start menu and delete a subfolder (and its contents) if the folder name matches a string? And is it possible to delete shortcuts if their file name matches a string)?

Ideally, I'd like the following subfolders (and any files contained within them):
C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\
C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Silverlight\
C:\Documents and Settings\All Users\Start Menu\Programs\Sophos\

and the following files:
C:\Documents and Settings\All Users\Start Menu\Programs\Acrobat.com.lnk
C:\Documents and Settings\All Users\Start Menu\Programs\Adobe Reader X.lnk
C:\Documents and Settings\All Users\Start Menu\Programs\HP Help and Support.lnk

...deleted from the Start menu of each named computer on the network.

I'm aware that this is a resource-intensive operation (particularly as there are may other folders I'd like to add to that list), but I plan on running this during network downtime so as not to slow the computers down when in use. My ultimate intention is to clear the clutter of our users' Start menus on the 400+ computers on the network without having to do so manually; I know how to set the node to read from a list in an external text file, as in:
WMIC /failfast:on /node:@"FILENAME.txt"

Open in new window

Many thanks in advance.
0
Comment
Question by:skrysiak
  • 2
3 Comments
 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
%TEMP% will take you to C:\Documents and Settings\username\Local Settings\Temp or equivalent
%ALLUSERSPROFILE% to C:\Documents and Settings\All Users or equivalent
More environment variables can be found here: http://vlaurie.com/computers2/Articles/environment.htm

also note that when using [Extension ='tmp'] - you may actually want just those files, but these temporary folders will be filled with tons of temporary files that don't have this extension. If you're doing a cleanup, you might want to be a little more generous with your file selection.



On a separate note, if you're using a batch file anyways, why not just use batch file commands?
@echo off
del  /q  /f  %Winddir%\temp\*.tmp
0
 
LVL 20

Accepted Solution

by:
alainbryden earned 500 total points
Comment Utility
See if the following works first:

Make a text file 'Workstations.txt':
-----------
COMPUTER01
COMPUTER02
COMPUTER03
_______

And in your batch file:
for /f %%i in (Workstations.txt) do del \\%%i\%WINDIR%\TEMP\*.tmp


Also note that in any of these cases %WINDIR% is an environment variable on your computer, and it's probably just subbing in the string "C:\WINDOWS" into your query, not actually checking what the windows directory is on that particular machine : /
0
 

Author Closing Comment

by:skrysiak
Comment Utility
Didn't know you could use external files in this way! Many thanks for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Transparency shows that a company is the kind of business that it wants people to think it is.
As a long-time IT Professional, the most important skill I have developed and consider to be my most valuable tool is Effective Troubleshooting. Step through my problem-solving procedure in this 10-step guide adapted from The Universal Troubleshooti…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now