1.

Solve : Delete files not only in Root but in Subfolders too?

Answer»

Ok im TRYING to make a batch program that will delete BAK and TMP FILES throught my HD. but im stuck on one thing. Deleting the folders in the subdirectories too. how can i do this?

Thanks in Advance
,Nickecho off
set path=c:\folderwithfiles
for /f "tokens=1*" %%a in ('dir /a %path%\*.bak /b /s') do del /q/f "%%a" Quote from: diablo416 on July 21, 2008, 08:39:47 AM

echo off
set path=c:\folderwithfiles
for /f "tokens=1*" %%a in ('dir /a %path%\*.bak /b /s') do del /q/f "%%a"

While the solution posted works for most files, filenames with embedded spaces will be bypassed. A more global solution might be:

Code: [Select]echo off
for %%x in (tmp bak) do (
for /f "tokens=*" %%a in ('dir /a c:\temp\*.%%x /b /s') do echo del /q/f "%%a"
)
)

Using path as a variable will change the system path until the command window is closed. Better to use another variable name or hardcode the directory.

The del command has a /s switch for subdirectories. Use at your own risk.

Just my 2¢ 

I dislike posting DESTRUCTIVE batch code, so I added an extra echo prior to the del command. Once you are happy with the preview, you can remove it.Sidewinder,

May I ask how do you highlight and frame the code part of your messages?
I looked at all toolbar options but I couldn't found any RELATED command.

Geza
For the code box use the # BBC tag.

For the bold highlight, use the "B" BBC tag

The icons are located under the subject line when replying to a post.

 

I'm sure SOMEONE else might explain it better, with pictures even. Code: [Select]I got it! Thanks!thanks diablo and sidewinder

it works


Discussion

No Comment Found