Articles

Batch File to Shorten File Names In a Folder?

February 14, 2011 at 14:39:45
Specs: Windows XP

Hello, does anyone know how to write a batch file that will shorten a file name if it is longer than 40 characters? I don't care if it truncates the file name at the end and I don't want to remove whitespace before the file extension.

I.E if the 36th character before the file extension is whitespace i want to leave it.
Original file name "A Files with whitespace before file extension.txt"
Rename to "A Files with whitespace before file .txt"

I need to be able rename all of the files that are all located in the same folder and all the files are text files. The batch file will not be in the same folder as the text files.

I.E. it would follow this structure:
The batch file will be in a folder. Inside this folder I will have say folderOne. Inside folderOne I will have folderTwo, Inside folderTwo I will have folderThree and inside folderThree will be all of the text files that need to be renamed.

Also not all the files in folderThree are longer than 40 characters. So if the file name is less than 40 characters long there shall be no change to the file name. Only files longer than 40 characters need to be renamed.

Any help on this would be greatly appreciated.
Thanks you.


See More: Batch File to Shorten File Names In a Folder?

Report •


#1
February 14, 2011 at 23:16:37

:: ==========================================
::
:: short240.bat Tue 15-02-2011 13:36:34.66
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set F=%%~Na
set F=!F:~0,36!
move /y "%%a" "!F!%%~Xa"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
February 15, 2011 at 08:56:07

Thanks Mecanix2Go!

This works great but it only works in the current directory. I have been trying to modify the batch file so that modifies files in a different directory where the batch file is not located. I.E. I want to modify the txt files say in FolderOne\FolderTwo.

Sorry for the inconvenience but I am not good at dos commands.

Thanks for your help.


Report •

#3
February 15, 2011 at 21:51:57

:: ==========================================
::
:: shorty40.bat Tue 15-02-2011 13:36:34.66
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d *.txt') do (
set F=%%~Na
set F=!F:~0,36!
move /y "%%a" "%%~DPa!F!%%~Xa"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
February 15, 2011 at 22:38:21

Mechanix2Go your are awesome.

Thank you so much for your help. This batch file works great.

I really appreciate you taking the time to help me out.

Thank you.


Report •

#5
September 3, 2011 at 16:01:22

I know this post may be a bit old but, would you mind explaining what happened in your code M2?

Any link to a website with such info will be greatly appreciated too.

Just want to learn more about batch files so I can understand it and modify/expand the code for my purposes ;)

Thank you.


Report •

#6
September 5, 2011 at 15:06:47

Mechanix2Go has more or less left the forums. I suggest you open the Command Prompt and type in the following:
FOR /?
SET /?

How To Ask Questions The Smart Way


Report •


Ask Question