1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to rename and add incrementing number suffix on multiple files in Batch Script?

Discussion in 'Software' started by r0mmel, Jul 14, 2011.

  1. r0mmel

    r0mmel Private E-2

    Hi Everyone,

    I have 500 files coming in and I need to first check if any file(s) exist then rename all of them regardless of what their filename is (the files are named in a different language).

    No need to process them in any order.

    Rename:

    1. “¦X¼d¬f-20110703-¦+¦dñHÑ-ª-¦=¬¦.xls”
    2. “¦X¼d¬f-20110707-¦+¡¦-+¡8.xls”
    3. “¦X¼d¬f-20110707-¦+¡¦ñj¦«.xls”
    4. “¦X¼d¬f-20110708-¦+¡¦¬M¼n.xls”
    5. “¦X¼d¬f-20110713-¦d¼O¼n¦hÑP.xls”
    .
    .
    .
    500


    To:

    “TWN_CH_INV_VISIT_FORM_01.xls”
    “TWN_CH_INV_VISIT_FORM_02.xls”
    “TWN_CH_INV_VISIT_FORM_03.xls”
    “TWN_CH_INV_VISIT_FORM_04.xls”
    “TWN_CH_INV_VISIT_FORM_05.xls”
    .
    .
    .
    “TWN_CH_INV_VISIT_FORM_500.xls”


    Hope you could help me on this one. I’ve been trying to do this for weeks.


    Thanks!
     
  2. GermanOne

    GermanOne Major Geek's Coding Genius Extraordinaire

    Re: How to rename and add incrementing number suffix on multiple files in Batch Scrip

    Place that batch into the same folder ...
    Code:
    @echo off &setlocal enabledelayedexpansion
    set /a n=1
    for /f "delims=" %%i in ('dir /a-d /b *.xls') do (
      set /a x=100!n! , n+=1
      ECHO ren "%%i" "TWN_CH_INV_VISIT_FORM_!x:~-3!.xls"
    )
    PAUSE
    
    ... run it and check the output. If you believe it would rename the files the right way then remove ECHO and PAUSE for arming.

    Regards
    GermanOne
     
  3. r0mmel

    r0mmel Private E-2

    Re: How to rename and add incrementing number suffix on multiple files in Batch Scrip

    Thanks German One!

    It showed the correct output. However, when I remove the ECHO and PAUSE, I get this error:

    "The system cannot find the file specified."

    Was there something I need to add on your script?

    Here's what I'm running. notice the removed echo:

    Code:
    @echo off &setlocal enabledelayedexpansion
    set /a n=1
    for /f "delims=" %%i in ('dir /a-d /b *.xls') do (
     set /a x=100!n! , n+=1
     ren "%%i" "TWN_CH_INV_VISIT_FORM_!x:~-3!.xls"
    )
     
  4. GermanOne

    GermanOne Major Geek's Coding Genius Extraordinaire

    Re: How to rename and add incrementing number suffix on multiple files in Batch Scrip

    Well I guess if this batch script doesn't work you have a problem with the characters in your file names. Maybe there are some unprintable characters which the forum software doesn't display.
    Try the same using VBScript.
    Code:
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(objFSO.GetParentFolderName(WScript.ScriptFullName))
    i = 0
    For Each objFile In objFolder.Files
      If LCase(objFSO.GetExtensionName(objFile.Name)) = "xls" Then
        i = i + 1
        objFSO.MoveFile objFile.Name, "TWN_CH_INV_VISIT_FORM_" & Right("00" & i, 3) & ".xls"
      End If
    Next
    
    Regards
    GermanOne
     
  5. r0mmel

    r0mmel Private E-2

    Re: How to rename and add incrementing number suffix on multiple files in Batch Scrip

    It worked! Thanks a lot GermanOne. :)
     

Share This Page

MajorGeeks.Com Menu

MajorGeeks.Com \ All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ NEW! PC Games \ System Tools \ Macintosh \ Demonews.Com \ Top Downloads

MajorGeeks.Com \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds