Board logo

标题: FOR 这样优化为什么不行? [打印本页]

作者: 313885174     时间: 2008-3-14 14:38    标题: FOR 这样优化为什么不行?
for /r c:\ %%i in (123456.txt) do del "%%i"
for /r d:\ %%i in (123456.txt) do del "%%i"
for /r e:\ %%i in (123456.txt) do del "%%i"
for /r f:\ %%i in (123456.txt) do del "%%i"
for /r g:\ %%i in (123456.txt) do del "%%i"



把上面的写成
for %%i in (c,d,e,f,g) do for /r %%u in (123456.txt) do del %%i:\"%%u"
这样的话P处理文件所在目录下对C D E F G查找 但后面的路径还是文件所在的原目录?

作者: pooronce     时间: 2008-3-14 16:27
你命令理解有误

FOR /R path] %variable IN (set) DO command


路径是放在变量前面的,所以正确写法应该是这样:

for %x in (c,d,e,f,g) do for /r %x: %u in (*.txt) do @echo %u






for %%i in (c,d,e,f,g) do call :ssss %%i
pause&exit
:ssss
set driver=%1
for /r %driver%: %%u in (123456.txt) do del %%u


有没有更好看方法,就看楼下的朋友了xD

作者: ngd     时间: 2008-3-14 17:21
for /r 都可以用 for /f 代替

@echo off
for %%i in (c d e f g) do (
for /f "delims=" %%j in ('"dir /a-d/b/s %%i:\123456.txt"') do del /f/q "%%j"
)


Last edited by ngd on 2008-3-14 at 05:34 PM ]

作者: 313885174     时间: 2008-3-14 18:49
2楼给的代码删不干净
3楼的测试可行


可以解释下这个是什么吗?
"delims="

Last edited by 313885174 on 2008-3-14 at 06:56 PM ]

作者: ngd     时间: 2008-3-14 23:38
for /f 语句中,默认以空格或者制表符(也就是按tab键产生的8个空格的字符)为分隔符
"delims=" 的含义是取消默认的分隔符
如果你去掉了delims选项,那么当文件含有空格等符号时,程序可能达不到你要的效果,甚至会出错。

作者: suntb     时间: 2008-3-14 23:49
@echo off
for %%i in (c d e f g) do (if exist %%i:\123456.txt del /f /q %%i:\123456.txt)

作者: chenall     时间: 2008-3-15 00:47
好像楼主要实现的功能不用这么麻烦吧,
用以下的就可以实现了,效率更高。

for %%i in (c,d,e,f,g) do del /f /s /q /a %%i:\123456.txt

作者: 313885174     时间: 2008-3-15 03:03
Originally posted by suntb at 2008-3-14 11:49 PM:
@echo off
for %%i in (c d e f g) do (if exist %%i:\123456.txt del /f /q %%i:\123456.txt)

没有用啊以前试过了要是不在前面加if exist %%i:\123456.txt最多每个分区下会删除掉文件然而子文件夹下还是存在的``
.......................................................

Last edited by 313885174 on 2008-12-27 at 21:02 ]

作者: 313885174     时间: 2008-3-15 03:13
Originally posted by chenall at 2008-3-15 12:47 AM:
好像楼主要实现的功能不用这么麻烦吧,
用以下的就可以实现了,效率更高。

你的代码只能删除分区下的文件子目录好象删除不了把...

作者: ngd     时间: 2008-3-15 12:42
Originally posted by 313885174 at 2008-3-15 03:13:

你的代码只能删除分区下的文件子目录好象删除不了把...

七楼的代码的确高效
因为 del 加了 /s 参数
----从当前目录及其所有子目录中删除指定文件。显示正在被删除的文件名。
我的代码更适合提取文件而非删除文件

LZ测试过了?

作者: suntb     时间: 2008-3-15 23:12
学习了 忘了dir还有/s开关