Reputation: 3
I want to rename a bunch of files in a directory by stripping their file names down to the first 16 characters but I cannot work out how to get a substring of @FNAME when using the forfiles command in a batch file.
Help would be greatly appreciated.
Thanks
Roland
Upvotes: 0
Views: 3128
Reputation: 11
You can use a second bat file and pass @fname
like %1
and make the substring here.
Forfiles /m *.* /c "cmd /c call second.bat @fname"
In second.bat
Set v=%1
Set substring=%v:~1,5%
Echo %substring%
Upvotes: 1
Reputation: 31241
Using forfiles
to do this is most likely going to be quite tricky, as I don't think you can set and use variables in the same batch, because you spawn a new cmd for each file ("cmd /c command"
).
However, the same functionality can easily be achieved using a simple for
loop.
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir C:\yourdir /s /b') do (
set file=%%~na
set ext=%%~xa
set new=!file:~0,16!
ren %%a !new!!ext!
)
That will recurse through all subdirectories as well. If you just want the folder you choose, remove the /s
from the dir
command.
Also, because you are stripping just the file name, I have made it so it will append the file extension back on after, otherwise the characters in the extension would count towards the 16 character limit.
Upvotes: 2