0

I'm currently trying to rename a file in a subfolder appending both the his subfolders name, the subfolder name immediately above in the path, and current date. This is the full path:

C:\USER_VALIDATION\INPUT\GRUPPO_FARLOCCO\AIX\merge.csv

I'd like that merge.csv become GRUPPO_FARLOCCO_AIX_17072020.csv I need to do that in a batch file and in a FOR, because I need to repeat this renaming action in every subfolder

C:\USER_VALIDATION\INPUT\.

Up until now I can get to this result: AIX_17072020.csv with the following code

FOR /D /R %%# in (*) DO (
    PUSHD "%%#"
  FOR %%@ in ("merge*") DO (
    Echo Ren: ".\%%~n#\%%@" "%%~n#_%date:/=%%%~x@"
        Ren "%%@" "%%~n#_%date:/=%%%~x@"
    )
    POPD
)
Pause&Exit

Now, I can get \USER_VALIDATION\INPUT\GRUPPO_FARLOCCO\AIX\ with %%~p@ but I cannot just get only the last two folders. I think it could be effective just remove the first 23 characters and replace the remaining two "" with "_" but I can't do it. Can someone please help? Thank you!

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Nicky No
  • 1
  • 2
  • And if you fully understand that link, you should be able to adapt that code to get the parent folder and the Grandparent folder. – Squashman Jul 17 '20 at 14:06
  • @Squashman I can get the right folder name with " for %%I in (..) do set ParentFolderName=%%~nI%%~xI" but immediately after the variable is empty (it turns my echo on when I try to print it), and if I place %%~nI%%~xI in front of my ren I get nothing. I think this is because I put that on my second FOR. Can you give advice? – Nicky No Jul 17 '20 at 14:37

1 Answers1

0

This is the working code:

@Echo OFF
FOR /D /R %%# in (*) DO (
    PUSHD "%%#"
  FOR %%@ in ("merge*") DO (
for %%I in (..) DO Ren "%%@" "%%~nI%%~xI_%%~n#_%date:/=%%%~x@"
    )
    POPD
)

Pause&Exit

Thanks for the share @squashman

Nicky No
  • 1
  • 2