此批處理文件保存有關其自身內部的最後一個文件夾名稱的信息。每次運行時,它都會檢索這些信息。如果找不到,則假定溫度。如果文件夾不存在,則會創建該文件夾。如果存在,則搜索新名稱,重命名該文件夾並將該信息保存在批處理文件中。
@echo off
setlocal enableextensions disabledelayedexpansion
rem Determine where to work
if "%cd:~-1%"=="\" (set "where=%cd%") else (set "where=%cd%\")
rem Determine what to search for in the current file
set "testString=:::set lastName=[0-9][0-9]*"
rem Retrieve the last name used
set "lastName="
for /f "tokens=* delims=:" %%a in ('findstr /r /b /e /c:"%testString%" "%~f0"') do %%a
if not defined lastName set "lastName=temp"
rem If the last folder does not exist, create it and finish
if not exist "%where%%lastName%\" (
mkdir "%where%\%lastName%"
echo(Folder [%lastName%] has been created
goto endProcess
)
rem Search for a new name
;:newNameLoop
set "newName=%random%"
if "%newName%"=="%lastName%" goto newNameLoop
if exist "%where%%newName%" goto newNameLoop
rem Rename the folder to the new name
ren "%where%%lastName%" "%newName%" 2>nul && set "save=1" || set "save="
rem If there were no problems, save the new name in current batch file
if defined save (
for /f "tokens=1,* delims=:" %%a in ('findstr /n /r /b /e /v /c:"%testString%" "%~f0" ^& break ^> "%~f0"') do >>"%~f0" echo(%%b
>>"%~f0" echo(:::set lastName=%newName%
echo Renamed [%lastName%] into [%newName%]
) else (
echo Rename operation failed. Ensure folder is not in use
)
rem End of the process, clean and exit
;:endProcess
endlocal
exit /b
而且,不,標籤中添加的分號不是拼寫錯誤。他們(或其它字符),需要避免與delims=:
問題什麼的一部分您有問題? – wimh