【问题标题】:How to randomly generate names for each file in folder in batch file?如何为批处理文件中文件夹中的每个文件随机生成名称?
【发布时间】:2015-11-16 16:14:18
【问题描述】:

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

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 . . .

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

【问题讨论】:

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


    【解决方案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! 也可能返回重复值,这些值在上述代码中没有考虑到。

    【讨论】:

      猜你喜欢
      • 2016-06-06
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多