如何在批处理文件中随机生成文件夹中每个文件的名称?

时间:2015-11-16 16:14:19

标签: file batch-file random filenames file-rename

您好我想将文件夹中的所有文件重命名为随机名称,但它想将所有文件重命名为同名

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?

1 个答案:

答案 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!也可能会返回上述代码中未考虑的重复值。