Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE  (Read 10741 times)

0 Members and 1 Guest are viewing this topic.

BRIANH

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows XP
    Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
    « Reply #15 on: October 02, 2008, 01:23:36 PM »
    A bit of a differnet twist to my filename rename issue in a batch file.

    I basically was attempting to append a - and a numeric value beginning with 1 to
    each file in a directory. My aim was to increase the variable containing a numeric value for each file found and append it to the filename.. e.g. filex-1, filey-2, filez-3.

    I tried the following but ended up with the same value of 0 in the variable. Not sure why the variable was not being adjusted..

    Hopefully someone can spot where I've gone wrong in the code...

    mycode

    setlocal enabledelayedexpansion

    set filenum=0
     

    cd D:\TEST

    for    %%A in (*.txt) do (
            @echo on
            set /A filenum=%filenum%+1
            set oldname=%%A
            set newname=%%~nA-%filenum%.txt       
            rem echo "oldname"
            rem echo "newname"
           
            ren "!oldname!" "!newname!"
            rem pause
       )

    Dias de verano

    • Guest
    Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
    « Reply #16 on: October 02, 2008, 02:16:34 PM »
    Quote from: BRIANH
    Not sure why the variable was not being adjusted..

    Because %filenum% should have been !filenum!

    BRIANH

      Topic Starter


      Rookie

      • Experience: Familiar
      • OS: Windows XP
      Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
      « Reply #17 on: October 02, 2008, 02:37:57 PM »
      I suspect we're getting close..however the result is not what I had targeted...

      The updated file names all have the same variable value..

      e.g.   filex-1,   file-y-1,   filez-1

      I'm trying to get ----   filex-1,  filey-2,  filez-3.

      My fault in not being able to articulate clearly what I wanted...

      The for loop now is coded as:

      for    %%A in (*.txt) do (
              @echo on
              set /A filenum=%filenum%+1
              set oldname=%%A
              set newname=%%~nA-!filenum!.txt       
              rem echo "oldname"
              rem echo "newname"
             
              ren "!oldname!" "!newname!"
              rem pause
         )

      Dias de verano

      • Guest
      Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
      « Reply #18 on: October 02, 2008, 02:41:59 PM »
      You only changed the second %filenum% when you should have changed both.

      BRIANH

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows XP
        Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
        « Reply #19 on: October 02, 2008, 03:14:30 PM »
        Terrific !!!!   I have exactly what I want...

        thanks fo much !!!

        One of the issues I have with some of the code is that I don't fully understand how some of the sublties really work... e.g. the significance of the !variable! versus
        %variable% within the scope of the FOR statement.. I assume !variable! indicates that the new value of the variable is to be used each time through the loop as opposed to the %variable% which appears to use only the initial value of the variable set during the first loop ...   again, just guessing... I've attempted to find some information on this and haven't had much luck...the help of the FOR statement does not explain this well....

        Dias de verano

        • Guest
        Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
        « Reply #20 on: October 02, 2008, 03:47:08 PM »
        The topic you need to learn about is "delayed expansion". There is a ton of stuff you can find out by Googling, explained much better than I can, However here is a brief run through:

        When a batch file containing only % sign variables is run, the first thing that happens is that ALL of the variables are "expanded" into their known values. Then the code is executed. Any variables which are SET before a parenthetical expression such as a FOR loop or an extended IF statement, keep the value they had before, and those which are SET inside the expression are blank, as you have noticed.

        However, with Windows 2000 came delayed expansion. You enable it with setlocal enabledelayedexpansion and you use variables with ! instead of %.

        BRIANH

          Topic Starter


          Rookie

          • Experience: Familiar
          • OS: Windows XP
          Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
          « Reply #21 on: October 02, 2008, 03:51:52 PM »
          Thanks for the tip... I'll follow up....and do some reading...