Following on from a previous question: PowerShell/Batch script to rename each file to include original name + folder name
I have the following folder structure, for several months:
2017
│
├─April reports
│ ├─01-04-2018
│ │ ├─XYZAPPROVED123.pdf
│ │ ├─XYZAPPROVED123_V2.pdf
│ │ └─XYZUNAPPROVED123.pdf
│ │
│ ├─02-04-2018
│ │ ├─XYZAPPROVED123.pdf
│ │ └─XYZUNAPPROVED123.pdf
│ │
│ │
│ └─30-04-2018
│ ├─XYZAPPROVED123.pdf
│ └─XYZUNAPPROVED123.pdf
│
├─ May reports
│ ├─01-04-2018
│ │ ├─XYZAPPROVED123.pdf
│ │ ├─XYZAPPROVED123_V2.pdf
│ │ └─XYZUNAPPROVED123.pdf
│ │
│ ├─02-04-2018
│ │ ├─XYZAPPROVED123.pdf
│ │ └─XYZUNAPPROVED123.pdf
│ │
│ │
│ ├─30-04-2018
│ ├─XYZAPPROVED123.pdf
.
.
.
├─December reports (etc)
Each folder for each day in the month contains an approved and an unapproved report with the same name ("XYZAPPROVED123" or "XYZUNAPPROVED123"). Some contain a V2.
I want to rename each one so that the "123" is removed and the folder name (the date) is included in the filename e.g. "XYZAPPROVED_01-04-2018". I don't want to completely replace the filename with the name of the folder, I just want it added on at the end separated by an underscore if possible.
Once I've done that, I'd like to delete all the files that contain "XYZUNAPPROVED" in the name and delete any "XYZAPPROVED" files where there is an "XYZAPPROVED_V2" in the same folder (version 2 supersedes the original).
I have managed to get a batch script to work for one month's (April) reports using the script below. The issue I am having is adding an additional loop so that I can run it for all the months.
April script (this works):
@echo off
cd "<actual path>"
for /D %%I in ("<actual path>\????-??-??") do (
for %%J in ("%%I\XYZ*.pdf") do (
if exist "%%I\XYZUN*" del /F "%%I\XYZUN*.pdf"
if exist "%%I\%%~nJ_V*.pdf" ( del "%%J" ) else ren "%%J"
"%%~nJ_%%~nI.pdf"
)
)
all months script- (nothing happens when I run it)
@echo off
cd "<actual path>"
for /D %%H in ("<actual path>") do (
rem Enter into the month name folder
rem process each date
for /D %%I in ("%%H\????-??-??") do (
for %%J in ("%%I\XYZ*.pdf") do (
if exist "%%I\XYZUN*" del /F "%%I\XYZUN*.pdf"
if exist "%%I\%%~nJ_V*.pdf" ( del "%%J" ) else ren "%%J" "%%~nJ_%%~nI.pdf"
)
)
)
What am I missing?