使用bat脚本删除除一个文件循环外的所有文件 [英] Delete all but one file loop with bat script

查看:637
本文介绍了使用bat脚本删除除一个文件循环外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在批处理文件中有一个循环,可以删除目录中除一个文件外的所有文件(Windows 7)。但是它挂断了,因为它仍然将命令发送到屏幕上,即使我认为我已经抑制了它。这是我正在使用的命令:

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

测试了我的批处理脚本,日志显示了它停在此命令。在命令行中测试了该命令,将 del / s file.ext> 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?

推荐答案

如果直接在打开的cmd窗口中而不是批处理中,

您可以使用前导 @ 禁止当前命令的输出。

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

在批处理中切换带有<$ c的命令的输出$ c> @echo off 并将for变量的符号加倍。

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

这篇关于使用bat脚本删除除一个文件循环外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆