Remove last seven characters from multiple filenames?

Page 3 of 4 FirstFirst 1234 LastLast

  1. Posts : 2,177
    Windows 8.1 Pro x64
       #21

    jdcrutch said:
    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 Computer


  2. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #22

    JDobbsy1987 said:
    jdcrutch said:
    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:

    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 Computer


  3. Posts : 2,177
    Windows 8.1 Pro x64
       #23

    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 Computer


  4. Posts : 742
    MS Windows 7 Ultimate 64-bit SP1
       #24

    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 Computer


  5. Posts : 8
    Windows 7 x64
    Thread Starter
       #25

    rraod said:
    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 Computer


  6. Posts : 8
    Windows 7 x64
    Thread Starter
       #26

    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:


    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:

    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 Computer


  7. Posts : 2,177
    Windows 8.1 Pro x64
       #27

    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 Computer


  8. Posts : 2,177
    Windows 8.1 Pro x64
       #28

    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 Computer


  9. Posts : 1
    32 bit xp
       #29

    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 Computer


  10. Posts : 2
    Windows 7 Pro 32-bit
       #30

    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 Computer


 
Page 3 of 4 FirstFirst 1234 LastLast

  Related Discussions
Our Sites
Site Links
About 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 05:04.
Find Us