作者:icyheart | 时间:2007-09-07 16:48 | 标题:[已解决]有关random对文件重命名的问题
我想对当前目录下的文本文件进行随机的命名,可是遇到一个问题,只能对一个文本进行重命名,而其它的则不变
@echo off
set /a b=%random%%%100+1
for /r %%a in (*.txt) do (
set /a c=%random:~0,2%
ren %%~dpnsa.txt %c%.txt)
pause
[ Last edited by icyheart on 2007-9-7 at 06:27 PM ]
作者:knoppix7 | 时间:2007-09-07 16:51
再FOR执行的时候%random:~0,2%的值已经固定了。
LZ可以把@echo off改成@echo on看看。
这时就需要用变量扩展延迟了。
作者:wudixin96 | 时间:2007-09-07 16:52
%random%也会产生一样的数值啊
重命名前,先判断,再命名
作者:knoppix7 | 时间:2007-09-07 17:01
总之,要用变量扩展延迟!
%random%重复的可能性为1/32768.难道LZ的运气。。。
作者:icyheart | 时间:2007-09-07 17:05
要用变量延迟该怎么写呢
[ Last edited by icyheart on 2007-9-7 at 05:11 PM ]
作者:icyheart | 时间:2007-09-07 17:05
@echo off
set /a b=%random%%%100+1
:start
for /r %%a in (*.txt) do (
set /a c=%random:~0,2%
if %%~na neq %c% (ren %%~dpnsa.txt %c%.txt) else goto start)
pause
这样也不行
作者:knoppix7 | 时间:2007-09-07 18:21
%~XX不叫变量扩展延迟..
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /a b=%random%%%100+1
for /r %%a in (*.txt) do (
set /a c=!random:~0,2!
ren %%~dpnsa.txt !c!.txt)
pause
作者:icyheart | 时间:2007-09-07 18:26
想出来啦这样的应该是
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /r %%a in (*.txt) do (
set c=!random!
ren %%~dpnsa.txt !c!.txt)
pause