I have got a folder in which there are several hundred subfolders, and each of these subfolders would have exactly one file in it. How do I rename these files and give them the name of their respective parent folder? I am a beginner, so explanation of codes would be highly appreciated :)
Asked
Active
Viewed 515 times
-1
-
2you have to try something first. – Jean-François Fabre Oct 10 '16 at 19:52
-
Type `Help` in the command prompt. For each command listed type `help
` (eg `help dir`) or ` – Oct 10 '16 at 22:07/?` (eg `dir /?`). This will get you started `cd c:\program files & For /f "delims=" %A in ('Dir /b /s "c:\Program Files\*.exe"') Do Echo %~dpnxA` . For help on special characters see answer at http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch -
1Possible duplicate: [How to bulk add folder name to file name?](http://stackoverflow.com/q/16987846/3439404) and [a lot of similar questions](http://stackoverflow.com/search?q=%5Bbatch-file%5D+add+folder+name+to+file+name) – JosefZ Oct 10 '16 at 22:20
1 Answers
0
Here is something that may help:
@Echo Off
(Set dRoot=C:\Users\Pankan\MyDirectory)
If /I "%CD%" NEq "%dRoot%" (PushD "%dRoot%" 2>Nul||Exit/B)
For /F "Tokens=1*" %%A In ('RoboCopy /L /S /NC /NFL /NJH /NJS "%CD%" null *.*'
) Do If "%%A"=="1" Call :Sub "%%B"
Exit/B
:Sub
Set "pDir=%~1"
For %%A In ("%pDir:~,-1%") Do Set "nDir=%%~nxA"
For %%A In ("%~1*") Do If /I "%%~nxA" NEq "%nDir%%%~xA" Ren "%%A" "%nDir%%%~xA"
Change line two to suit the directory name holding all of your subfolders.

Compo
- 36,585
- 5
- 27
- 39