Delete all but one file loop with bat script

心不动则不痛 提交于 2019-12-11 08:24:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!