您好我想将文件夹中的所有文件重命名为随机名称,但它想将所有文件重命名为同名
ren "c:\Test\*.txt" %Random%.txt
pause
输出:
C:\Users\Oliver\Desktop>ren "c:\Test\*.txt" 9466.txt
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
C:\Users\Oliver\Desktop>pause
Press any key to continue . . .
认识某人How to randomly generate names for each file in folder in batch file?
答案 0 :(得分:1)
在ren "C:\Test\*.txt" "%RANDOM%.txt"
之类的命令行中,%RANDOM%
只展开一次,因此它会尝试将每个文件重命名为同名。
要单独重命名每个文件,您需要遍历所有文件
为此,需要延迟扩展 - 请参阅set /?
。
以下是批处理文件解决方案:
@echo off
setlocal EnableDelayedExpansion
for %%F in ("C:\Test\*.txt") do (
ren "%%~F" "!RANDOM!.txt"
)
endlocal
这是命令行变体:
cmd /V:ON /C for %F in ("C:\Test\*.txt") do ren "%~F" "!RANDOM!.txt"
请注意,!RANDOM!
也可能会返回上述代码中未考虑的重复值。