I am trying to recursively change some file names according to their directory name but I am failed. Here is what I have done so far:
for /r %1 %%Z in (*.c) do ( echo ==== rem Change to the directory of .c file echo cd /d "%%~dpZ" cd /d "%%~dpZ" rem Change the file's name with its directory name with .c extension ren %%~nxZ %cd%.c ) And here is the directory structure:
SubDir renamer.bat sub1 file1.c sub2 file2.c so on so forth All the other posts say that using %cd% returns the current directory's name, however it returns something like that: c:\users\myusername\desktop\SubDir,means it returns the batch file's directory name. However, as you can see, I use cd command in the batch file, so I want it to returns only sub1, sub2, etc... Thus, I can able to change the file names into their directory's name:
ren file1.c sub1.c Thanks in advance.
EDIT: Answer
setlocal EnableDelayedExpansion for /r %1 %%Z in (*.c) do ( echo ==== rem Change to the directory of .c file echo cd /d "%%~dpZ" cd /d "%%~dpZ" rem Change the file's name with its directory name with .c extension FOR /f "delims=" %%a IN ("%%~dpZ\.") DO (ren %%~nxZ %%~nxa.c) )