-1

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

  • Try changing `dir /b /a-d` to ```Dir /B /S /A:-D``` and change `if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"` to ```If /I Not "%%a" == "%~f0” Ren "%%a" "%fullstamp% - %%~nxa"```. Both those changes are clear when reading the help information for both the `DIR` command, and the `FOR` command. The idea is not to find an exact matching question, but to adapt those you find, armed with the basic and built-in reference docs, and other questions on this site. I ask that you seriously consider studying what the code you found does, and how it works, then adapting it becomes far simpler. – Compo Jul 25 '22 at 21:14
  • Thanks for the response. after replacing the code, I am receiving the following message. '-' is not recognized as an internal or external command, operable program or batch file. – Network-Security Jul 27 '22 at 06:32
  • It seems, to me, as if you have at least one missing doublequote. If you directly copied and pasted my example above, please note that there is a strange typo/autocorrect, a curly doublequote has appeared where a straight one should be. I would suggest that may be the issue, so change `"%~f0”` to `"%~f0"`. – Compo Jul 27 '22 at 09:48
  • Yes, Thanks it works. But now when we run this batch again it renames the file again and again like 2022-07-27_15-10-13 - 2022-07-27_15-09-33 - 21. before this change when It renames any once it will not rename that file again. – Network-Security Jul 27 '22 at 10:14
  • That did not form part of your question Network-Security, and was not catered for in the code you submitted either. My comment was not a robust answer for the task, _(those go in the answer area)_, it was a fix for the specific issue you were reporting. If you need to also filter out every filename which already begins with a date in the used format, you'll need to include a `FindStr` filter within the parenthesized command to exclude them. The code you linked does include a pipe to `findstr` so should have took that into account, _(although I would not have done it like that)_. – Compo Jul 27 '22 at 10:37
  • The problem with the `FindStr` filter in your existing code, is that it is using a caret to filter names beginning with a date. That was fine with the original code, because the results were basenames with extensions. However with your modification the filenames are fully qualified absolute paths, which means they now begin with a drive letter, and directory path. You therefore need to adjust the `FindStr` filter to suit the new file paths. – Compo Jul 27 '22 at 10:48
  • Please guide, if you have a solution for this. Thanks for your support and guidance. – Network-Security Jul 27 '22 at 11:21
  • I have given you a guide, you need to modify the `findstr` filter(s). If you open up a Command Prompt window, type `findstr /?` and press the `[ENTER]` key, it will explain exactly how the command works. – Compo Jul 27 '22 at 12:44

1 Answers1

0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39