Kasa
Kasa

Reputation: 13

Attaching the foldername to all the all files in that particular folder

There are 100s of folders in a directory and inside each of the folder there is a (filename).ini file.

All I need to do is to attach the parent folder name to the .ini file so that it becomes (foldername)-(filename).txt file.

Is there a batch script which could do this? i am new to batch files. So, any help is greatly appreciated!

Upvotes: 0

Views: 91

Answers (2)

PA.
PA.

Reputation: 29369

This works for any folder and any folder structure.

@echo off
setlocal enabledelayedexpansion
for /R %%a in (*.ini) do (
  set foldername=%%~pa
  set foldername=!foldername:\=_!
  echo ren "%%~fa" "(!foldername!)-%%~na.txt"
)

For all the ini files in the current directory: extract the path from the filename, using the ~p modifier; change all backslash occurrencies to underscores, using the := modifier; finally rename the original ini file.

After testing, remove the ECHO

See HELP FOR and HELP SET for more information.

Upvotes: 0

Ryan Bemrose
Ryan Bemrose

Reputation: 9266

setlocal enabledelayedexpansion

for /R %%a in (*.ini) do (
  set relativepath=%%a
  set relativepath=!relativepath:%cd%\=!
  set newname=!relativepath:\=-!
  set newname=!newname:.ini=.txt!
  ren !relativepath! !newname!
)

endlocal

Upvotes: 1

Related Questions