随机重命名多个文件

时间:2017-09-28 19:58:55

标签: batch-file random menu

我创建了一个菜单来调用此脚本,该脚本随机重命名多个文本文件,它仅在.bat文件中工作。但它不能在我创建的菜单的上下文中工作,我相信它与循环有关,因为它只重命名第一个文件!我希望有人来评估这种情况,非常感谢你很多。

:4
cls

setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"
exit /b

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)

echo if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt

)

pause
goto Menu
)

:5 < - here start the next option of menu
...

2 个答案:

答案 0 :(得分:0)

也许是这样:

:4
cls

setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"

pause
goto Menu

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)

if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt
goto :eof

现有线路上的回声很奇怪。看起来没有那个人可能会出错。

答案 1 :(得分:0)

在更好地审核之后,我发现exit /b是在goto: eof之后窗口关闭的命令,它返回上面的命令。所以,我将exit /b部分替换为:

pause
goto Menu

现在它返回我的菜单

:Menu

完整代码:

cls

setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"

pause
goto Menu

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)

if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt

goto :eof