Delete all but one file loop with bat script
Asked Answered
C

1

1

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?

Cementum answered 14/8, 2017 at 16:7 Comment(5)
What output do you see? What about if you remove the >nul part? Anyways, I'm pretty sure you need the /q (and possibly also the /f and /a) flag for delElasticize
I see if 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
Even getting rid of the /s is not preventing it from outputting a command to the command line for every file found.Cementum
Your /S option is trying to delete in subdirectories, despite %i being relative only to a single level. I'd also suggest using If /I Not "%i"=="UPDATE.BAT" instead. If UPDATE.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
The command is working correctly - it's just not suppressing the output to the command line, which is what's causing my batch file to stop. Why is it still outputting a command for each file found?Cementum
R
3

If this is directly in an open cmd window and not a batch,
you can suppres output of the current command with a leading @ sign.

@for %i in (*) do @if /i not "%i"=="UPDATE.BAT" @del "%i" >nul 2>&1

In an batch toggle output of the commands with @Echo off and double the % signs of the for variable.

@Echo off
for %%i in (*) do if /i not "%%i"=="%~nx0" del "%%i" >nul 2>&1
Rathbone answered 14/8, 2017 at 16:51 Comment(8)
The /S option is unlikely required in this case and the /Q option is definitely not required.Selie
Well, the @for %i in (*) do @if /i not "%i"=="%~nx0" @del /q /s "%i" >nul 2>&1 did work at the command line, but the @Echo off for %%i in (*) do if /i not "%i"=="%~nx0" del /q /s "%%i" >nul 2>&1 did not work in the batch script - still shows in the logs that it hangs at that command.Cementum
@Cementum missed to double one %i to %%i changed. Compo is right the /S /Q dont' make sense.Rathbone
As >nul 2>&1 was used, I'm intrigued as to how your not mentioned previously logs show anything pertinent to the del command. Also, the command prompt code is wrong because the "%~nx0" notation will resolve to "" so "%i" would never equal it.Selie
I have a log for the entire batch script - just before each command, I send a comment with what is about to happen and a time stamp. It always hangs at "about to delete extra files from the Download directory." And I had already tried changing it to actually name the "UPDATE.BAT", but it still hung at the log entry just before this command was run.Cementum
@LotPings Thank you! The missing % was what was causing the problem! Just ran my update and it completed and did what I wanted!Cementum
Regardless of the fixed/edited command line version I stand by my first comment under this answer.Selie
Sorry @compo, had too much windows open and at least one change got lost.Rathbone

© 2022 - 2024 — McMap. All rights reserved.