I have a loop in a batch file to delete all but one file in a directory (Windows 7). But it hangs up because it still sends the commands to the screen, even though I'd thought I'd suppressed it. Here is the command I'm using:
for %i in (*) do if not %i == UPDATE.BAT del /s %i >nul 2>&1
Tested my batch script, log shows it stops right at this command. Tested the command at the command line, outputs the "del /s file.ext >nul 2>&1" command to the prompt for each file in the directory, which is what causes my batch file to hang.
What do I need to change here?
>nul
part? Anyways, I'm pretty sure you need the /q (and possibly also the /f and /a) flag fordel
– Elasticizeif not file.ext == UPDATE.BAT del /s file.ext >nul 2>&1
for every file in the directory. I tried it without the>nul
and that did not work either. – Cementum/s
is not preventing it from outputting a command to the command line for every file found. – Cementum/S
option is trying to delete in subdirectories, despite%i
being relative only to a single level. I'd also suggest usingIf /I Not "%i"=="UPDATE.BAT"
instead. IfUPDATE.BAT
is the name of your running script you can also change that to"%~nx0"
to make that name not hard coded. Finally if you're using a batch file each instance of%i
should be%%i
. – Selie