问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/45678627/delete-all-but-one-file-loop-with-bat-script