Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Remove last seven characters from multiple filenames?

27 Mar 2012   #21
JDobbsy1987

Windows 8.1 Pro x64
 
 

Quote   Quote: Originally Posted by jdcrutch View Post
That helps a lot. Can't wait to try 'em out. I'd thank you again, but I might be starting to sound a bit effusive. ;0)
Not a problem, taught myself a few things on this script
Feel free to post back if you have an issues.

Regards,
JDobbsy1987


My System SpecsSystem Spec
.
27 Mar 2012   #22
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

Quote   Quote: Originally Posted by JDobbsy1987 View Post
Quote   Quote: Originally Posted by jdcrutch View Post
was only lopping off the last six digits. I don't know what the ~0,-11 arguments mean (assuming they're arguments), but I took a wild stab and changed -11 to -12. Now here's what I get:

Quote:
C:\Temp\test>renpdf.bat
C:\Temp\test>for %i in (*.pdf) do (set fName="%i")
C:\Temp\test>(set fName="EY 000000302768049.pdf")
C:\Temp\test>ren "EY 000000302768049.pdf" "EY 00000030.pdf"
which is exactly what I want. I don't know how it works, but it works. Thanks again, JDobbsy1987! Now for the loop!

Edit: a little research tells me that the ~0,-12 bit is a modifier that strips the last n characters (in this case, n=12) from a string. I guess I had to add a character to JDobbsy1987's -11 in order to account for the quotation mark I'd added, though I'm not sure why that doesn't let the space cause problems again. At any rate, it seems to work.
I'm glad this is what you are after, I nearly have the loop working, hopefully i will have this back to you by tomorrow evening (well this evening as it's past midnight )

You are correct about the below code
Code:
ren %fName% %fName:~0,-11%.txt
~0 = Remove the first n letters (we don't want to remove any hence 0)
,-11 = Remove the last n letters

Regards,
JDobbsy1987
you want to remove 8 characters and .pdf (4 character). so a total of 12
To let it work for more pdf files:

Code:
 
for %%i in (*.pdf) do (
set fName=%%i
ren "%fName%" "%fName:~0,-14%.pdf"
)
My System SpecsSystem Spec
27 Mar 2012   #23
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi,

This was kind of a test script to get the correct rename function... we have since moved onto the finished article at post #17 - Remove last seven characters from multiple filenames?

Regards,
JDobbsy1987
My System SpecsSystem Spec
.

27 Mar 2012   #24
rraod

MS Windows 7 Ultimate 64-bit SP1
 
 

I saw everyone enthusiastically discussing about the scripting language and batch file programming for removing the last seven characters from a filename.

Another simple alternative is to download the free Bulk Rename Utility No Installer version (zipped file), Unzip the files to a USB flash drive, and use it without installing on your office system.
My System SpecsSystem Spec
27 Mar 2012   #25
jdcrutch

Windows 7 x64
 
 

Quote   Quote: Originally Posted by rraod View Post
I saw everyone enthusiastically discussing about the scripting language and batch file programming for removing the last seven characters from a filename.

Another simple alternative is to download the free Bulk Rename Utility No Installer version (zipped file), Unzip the files to a USB flash drive, and use it without installing on your office system.
Thanks, Rraod. I'll try that at home, but at work I'm not allowed even to run unapproved programs, whether I install them on the system or not. I'm sure the makers of Bulk Rename Utility are honest and responsible, but if by some fluke a bad guy had cracked their site and infected their downloads with a virus, it'd mean my job, at a minimum. Security is a huge issue at my company.

Also, as JDobbsy1987 suggests above, this is a valuable learning opportunity for me. There's value, at least for me, in learning how to write simple scripts like this, to handle little everyday computer tasks.
My System SpecsSystem Spec
27 Mar 2012   #26
jdcrutch

Windows 7 x64
 
 

OK, I've tried JDobbsy1987's two batch files, and they appear to be having trouble with the spaces in my filenames. I put a file named <EY 000000012768048.PDF> into a Temp directory and ran RunMe.bat. It should have renamed the file <EY 00000001.PDF>, but here's what happened:
e:\Temp>dir /b | find "pdf" /i 1>temp.log
e:\Temp>set tempvar=
e:\Temp>for /F "tokens=1-2 delims=." %A in (temp.log) do (rename_file.bat %A %B )
e:\Temp>(rename_file.bat EY 000000012768048 pdf )
e:\Temp>set fName=EY
e:\Temp>ren EY.000000012768048 .000000012768048
The system cannot find the file specified.
(Yes, I found out how to redirect a batch file's output to a log file!)

I experimented with putting variables in quotation marks, but nothing seemed to work. Then I remembered, RTFM! C:\>FOR /? informs me that the "usebackq" option allows one to use double quotation marks on filenames in "file-set".

Unfortunately, that gets me only so far, because in our setup "file-set" is <temp.log>, not the filename with the troublesome space--which is a string within <temp.log>. So we need to put double quotes around the string in <temp.log>, either when it's written to that file, or when it's passed to <ReName_File.bat>.

A little more searching on the web took me to this thread (on a site that inflicts annoying pop-overs, or whatever they're called), where a helpful person points out the following:

Quote:

call gets expanded to:
:prtpath C: Documents and Settings All Users
which is correctly mapped to:
%1 = C:
%2 = Documents
%3 = and
%4 = Settings
%5 = All
%6 = Users The trick is to double quote the strings that may contain spaces:
for /f "usebackq tokens=1-7* delims=\" %%a in (`echo %myvar%`) do call
:prtpath "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g"
So I edited <RunMe.bat> to put double quotes around the parameters that get fed to <ReName_File.bat>, thus:

Code:
dir /b | find "pdf" /i > temp.log
set tempvar=
for /f "tokens=1-2 delims=." %%A in (temp.log) do (
rename_file.bat "%%A" "%%B"
)
del temp.log
(I also deleted the second "/f" that JDobbsy1987 had in his "FOR" clause, on the suspicion that it was unnecessary and had been left in by accident. The batch seems to run OK without it.)

I don't think I need quotes around %%B, but they don't seem to do any harm. The quotes around %%A added a digit to either end of string %A, so I edited <ReName_File.bat> to account for that, by changing JDobbsy1987's ~0,-7 to ~1,-8:

Code:
set fName=%1
 ren %fName%.%2 %fName:~1,-8%.%2
Only that caused a syntax error, which I don't understand. Changing the ~1 back to ~0 (but leaving -8) eliminates the error, and it seems to work, although I don't know why.

I initially added "usebackq" to the FOR options, but I accidentally deleted it, and the script ran just as well without it, so I guess it wasn't needed.

With those modifications, the script seems to work exactly right, except that it doesn't delete temp.log when it's finished. That's a very minor thing, and I'll figure out what's causing it when I have time--unless JDobbsy1987 beats me to it.

Just as an academic matter, I don't understand what's going on with the quotation marks I introduced. My log file reports the following:

Quote:
e:\Temp>dir /b | find "pdf" /i 1>temp.log
e:\Temp>set tempvar=
e:\Temp>for /F "tokens=1-2 delims=." %A in (temp.log) do (rename_file.bat "%A" "%B" )
e:\Temp>(rename_file.bat "EY 000001832768069" "pdf" )
e:\Temp>set fName="EY 000001832768069"
e:\Temp>ren "EY 000001832768069"."pdf" "EY 00000183."pdf"
<RunMe.bat> is putting quotation marks around the filename and the extension separately, with the dot delimiter in between, instead of around the whole filename (i.e., "filename"."ext" instead of "filename.ext"). I think I understand why it does that, but I don't know how to make it do the other, which I think would be better. Only it doesn't seem to matter, because I'm getting the results I want: the REN command seems to be ignoring the quotes around the dot (i.e., at the end of the filename and the beginning of the extension), even after the one at the end of the filename has been trimmed off, leaving an odd " ; and I don't know why it should do that. These are the mysteries of programming, I suppose. If any of you would care to enlighten me about them, I'll let you ride my bicycle.

Thanks again to JDobbsy1987 and all who've chimed in to help.
My System SpecsSystem Spec
27 Mar 2012   #27
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi Jim,

Spaces in file names... hhmmm... here it goes... (keeping to the same principle of 2 batch files)

Make batch file called "RunMe.bat" with the following code:
Code:
dir /b | find "pdf" /i > temp.log
 set tempvar=
 for /f "tokens=1-2 delims=." /f %%A in (temp.log) do (
 rename_file.bat "%%A" %%B
 )
 del temp.log
Now create a batch file called "ReName_File.bat" with the following code:
Code:
set fName=%1
 set fName=%fName:~0,-1%
 set fName=%fName:~1%
 ren "%fName%.%2" "%fName:~0,-7%.%2"
Put those 2 batch files in the folder with your PDF's and double click RunMe.bat (It may not delete the temp.log but thats nothing. once its finished you can just delete that).

I have tested the above scripts with PDF's in a folder, without spaces, with 1 space and multiple spaces.

/Fingers Crossed
My System SpecsSystem Spec
27 Mar 2012   #28
JDobbsy1987

Windows 8.1 Pro x64
 
 

Obviously as this is set to remove the last 7 characters, if you have mixed PDF's in the folder (i.e. some with spaces and some without then it may not work perfect)

e.g.
JDobbsy1987PDF1234567.pdf >> Would Become >> JDobbsy1987PDF.pdf
but
JDobbsy1987PDF123 4567.pdf >> Would Become >> JDobbsy1987PDF1.pdf

This is because the space will also be counted as 1 character.

Regards,
JDobbsy1987
My System SpecsSystem Spec
05 Oct 2012   #29
kevnssuresh

32 bit xp
 
 
Renaming filename using batch script

Hello Gurus,
I have seen lot of postings on renaming the filename using batch script (.cmd script or .bat script) None really worked for my scenario though it was same. Any help would be greately appreciated.

Here is my problem. I have several pdf files under "\\servername\C$\Test Scripts" folder that ends with "-en-us" so wanted to stript them off. For ex: "3test-en-us.pdf" to "3test.pdf" and it has to be done recursevely in all sub folders underneath of the above mentioned folder. None seems to be working. And this has to be automated, any code that does this would be greately appreciated.

Pl email me the code if you can to kevnss@textfree.us or reply back would be a great help.
My System SpecsSystem Spec
07 Oct 2014   #30
mubashar

Windows 7 Pro 32-bit
 
 
How to bulk rename multiple files with different file extensions

I have a Sourcefolder with following files and file types

ab c xxxxxx.sss
ab c xxxxxx.fff
ab c xxxxxx.ggg
ab c xxxxxx.hhh

where xxxxxx is datenumber code varying from dataset to dataset.

I wish to run a batch script (*.bat) which

1- temporarily changes the path to the SourceFolder directory

2- removes spaces (and the datenumbers) and renames all the files as follows

opqr.sss
opqr.fff
opqr.ggg
opqr.hhh


3- copies the renamed files to a SourceFolder\Dump folder.

4- moves the files from Dump folder to a Destination folder.

I will really appreciate your help.
My System SpecsSystem Spec
Reply

 Remove last seven characters from multiple filenames?




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Multiple file attachments have same content but different filenames.
Had a strange call from a member of staff today. They showed me an email in their Sent Items (Office 2007) where they had attached 4 separate documents. I can confirm that, on the server, each document has a different filename with different contents. The email was then sent to it's receiver. ...
Microsoft Office
How to Autofill Filenames?
I'm not sure how to word this question. I remember figuring out in Windows XP how to autofill filenames in Windows Explorer, but I can't remember how to do it anymore. Does anyone know what I mean and how to do it? So for instance, say I have 100 files that I want to name as XXXX01, XXXX02,...
General Discussion
filenames and titles of videos DVD to MP4
I am putting DVDs on an iPod using a DVD ripper and then iTunes. The DVDs have multiple videos. When then rip from the DVD to a folder on my hard drive, they all are titled with the DVD title and then numbered (such as Windows_Video_Title1.mp4) rather than with the name of each video. I went in...
Music, Pictures & Video
Multiple crashes and BSODs with random messages and filenames in them.
Hello, my computer began having problems a few weeks ago. It either crashes with BSOD, or just goes down with black screen, not shutting off, I can hear it's on, but screen is black, so I have to manually shut it off. It happens several times a day. Sometimes after restart (either by itself or by...
BSOD Help and Support
Foreign Characters/Characters appearing.
Hi there. This is an odd problem, whenever certain characters are used ("^ are just some) then a letter is pressed foreign characters appear. I.E When I press " Then O it comes up as Ö, instead of "O, and so on. Does anyone have any ideas, on how this could be fixed. I've checked in the...
Hardware & Devices
How explorer sorts filenames.
Is it possible to make windows filemanager explorer sort filenames in a way so it doesn't include names with THE under T?
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 06:37.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App