简体   繁体   中英

Batch file to rename all files in a parent folder and sub folders by adding file modified date/time to beginning of the filename

I cannot find a solution for how to add a parent folder path; this will work on all the subfolders under the parent folder. like this will rename all the files in the subfolders. I am using the code in the below link but the folder path is unavailable here.

https://stackoverflow.com/a/24594200/19620197

Here is an example using the information I asked you to research from the built-in help.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "FullStamp="
For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
 -NoLogo -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd_hh-mm-ss'" 2^>NUL
) Do Set "FullStamp=%%G"
If Not Defined FullStamp GoTo :EOF
For /F "Delims=" %%G In ('Dir /B /S /A:-D ^| %SystemRoot%\System32\findstr.exe /R /V
 /C:"\\19[789][0-9]-0[1-9]-0[1-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-0[1-9]-[12][0-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-0[1-9]-3[01]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-0[1-9]-0[1-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-0[1-9]-[12][0-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-0[1-9]-3[01]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-0[1-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-[12][0-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-3[01]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-0[1-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-[12][0-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\19[789][0-9]-1[012]-3[01]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-0[1-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-[12][0-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-3[01]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-0[1-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-[12][0-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-0[1-9]-3[01]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-0[1-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-[12][0-9]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-3[01]_[01][0-9]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-0[1-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-[12][0-9]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 /C:"\\20[012][0-9]-1[012]-3[01]_2[0-3]-[0-5][0-9]-[0-5][0-9] - [^\\][^\\]*$"
 ') Do If /I Not "%%G" == "%~f0" Ren "%%G" "%FullStamp% - %%~nxG"

You will note that I have used powershell.exe to generate the date stamp, instead of wmic.exe , because the latter will be removed from the upcoming version of Windows 11, and the code is simpler. You should also be aware that your question title appears to suggest that you wanted the file modified date and time, but your code was only using the current date and time. As you should not ask multiple questions within a problem, I have continued to use the current date and time, as per your submitted code.


I will add however that in the help information it is not documented that the range [0-9] does not only include standard digits, it also includes subscript characters , , , , , , , , and ; and also superscript characters , ¹ , ² , ³ , , , , , and too. Because those characters are not invalid in Windows file and directory names, it is much better to not use a range at all, and instead list the set of individual digits.

Example:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "FullStamp="
For /F %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
 -NoLogo -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd_hh-mm-ss'" 2^>NUL
) Do Set "FullStamp=%%G"
If Not Defined FullStamp GoTo :EOF
For /F "Delims=" %%G In ('Dir /B /S /A:-D ^| %SystemRoot%\System32\findstr.exe /R /V
 /C:"\\19[789][0123456789]-0[123456789]-0[123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-0[123456789]-[12][0123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-0[123456789]-3[01]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-0[123456789]-0[123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-0[123456789]-[12][0123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-0[123456789]-3[01]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-0[123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-[12][0123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-3[01]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-0[123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-[12][0123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\19[789][0123456789]-1[012]-3[01]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-0[123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-[12][0123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-3[01]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-0[123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-[12][0123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-0[123456789]-3[01]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-0[123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-[12][0123456789]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-3[01]_[01][0123456789]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-0[123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-[12][0123456789]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 /C:"\\20[012][0123456789]-1[012]-3[01]_2[0123]-[012345][0123456789]-[012345][0123456789] - [^\\][^\\]*$"
 ') Do If /I Not "%%G" == "%~f0" Ren "%%G" "%FullStamp% - %%~nxG"

Please note that I have specifically used FindStr filters for dates between 1970-01-01_00-00-00 and 2029-12-31_23-59-59. However, due to limitations in FindStr 's implementation of regular expressions, and the maximum command line length allowed, the above examples do not validate dates What I mean by that, is, it does not determine whether the strings are real dates so you could have the thirty first day of the second month. However the code is significantly more robust than the [0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]* example used in your linked code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM