Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch files

Status
Not open for further replies.
Jun 2, 2003
24
US
How do I write a batch file to delete all files in a directory except specific one?

Thanks.
 
DO not now how to delete all but a specific file in batch. Perhaps you could copy the specific file to another directory, delete all file, then copy the file back!!!
 
Create a new text doc, save it as what ever i.e. Delete.txt
Type the following for example:

DEL C:\TEMP\OLDFILES\*.* /Q

Then save the file and change the extension to .Bat

Job Done (Then /q is quiet mode and will NOT prompt you before deleting files, YOU HAVE BEEN WARNED!!!!)

Iain
 
Sorry didn't read that one properly, Dunno had a quick go but no luck I tried using the "/a -" switch but could get it to work sorry.

Iain
 
Depending on the names of the files, there could be many different ways to write a complicated batch file to do this.

If you are looking for a quick and dirty way to get the job done, then try what Stiddy suggested. Another way to do it would be to use attrib to mark the file as hidden before you delete all files, then unhide it afterwards.

attrib +H file.nam
del *.* /Q
attrib -H file.nam

In either case, I would probably want to add some error checking to make sure the delete command does not run if there is a problem with the copy or attrib command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top