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?
Windows Batch

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
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

Paul Tomasi

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
Paul Tomasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Paul Tomasi

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

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
Paul Tomasi

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

Steve Knight

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

I think that has been around for a while...

~bp
Steve Knight

Dos 6.22 ...
Bill Prew

@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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck