This is bascially a continuation of answered question but with additional condition
Add folder name to beginning of filename
I have a directory structure as below:
Folder
> SubFolder1xxxx
> FileName1.abc
> Filename2.abc
> .............
> SubFolder2xxxx
> FileName11.abc
> Filename12.abc
> ..............
> ..........
etc. I want to rename the files inside the subfolders as:
SubFolder1_Filename1.abc
SubFolder1_Filename2.abc
SubFolder2_Filename11.abc
SubFolder2_Filename12.abc
i.e. add part of the folder name at the beginning of the file name with the delimiter "_". The directory structure should remain unchanged.
I have below Script
for /d %D in (*) do (
for %F in ("%~D\*") do (
for %P in ("%F\..") do (
ren "%F" "%~nxP_%~nxF"
)
)
)
Problem with the above script is that it is taking the whole folder name
hence I tried to extract only part of the filename using syntax substring
%variable:~num_chars_to_skip,num_chars_to_keep%
So I modify above code with it, but it gave me error :(
for /d %D in (*) do (
for %F in ("%~D\*") do (
for %P in ("%F\..") do (
ren "%F" "%~nxP:~0,10%_%~nxF"
)
)
)
the error is basically the syntax subtring do not work, it just print it as a string. somthing like this
SubFolder1xxxx:~0,10%