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?

23 Mar 2012   #1
jdcrutch

Windows 7 x64
 
 
Remove last seven characters from multiple filenames?

Hello.

I have a bunch of documents in a data base, all associated with TIFF images, and all having unique DOCIDs. When I batch-print several of the TIFFs to PDFs, the stupid program, instead of just naming them by their DOCIDs, appends a seven-digit serial number to the DOCID, thus:
ABCD0000789 --> ABCD00007891234567.PDF
I want to delete those last seven digits before the dot.

I know there are free utilities that make this stuff easy, but I'm at work, where I'm not allowed to install programs; so I have to use native DOS or Windows 7 functions. I'm sure it can be done quickly and easily with the REN command in a DOS window, but I don't know how to identify the last seven digits for REN and tell it to delete them.

Unfortunately, what comes before the last seven digits is variable--it's always some letters followed by some numerals, but the number of each can vary, there might be a hyphen or a space among the letters, and there might be a space between the letters and the numerals; so I can't just tell DOS to delete everything after the first n digits, which I gather would be easier. (I've found several examples of scripts for removing the first so-many digits, but I can't figure out how to adapt those to solve my problem.)


Thanks for any help.

Regards,

Jim Crutchfield
Long Island City, NY


My System SpecsSystem Spec
.
23 Mar 2012   #2
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi,

I too wouldn't mind knowing how to do this if it's possible.
I seem to have gotten as far as you.. that being i can remove x digits from the beginning but not from the end

I will see if i can get someone that knows

Regards,
My System SpecsSystem Spec
23 Mar 2012   #3
profdlp

Main - Windows 7 Pro SP1 64-Bit; 2nd - Windows Server 2008 R2
 
 

ReNamer Products den4b.com

Been using it for years. Pay attention to the preview to make sure you've got it the way you want it.

EDIT: Oops. Didn't catch the part about "Not installing programs". Sorry.
My System SpecsSystem Spec
.

23 Mar 2012   #4
profdlp

Main - Windows 7 Pro SP1 64-Bit; 2nd - Windows Server 2008 R2
 
 

Just so I'm not a total drag, can you link to one of the scripts you've tried so far? Though I wouldn't consider myself a "Programmer", I've had a few programming assignments similar to this stuff. Maybe I can make up for being a gun-jumper.
My System SpecsSystem Spec
23 Mar 2012   #5
JDobbsy1987

Windows 8.1 Pro x64
 
 

I have been running straight in the dos window while testing hence only 1 % and only testing on 1 file at the moment

Code:
for %i in (*.txt) do (set fName=%i)
then
Code:
ren %fName% %fName:~7%
That just removes the first 7 digits... still working on the last digits.
My System SpecsSystem Spec
23 Mar 2012   #6
JDobbsy1987

Windows 8.1 Pro x64
 
 

based on my example above i think i have done it!!

From within the cmd window i have changed the directory to where i have put some test .txt files and the ran the 2 commands below.

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

Windows 8.1 Pro x64
 
 

Quote   Quote: Originally Posted by JDobbsy1987 View Post
based on my example above i think i have done it!!

Code:
for %i in (*.txt) do (set fName=%i)
ren %fName% %fName:~0,-11%.txt
Just need to update the file extension to .pdf and to put it in some sort of loop to do multiple files?
My System SpecsSystem Spec
25 Mar 2012   #8
jdcrutch

Windows 7 x64
 
 

Thanks for the help!

I'm working with only one file at the moment, and I copied JDobbsy's code exactly, except that I put in ".pdf" for ".txt":

Code:
for %%i in (*.pdf) do (set fName=%i)
ren %fName% %fName:~0,-11%.pdf
Unfortunately, when I try that, I get,
Quote:
C:\Temp\test>i) was unexpected at this time.
C:\Temp\test>for i)
The help file for "FOR" says,
Quote:
To use the FOR command in a batch program, specify %%variable instead of %variable.
so I tried
Code:
for %%i in (*.pdf) do (set fName=%i)
ren %fName% %fName:~0,-11%.pdf
which results in
Quote:
C:\Temp\test>for %i in (*.pdf) do (set fName=i )
C:\Temp\test>(set fName=i )
C:\Temp\test>ren i .pdf
The system cannot find the file specified.
I'm probably just showing my ignorance here--or maybe my senility: it's been twenty years since I tried to write a DOS batch file, and I've completely forgotten the language. I'm probably leaving out standard code that you guys take for granted.

Meanwhile, I've found the following on DOStips.com, which looks like it might be something in the right direction:

Code:
:rTrim string char max -- strips white spaces (or other characters) from the end of a string
::                     -- string [in,out] - string variable to be trimmed
::                     -- char   [in,opt] - character to be trimmed, default is space
::                     -- max    [in,opt] - maximum number of characters to be trimmed from the end, default is 32
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call set string=%%%~1%%
set char=%~2
set max=%~3
if "%char%"=="" set char= &rem one space
if "%max%"=="" set max=32
for /l %%a in (1,1,%max%) do if "!string:~-1!"=="%char%" set string=!string:~0,-1!
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" SET %~1=%string%
)
EXIT /b
But I can't really read it well enough to know. I don't think it requires the string to have any particular number of characters before the ones we want to trim.

I suspect that if we set "%char%" to * (i.e., any character) and set "%max%" to equal seven (or 11 if we can't define the file suffix out of our string), it might work; but we'd need to preserve (or restore) the file suffix.

I also don't see how the string is defined in the first place (maybe that "call" function at the beginning?); and I'm not sure how to make a loop in a DOS batch file, though that shouldn't be hard to find out.

I'll give it a try, but I'll have to rely on you guys to make it work. ;0)

Thanks again!

Regards,

Jim Crutchfield
My System SpecsSystem Spec
25 Mar 2012   #9
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi Jim,

I will look further into getting it to work in a loop for multiple files tomorrow however the code i posted was entered directly into the command prompt.

In order to use that in a batch file you need double %% which you did pick up on:

Quote   Quote: Originally Posted by jdcrutch View Post
so I tried
Code:
for %%i in (*.pdf) do (set fName=%%i)
ren %fName% %fName:~0,-11%.pdf
however you forgot the second %i

Try this:
Code:
for %%i in (*.pdf) do (set fName=%i)
ren %fName% %fName:~0,-11%.pdf
This will only work for 1 PDF though.

As i said, i will work a little more on this to try and create the batch file to do multiple file tomorrow.
As i know this command works, i know it will do the trick, the issue getting it to work for multiple file is getting the for /f loop to work (not really done a lot with for loops before)

Regards,
JDobbsy1987
My System SpecsSystem Spec
25 Mar 2012   #10
jdcrutch

Windows 7 x64
 
 

Aha! Thanks, JDobbsy! Adding another % let the command run, but I hove up against another problem: many of my filenames have spaces in them, which make REN return a syntax error. By trial and error, I figured out how to put the fName variable in quotation marks:

Code:
for %%i in (*.pdf) do (set fName="%%i")
and that worked like a charm, except that

Code:
ren %fName% %fName:~0,-11%.pdf
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.
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:39.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App