lm1189
asked on
Renaming file and keeping first 10 characters of filename
Experts,
I have a filename "0000000001_someothertext. pdf" where the first 10 characters is what I want to name the file, the rest I want to remove, IE the end result is 0000000001.pdf. Is there a way to tell a batch file to remove any filename characters past the 10th character? Or perhaps anything where it sees a first underscore?
I have a filename "0000000001_someothertext.
yeah, in your batch file you need the following code:
set filename=0000000001_someothertext.pdf
for %%a in ("%filename%") do set extension=%%~xa
ren "%filename%" %filename:~0,10%%extension%
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or if you're only working with '.PDF' extension names then it's even easier:
set filename=0000000001_someothertext.pdf
ren "%filename%" "%filename:~0,10%.pdf"
Seems like the easiest way would just be:
ren "0000000001_someothertext. pdf" "??????????.*"
and if you have more than one file then you could do:
ren "*_*.* " "??????????.*"
~bp
ren "0000000001_someothertext.
and if you have more than one file then you could do:
ren "*_*.* " "??????????.*"
~bp
Not sure if the underscore ('_') is actually part of the filename or just a separator for the phrase 'someothertext'.
The first solution's neat though.
What version of DOS did that first appear in? I just tested it in 32-bit XP Pro and it worked fine.
The first solution's neat though.
What version of DOS did that first appear in? I just tested it in 32-bit XP Pro and it worked fine.
set filename=0000000001_someothertext.pdf
ren "%filename%" "??????????.*"
Paul - Haven't used it in a while but it certainly worked back in command.com for Windows 98 as I have scripts from back then that did before we had the for loop and %~ ways.
Steve
Steve
I think that has been around for a while...
~bp
~bp
@lm1189
Just curios, why did you chose that solution over http:#a38602319 ?
It's certainly your choice to accept what works for you, but seems like a simpler one solution should have been preferred over the more complex 2 line solution that requires delayed expansion of variables? Wanted to see if I was missing something.
~bp
Just curios, why did you chose that solution over http:#a38602319 ?
It's certainly your choice to accept what works for you, but seems like a simpler one solution should have been preferred over the more complex 2 line solution that requires delayed expansion of variables? Wanted to see if I was missing something.
~bp
First ten characters:
Open in new window
Split at underscore:Open in new window