简体   繁体   中英

using a batch file, how can i delete all files not in the windows dir?

i have a litle project for which i would like to remove all files outside of and not needed by the c:\\windows operating system. therefore i need to create a list of what i want to keep ie boot.ini c:\\windows etc and then delete everything else. i have seen examples using forFile, but i can be sure that extension will be on the machine. does anyone have any ideas how i can create the exclude list and then do the delete?

Here's a quick stab at a list of top level folders you should not touch:

$Recycle.Bin
Documents and Settings [C:\Users]
inetpub
MSOCache
Program Files
Program Files (x86)
ProgramData
Recovery
System Volume Information
Users
Windows

On XP Documents and Settings is a folder, on Vista and 7 it's a link to the Users folder.

MSOCache is safe to delete as well. However, a lot of installer packages are cached there and used for various MS products updates/repairs. As such, I'd actually recommend keeping it.

Program Files (x86) is the redirected Program Files for 32-bit apps on 64-bit OS installs. However, I've seen applications that install in this folder instead of Program Files even on 32-bit OSes, so I'd advise on keeping it and manually inspecting its content to make a decision if it should be gone.

Update: There's no reliable way to list the OS files only. The list can vary widely between OS version, SKU, architecture, OS features installed, hardware drivers, OS updates and installed applications.

Don't forget to backup whichever method you implement.

:: you can define parameters so it know not include the folders you don't want and check it like so

IF "%2" == "" 
IF "%1" == "RunMyBatchFile" ^


FOR %%i IN (""%filter%) ^  ::directory name goes in here that you want to iterate over todelete
DO (^::Do your stuff, make sure to use echo to see the output on the cmd
) ^

In addition to the comments about other folders than c:\\Windows, a lot of common windows components go in Program Files\\Common Files, and the application folders under Program Files are likely to have drivers that Windows won't boot normally without; AntiVirus, graphics and other non-default hardware drivers.

Here be dragons.

If what you want is to get rid of user data files, you only need to look in a couple of places, such as %USERPROFILE%\\AppData . The actual directory names vary with the OS (XP/Vista/7).

But don't do it to the Administrator, or yourself!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM