如何在批处理文件中为文件夹中的每个文件随机生成名称?
How to randomly generate names for each file in folder in batch file?
我想将文件夹中的所有文件重命名为随机名称,但它想将所有文件重命名为相同的名称:
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 . . .
有人知道如何在批处理文件中为文件夹中的每个文件随机生成名称吗?
在像 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!
也可能 return 重复值,这些值在上述代码中未被考虑。
我想将文件夹中的所有文件重命名为随机名称,但它想将所有文件重命名为相同的名称:
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 . . .
有人知道如何在批处理文件中为文件夹中的每个文件随机生成名称吗?
在像 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!
也可能 return 重复值,这些值在上述代码中未被考虑。