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?

25 Mar 2012   #11
F5ing

Windows 7 Ultimate x64
 
 

Hey JDobbsy1987, kind of sloppy/clunky, but instead of renaming with the same extension go with something different, that way you could run it on a folder full of files. When it's done you run code to correct the extension.


My System SpecsSystem Spec
.
25 Mar 2012   #12
JDobbsy1987

Windows 8.1 Pro x64
 
 

Quote   Quote: Originally Posted by sibbil View Post
Hey JDobbsy1987, kind of sloppy/clunky
It is a little clunky at the moment but as i said in earlier posts, these commands were just to get the rename command to work correctly within the command window (not a batch file), now i have that all i need to do is create a new command with a for loop that will replace the previous commands mentioned in this thread. once i have done that it will not be clunky... just a standard batch file.


Quote   Quote: Originally Posted by sibbil View Post
instead of renaming with the same extension go with something different, that way you could run it on a folder full of files. When it's done you run code to correct the extension.
Why run a 2nd command to correct the file extension when the first command can do it at the same time, we know that files that need renaming are PDF's?

That is not the issue, this issue is just putting the rename command in a loop... nothing to do with the file extension.

I have nearly got the for loop working by the way
My System SpecsSystem Spec
25 Mar 2012   #13
JDobbsy1987

Windows 8.1 Pro x64
 
 

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
My System SpecsSystem Spec
.

25 Mar 2012   #14
F5ing

Windows 7 Ultimate x64
 
 

Hey JDobbsy1987,

Sorry I wasn't clear, I meant my addition would be sloppy/clunky, not yours.

Everytime I've gone and looked at batch files I've written over the years, I think "you know, I really could wipe out a few lines here and there and be more efficient just by modifying a couple other lines".
My System SpecsSystem Spec
25 Mar 2012   #15
JDobbsy1987

Windows 8.1 Pro x64
 
 

Quote   Quote: Originally Posted by sibbil View Post
Hey JDobbsy1987,

Sorry I wasn't clear, I meant my addition would be sloppy/clunky, not yours.

Everytime I've gone and looked at batch files I've written over the years, I think "you know, I really could wipe out a few lines here and there and be more efficient just by modifying a couple other lines".
Ah, I misunderstood sorry , i do find that i end up re-writing old batch files reducing them from 20 lines down to 10 as i pick up more over time.
My System SpecsSystem Spec
25 Mar 2012   #16
F5ing

Windows 7 Ultimate x64
 
 

I thought you were having problems with a working, but never ending command. I was thinking you were looking for a way to stop the loop at the right moment.
My System SpecsSystem Spec
26 Mar 2012   #17
JDobbsy1987

Windows 8.1 Pro x64
 
 

OK Jim...

I have now got the script to work, 2 batch files are required for it.
All the .PDF's will need to be put in a folder for it to work (obviously if you can change the script to look in different folders then go for it

Please put a copy of a few PDF's into a folder to test the following:

First you need to create the 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
 ren %fName%.%2 %fName:~0,-7%.%2
Put the 2 batch files in the folder with the test pdf's and then double click the RunMe.bat

It may take a second but all the files should now have the last 7 digits removed?

Please advise how you get on.
Sorry it has taken so long to get to the bottom of it

Regards,
JDobbsy1987
My System SpecsSystem Spec
26 Mar 2012   #18
jdcrutch

Windows 7 x64
 
 

Brilliant, JDobbsy1987! I really appreciate your putting in all this time to help me.

Just for my education, and not trying to be a know-it-all, but it seems strange to have to run two separate batch files, rather than one with function calls. Can you explain why the former is better? (Easier counts as better in my book, even if it's less elegant, since you're doing this out of the goodness of your heart, after all.)

If you have the further time and inclination to walk me through these scripts, step by step, so I'll understand how they work, I'll be even more grateful--because I realize it wouldn't be all that much trouble for me to educate myself about all this, and you've already done a lot. It's not clear to me, e.g., since you don't have an "IF" or a "FOREACH" (which I don't think DOS recognizes), how the script knows to stop running after it's renamed each file once, and not keep going until all the files have names of less than seven characters. So if you feel like educating me as well as helping me out, please do.

I do like the way you got around having to restore the file suffix, by the way, and I think I even understand how you did it. I knew there must be a way.

I'll give these a try when I get home tonight (don't want to try them at work yet, because the firm has very strict IT policies, and I want to be able to say that I've tried them at home, and they work, when I ask permission to run them at the office).

Thanks again, JDobbsy1987. You're a champ.

Regards,

Jim Crutchfield
My System SpecsSystem Spec
26 Mar 2012   #19
JDobbsy1987

Windows 8.1 Pro x64
 
 

Hi Jim,

It's not a problem, the reason i have 2 batch files is it's to do with how loops deal with variables and the order the commands are run in the stack (it may be possible to get it to run in a single batch file but much easier {for me} to get it split over 2 although I'm not actually sure it is possible to do this is a single command/batch file due to the rename variable).

You don't need to specify the file extension in the script as the for loop uses a delimiting character to split it into two variables (%%A and %%B) with A being the file name, and B being the extension.

Both of those are then loaded into rename_file.bat as %1 and %2 respectively. It works this way because rename_file.bat is called for each loop, and when it finished its two lines, it closes, and therefore removes the fName variable so it can be reused by the next loop.

I hope his helps understand it a little more.

Regards,
JDobbsy1987
My System SpecsSystem Spec
26 Mar 2012   #20
jdcrutch

Windows 7 x64
 
 

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)
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 00:58.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App