6
6
I have a directory structure as below:
Folder
> SubFolder1
> FileName1.abc
> Filename2.abc
> .............
> SubFolder2
> 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 the folder name at the beginning of the file name with the delimiter "_
". The directory structure should remain unchanged. Note: Beginning of file name is same. e.g. in above case File*.
I made below Script
for /r "PATH" %%G in (.) do (
pushd %%G
for %%* in (.) do set MyDir=%%~n*
FOR %%v IN (File*.*) DO REN %%v "%MyDir%_%%v"
popd
)
Problem with the above script is that it is taking only one Subfolder name and placing it to the beginning of file name irrespective of the folder.
1Are you restricted to doing this with
cmd.exe
? This would be a LOT easier (trivial, actually) with a Unix shell. – Nicole Hamilton – 2012-12-07T06:57:25.277Yes I do want to do it in cmd only as I am on Windows. I know, by installing bash tools i can do it more easily in unix. But I was just curious to get it done in cmd. and want to use the built in features of windows effectively. Moreover I don't have permission to install any third party tool on the machine I am working on. – shekhar – 2012-12-07T07:09:23.090
@NicoleHamilton - it is actually quite trivial in Windows batch as well. – dbenham – 2012-12-07T13:24:48.090
3@dbenham Your idea of trivial and my idea of trivial are quite different. – Nicole Hamilton – 2012-12-07T15:34:23.187