Reputation: 23
In a folder, I had many file names such as:
ABC.DEF.GHI.YYYYMMDDhhmmss
JKL.MNO.PQR.YYYYMMDDhhmmsss
(not sure why sometimes the timestamp had 9 digits, based on the 24 hours)
The desired result is to truncate the timestamp, keep the filename with YYYMMDD
only:
ABC.DEF.GHI.YYYYMMDD
JKL.MNO.PQR.YYYYMMDD
How can I do this in a script batch file, windows (not Linux)? I appreciate your help very much.
Upvotes: 0
Views: 1364
Reputation: 38579
Here's a slightly more complete version which assumes that your target files actually have an extension which you omitted to tell us in your question. (I've used .csv
for demonstration purposes, just change it as needed).
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B /A:-D "<.<.<.>>>>>>>>>>>>>>>.csv"') Do (
For %%H In ("%%~nG") Do (
Set "DateStamp=%%~xH"
SetLocal EnableDelayedExpansion
Ren "%%G" "%%~nH!DateStamp:~,9!%%~xG"
EndLocal
)
)
[EDIT /]
Based upon your clarification, that your files were not assigned extensions by their creation software, your code can be simplified:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B /A:-D "<.<.<.>>>>>>>>>>>>>>>"') Do (
Set "DateStamp=%%~xG"
SetLocal EnableDelayedExpansion
Ren "%%G" "%%~nG!DateStamp:~,9!"
EndLocal
)
Upvotes: 1
Reputation: 2951
An expansion on my comment:
@Echo Off & Setlocal EnableDelayedExpansion
Set "FILENAME.EXT=%~1"
For /F "Delims=" %%X in ("!FILENAME.EXT!") Do for %%F in ("%%~nX") Do (Set "_DT=%%~xF" & Set "_FN=%%~nF.!_DT:~1,8!")
Echo/!_FN!
Endlocal & Exit /B
Note: the above is not scripted to your exact usage situation, it only demonstrates the principle in a testable way for you to be able to adapt to your needs.
Upvotes: 1