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

Author Topic: Trouble With A Recursive Rename Batch File  (Read 6826 times)

0 Members and 1 Guest are viewing this topic.

Mulsiphix

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    Trouble With A Recursive Rename Batch File
    « on: April 05, 2016, 05:32:18 PM »
    I have a ton of files with names that end in *.crop.jpg and I would like to change crop to full. I have done a lot of searching and I read the Microsoft TechNet articles for REN and FOR, but I just can't get it. Is what I am trying to do impossible inside a batch file?

    Trying
    Code: [Select]
    for /r %%a in (*.crop.jpg) do ren "%%a"  "*.full.jpg"
    OR

    Code: [Select]
    for /r %%I in (*.crop.jpg) do ren "%%I"  "%%~nI.full.jpg"
    Result
    Grandma.crop.jpg ----> Grandma.crop.full.jpg

    Salmon Trout

    • Guest
    Re: Trouble With A Recursive Rename Batch File
    « Reply #1 on: April 06, 2016, 01:04:44 PM »
    When you say "recursive" do you mean you want to operate on every file in a folder, and every file in every subfolder under that folder?

    Mulsiphix

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: Trouble With A Recursive Rename Batch File
      « Reply #2 on: April 06, 2016, 01:08:24 PM »
      When you say "recursive" do you mean you want to operate on every file in a folder, and every file in every subfolder under that folder?

      Yes  :)

      Salmon Trout

      • Guest
      Re: Trouble With A Recursive Rename Batch File
      « Reply #3 on: April 06, 2016, 02:01:55 PM »
      Try this
      Put it in the top folder and if it shows the right renames
      change this line:
      echo REN "!oldname!" "!newname!"
      to this:
      REN "!oldname!" "!newname!"

      Perhaps best to try it on some test files before you let it loose on your real data (if you are sensible you make backups!)

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      for /f %%A in ('dir /s /b *.full.jpg') do (
      set folder=%%~dpA
      set filename=%%~nA
      set oldname=!folder!%%~nA.jpg
      set newname=!filename:~0,-5!.crop.jpg
      echo REN "!oldname!" "!newname!"
      )
      echo Finished
      Pause

      Example output

      Code: [Select]
      C:\Batch\Test>test.bat
      REN "C:\Batch\Test\apicture.full.jpg" "apicture.crop.jpg"
      REN "C:\Batch\Test\sub1\testpicture.full.jpg" "testpicture.crop.jpg"
      REN "C:\Batch\Test\sub1\sub2\anotherpicture.full.jpg" "anotherpicture.crop.jpg"
      Finished
      Press any key to continue . . .

      Mulsiphix

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Re: Trouble With A Recursive Rename Batch File
        « Reply #4 on: April 06, 2016, 03:48:53 PM »
        Quote from: Error Returned
        File Not Found
        Finished
        Press any key to continue . . .

        This looks awesome. I had no idea you could write scripts like this for the Command Line. Sadly, it isn't working yet. I have a test directory structure set up like this:
        • Grandma Photos (root dir)
        • Image0.crop.jpg
          • Grandma Trip 1 (1st Level sub dir)
            • Image1.crop.jpg
            • Image2.crop.jpg
          • Grandma Trip 2 (1st Level sub dir)
            • Image3.crop.jpg
            • Image4.crop.jpg
        Folders = Red
        Files = Blue

        Salmon Trout

        • Guest
        Re: Trouble With A Recursive Rename Batch File
        « Reply #5 on: April 07, 2016, 12:48:49 AM »
        Interchange crop and full in my script

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Trouble With A Recursive Rename Batch File
        « Reply #6 on: April 07, 2016, 09:45:46 AM »

        Mulsiphix

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Re: Trouble With A Recursive Rename Batch File
          « Reply #7 on: April 07, 2016, 12:15:07 PM »
          Also posted here.
          http://www.dostips.com/forum/viewtopic.php?f=3&t=7066
          I received a workable solution at this link the other day. Then I checked my email, saw this response, and it seemed impossible. My understanding of the command line is that you can do single line commands. This one is written more like code form a programming language. I was extremely intrigued and wanted to understand how this worked. When it didn't work I decided to follow up with it rather than announce I no longer needed it. I apologize if my actions have upset anyone :(.

          Interchange crop and full in my script
          While the script runs all the way through, none of the actual file names are changed :-\. This is the output:


          Salmon Trout

          • Guest
          Re: Trouble With A Recursive Rename Batch File
          « Reply #8 on: April 07, 2016, 12:38:19 PM »
          I used a common testing safeguard, namely to preface the rename command with 'echo' so that you can first verify the renames are correct. I showed you how to deal with this, when I previously posted this above:

          Quote
          if it shows the right renames
          change this line:
          echo REN "!oldname!" "!newname!"
          to this:
          REN "!oldname!" "!newname!"

          Salmon Trout

          • Guest
          Re: Trouble With A Recursive Rename Batch File
          « Reply #9 on: April 07, 2016, 12:43:14 PM »
          My understanding of the command line is that you can do single line commands. This one is written more like code form a programming language.
          At the command prompt you can only enter single line commands but you can combine them in a 'batch script'. The language is called Windows command language, or Windows batch language. You can find numerous reference guides to the syntax online.

          Mulsiphix

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Re: Trouble With A Recursive Rename Batch File
            « Reply #10 on: April 07, 2016, 01:20:28 PM »
            I used a common testing safeguard, namely to preface the rename command with 'echo' so that you can first verify the renames are correct. I showed you how to deal with this, when I previously posted this above:
            I mean none of the actual file names on the hard drive are changed. The batch still isn't affecting the files. I made the change you requested previously. Just to be certain, this is what I'm running:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            for /f %%A in ('dir /s /b *.crop.jpg') do (
            set folder=%%~dpA
            set filename=%%~nA
            set oldname=!folder!%%~nA.jpg
            set newname=!filename:~0,-5!.full.jpg
            echo REN "!oldname!" "!newname!"
            )
            echo Finished
            Pause

            At the command prompt you can only enter single line commands but you can combine them in a 'batch script'. The language is called Windows command language, or Windows batch language. You can find numerous reference guides to the syntax online.
            That is awesome. I had no idea. Considering it's utility, I'm definitely going to make an effort. I recently took the time to really try and learn AutoHotKey. It has changed my life, but there are some things (like recursive renaming) that are overly complicated without a higher degree of programming knowledge and prowess. So this is welcome news. Thank you for cluing me in  ;D

            Salmon Trout

            • Guest
            Re: Trouble With A Recursive Rename Batch File
            « Reply #11 on: April 07, 2016, 01:26:33 PM »
            Quote
            Just to be certain, this is what I'm running:
            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            for /f %%A in ('dir /s /b *.crop.jpg') do (
            set folder=%%~dpA
            set filename=%%~nA
            set oldname=!folder!%%~nA.jpg
            set newname=!filename:~0,-5!.full.jpg
            echo REN "!oldname!" "!newname!"
            )
            echo Finished
            Pause

            Can you see the big red echo? You need to delete this, as I said above (twice).

            @echo off
            setlocal enabledelayedexpansion
            for /f %%A in ('dir /s /b *.crop.jpg') do (
               set folder=%%~dpA
               set filename=%%~nA
               set oldname=!folder!%%~nA.jpg
               set newname=!filename:~0,-5!.full.jpg
               echo REN "!oldname!" "!newname!"
               )
            echo Finished
            Pause
            « Last Edit: April 07, 2016, 01:42:32 PM by Salmon Trout »

            Mulsiphix

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              Re: Trouble With A Recursive Rename Batch File
              « Reply #12 on: April 07, 2016, 01:47:17 PM »
              Egg on my face :'(. I thought your reference to echo was explaining the output I saw in the CMD window. I had no idea it was actually interfering with the operation of the batch. I have made the change and the script is now returning errors.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              for /f %%A in ('dir /s /b *.crop.jpg') do (
              set folder=%%~dpA
              set filename=%%~nA
              set oldname=!folder!%%~nA.jpg
              set newname=!filename:~0,-5!.full.jpg
              REN "!oldname!" "!newname!"
              )
              echo Finished
              Pause



              I'm messing with the script and I'm getting now here simply fiddling with various parts of it. So I used some echo's and figured out the variables aren't holding the correct information. No clue how to fix that, but its a start. If you'd like to just pass on this, given I have a workable solution and I very well may just be aggravating you at this point, I completely understand. Thank you so much for your help thus far. I appreciate you taking a moment to help me learn ;).

              You'll notice "-=-=" appears in the Filename variable below. While the batch is in the root of the Grandma Photos directory, Grandma Photos itself is within a folder named "-=-= test dir 2 =-=-". Seems like Filename is somehow pulling a portion of that directory name instead.

              « Last Edit: April 07, 2016, 02:02:14 PM by Mulsiphix »

              Salmon Trout

              • Guest
              Re: Trouble With A Recursive Rename Batch File
              « Reply #13 on: April 07, 2016, 01:51:37 PM »
              what do you see if you open a command prompt in the folder where the batch is located, and type (or paste) this at the prompt and hit ENTER?

              dir /s /b *.crop.jpg



              Mulsiphix

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                Re: Trouble With A Recursive Rename Batch File
                « Reply #14 on: April 07, 2016, 02:11:38 PM »
                what do you see if you open a command prompt in the folder where the batch is located, and type (or paste) this at the prompt and hit ENTER?

                dir /s /b *.crop.jpg



                Also, I tried changing the test dir's name to one with no spaces and ran the echo modified script again. This returned the following: