Link to home
Create AccountLog in
Avatar of lm1189
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?
Avatar of oBdA
oBdA

Assuming you don't want to do this for a single file, but a whole folder, here are two versions that will process all pdfs in the folder specified in "Source". Both are in test mode and will only display the "ren" commands they would normally run. Remove the uppercase ECHO to run them for real.
First ten characters:
@echo off
setlocal enabledelayedexpansion
set Source=C:\Temp
for %%a in ("%Source%\*.pdf") do (
	set NewFileName=%%~na
	set NewFileName=!NewFileName:~0,10!%%~xa
	ECHO ren "%%~a" !NewFileName!
)

Open in new window

Split at underscore:
@echo off
setlocal enabledelayedexpansion
set Source=C:\Temp
for %%a in ("%Source%\*.pdf") do (
	for /f "tokens=1 delims=_" %%f in ("%%~na") do set NewFileName=%%f%%~xa
	ECHO ren "%%~a" "!NewFileName!"
)

Open in new window

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%

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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"

Open in new window

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
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.

set filename=0000000001_someothertext.pdf

ren "%filename%" "??????????.*"

Open in new window

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
I think that has been around for a while...

~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