admin管理员组文章数量:1125603
I have this batch script but it is not renaming the files at all.
I have a list of documents that have been named in this format: yyyymmdd_docname1.pdf. I am trying to rename all of the documents to docname1_yyyymmdd.pdf.
Example, to rename:
20230101_fullbloodcount.pdf to fullbloodcount_20230101.pdf.
20230202_esr.pdf to esr_20230202.pdf
What is constant for all these documents is the date format that is appended in front of the document name.
The script is as per below. I would appreciate if you could look over the script and let me know what is wrong with it. Thank you in advance.
@echo off
setlocal enabledelayedexpansion
set "directory=C:\Users\user1\Downloads\Rename"
cd /d "%directory%"
for %%f in (??????_*.pdf) do (
set "filename=%%~nf"
set "extension=%%~xf"
set "datepart=!filename:~0,8!"
set "docpart=!filename:~9!"
set "newname=!docpart!_!datepart!!extension!"
ren "%%f" "!newname!"
)
echo Files have been renamed.
pause
I have this batch script but it is not renaming the files at all.
I have a list of documents that have been named in this format: yyyymmdd_docname1.pdf. I am trying to rename all of the documents to docname1_yyyymmdd.pdf.
Example, to rename:
20230101_fullbloodcount.pdf to fullbloodcount_20230101.pdf.
20230202_esr.pdf to esr_20230202.pdf
What is constant for all these documents is the date format that is appended in front of the document name.
The script is as per below. I would appreciate if you could look over the script and let me know what is wrong with it. Thank you in advance.
@echo off
setlocal enabledelayedexpansion
set "directory=C:\Users\user1\Downloads\Rename"
cd /d "%directory%"
for %%f in (??????_*.pdf) do (
set "filename=%%~nf"
set "extension=%%~xf"
set "datepart=!filename:~0,8!"
set "docpart=!filename:~9!"
set "newname=!docpart!_!datepart!!extension!"
ren "%%f" "!newname!"
)
echo Files have been renamed.
pause
Share
Improve this question
edited 2 days ago
Santiago Squarzon
59k5 gold badges21 silver badges50 bronze badges
asked 2 days ago
name_hiddenname_hidden
1
New contributor
name_hidden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8
|
Show 3 more comments
1 Answer
Reset to default 0Here's a basic example of one way to approach the task:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Directory=%UserProfile%\Downloads\Rename"
PushD "%Directory%" 2>NUL || GoTo :EOF
For /F "Delims=" %%G In ('Dir /A:-D /B "????????_*.pdf" 2^>NUL') Do (
If /I "%%~xG" == ".pdf" (
Set "_=%%~nG"
Set "$="
Set /A "$=%%~nG" 2>NUL
If Defined $ (
SetLocal EnableDelayedExpansion
If !$! GEq 19700101 (
If !$! LEq 20251231 (
Ren "%%G" "!_:~9!!_:~8,1!!$!%%~xG"
)
)
EndLocal
)
)
)
PopD
EndLocal
Exit /B
I have added some simple date validation to this code, (instead of relying upon up to any eight characters, or even a sequence of eight digits). The above is designed to work with all valid dates from 1970 up until the end of this year, (you would need to adjust the LEq
string beyond that). It does not however exclude invalid dates between those two, (e.g.20150229
or 20240931
), as doing so would be well beyond the requirements of your stated problem, and simple batch scripting.
本文标签: windows scriptingBatch script to rename files beginning with date format yyyymmddStack Overflow
版权声明:本文标题:windows scripting - Batch script to rename files beginning with date format yyyymmdd - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736630718a1945772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
??????
to representyyyymmdd
. The?
wildcard means zero or one character, so six of them will only match up to six characters, not the eight you want to target. – Compo Commented 2 days ago@pushd "%USERPROFILE%\Downloads\Rename" && ((@for /F "delims=" %%G in ('dir ????????_*.pdf /A-D-L /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "^[12][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]_"') do @for /F "tokens=1* delims=_" %%I in ("%%~nG") do @ren "%%G" "%%J_%%I%%~xG") & popd)
That command line works also for PDF files with one or more!
in file name. The batch file with this command line can be run multiple times on same folder even on files have been renamed already before with same batch file. – Mofi Commented 2 days ago20230202___esr.pdf
is renamed toesr_20230202.pdf
and not to__esr_20230202.pdf
, i.e. the sequence of underscores between date and rest of the file name is interpreted like a single underscore which can be good or not good depending on your requirements for such a file name. This command line in the batch file would works also on drives with FAT32 and exFAT although%USERPROFILE%
expands usually to a path on system drive which is nowadays always NTFS. However, loading first the file names list into memory and then renaming the files is always better. – Mofi Commented 2 days agofor
orfor /R
enumerate the directory (tree)? It explains the reason for using afor /F
loop which runs in background one more Windows Command Processor to run commanddir
, filter the output withfindstr
to avoid (not perfect) renaming files not beginning with a date but with eight other characters left to an underscore, capture the output file names list, and then rename one file after the other from the list in memory ofcmd.exe
. – Mofi Commented 2 days ago