我想创建一个bat,它获取文件所在目录的名称和文件的创建日期并将其添加到文件名中。
图像如下
文件名.txt→文件名_目录名_YYYYYMMDD.txt
示例:school_information.txt→school_information_university_20230909.txt
这是当前正在创建的命令。
@echo off
setlocal EnableDelayedExpansion
REM Get the current directory name
set "dirName=%CD%"
for %%a in ("%dirName%") do set "dirName=%%~nxa"
REM Rename the file
ren "file.txt" "file_!dirName!.txt"
试试这个:
@echo off
setlocal EnableDelayedExpansion
set "dirName=%CD%"
for %%a in ("%dirName%") do set "dirName=%%~nxa"
for %%f in (*.txt) do (
for /f "delims=" %%d in ('dir "%%f" /tc ^| find "%%f"') do (
set "fullDate=%%d"
)
set "year=!fullDate:~6,4!"
set "month=!fullDate:~0,2!"
set "day=!fullDate:~3,2!"
ren "%%f" "%%~nf_!dirName_!year!!month!!day!%%~xf"
)
endlocal
exit