Link to home
Create AccountLog in
Windows Batch

Windows Batch

--

Questions

--

Followers

Top Experts

Avatar of lm1189
lm1189

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?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of oBdAoBdA

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


Avatar of Paul TomasiPaul 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
Avatar of Paul TomasiPaul Tomasi🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Paul TomasiPaul 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


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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

Avatar of Paul TomasiPaul 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


Avatar of Steve KnightSteve 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 T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


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

~bp

Avatar of Steve KnightSteve Knight🇬🇧

User generated image

@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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Windows Batch

Windows Batch

--

Questions

--

Followers

Top Experts

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.