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

Author Topic: Code - remove part of file name  (Read 38301 times)

0 Members and 1 Guest are viewing this topic.

Dias de verano

  • Guest
Re: Code - remove part of file name
« Reply #15 on: January 16, 2009, 01:17:36 PM »
It worked!

Wicked!  :) :) :) :) :) :)

Quote
Your a genius! :P

Yup.

Quote
And it's called football not soccer :P I'm from England ^_^

So am I. It's just that there are so many Seppos on here, I thought I had to use a word they'd understand.

Glad I could give you a hand, shandy  :)

Shandy

    Topic Starter


    Intermediate
  • Thanked: 7
    • Experience: Beginner
    • OS: Unknown
    Re: Code - remove part of file name
    « Reply #16 on: January 16, 2009, 01:18:59 PM »
     :o :o :o :o What a guy! hehe

    Dias de verano

    • Guest
    Re: Code - remove part of file name
    « Reply #17 on: January 16, 2009, 01:22:06 PM »
    So is the /1 syntax to ignore letter case?? :) :S

    It's not a figure 1, it's a small L, although a capital L works just as well. It forces the output of DIR to be in all lower case, whatever the case of the letters in the filenames.


    Shandy

      Topic Starter


      Intermediate
    • Thanked: 7
      • Experience: Beginner
      • OS: Unknown
      Re: Code - remove part of file name
      « Reply #18 on: January 16, 2009, 01:51:24 PM »
      I see, so was it there to convert everything to lowercase so when its looking for that string the case of the letters won't be an issue?
      I take it that it didn't work before because it was looking for [www.lokotorrents.com] in lower case and couldn't find that exact string? would it have worked if we changed set deletestring=[www.lokotorrents.com] to set deletestring=[WwW.LoKoTorrents.CoM] ?

      Dias de verano

      • Guest
      Re: Code - remove part of file name
      « Reply #19 on: January 16, 2009, 02:14:17 PM »
      I see, so was it there to convert everything to lowercase so when its looking for that string the case of the letters won't be an issue?

      Yes, that's right.

      Quote
      I take it that it didn't work before because it was looking for [www.lokotorrents.com] in lower case and couldn't find that exact string?

      Yes, that's right too. The new filename was produced from the old one by using the string replace feature of the SET command

      set newstring=%oldstring:A=B% where A is the string you are looking for and B is the string you want to replace it with. If B is absent it replaces A with nothing, i.e. removes it. It is case sensitive. For example

      set string=Mary had a little lamb
      set string=%string:Mary=John%
      string becomes John had a little lamb
      set string=%string:little =%          (include the space after little!)
      string becomes John had a lamb

      Quote
      would it have worked if we changed set deletestring=[www.lokotorrents.com] to set deletestring=[WwW.LoKoTorrents.CoM] ?

      Yes, that's the other way we could have solved the problem.


      Shandy

        Topic Starter


        Intermediate
      • Thanked: 7
        • Experience: Beginner
        • OS: Unknown
        Re: Code - remove part of file name
        « Reply #20 on: January 16, 2009, 02:35:04 PM »
        I see how it works now, thanks a lot, this code will be useful! :)

        sammmurai

        • Guest
        Re: Code - remove part of file name
        « Reply #21 on: May 25, 2010, 07:11:04 AM »
        Code: [Select]

        @echo off
        setlocal enabledelayedexpansion
        set deletestring=[www.lokotorrents.com]
        echo Ready to start
        echo.
        pause
        echo.
        for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
        set oldfilename=%%F
        set newfilename=!oldfilename:%deletestring%=!
        echo Ren "!oldfilename!" "!newfilename!"
        )
        echo.
        echo All done
        pause


        Copy the above into your editor e.g. Notepad. Save as a .bat or .cmd file in the folder where the files are. Run it. See the results. If you are happy with the way it offers to rename the files, remove 'echo' from the beginning of the line starting 'echo Ren' & save and run it for real.

        Maybe backup the files first in case it all goes wrong?

        Or change Ren to Copy?

        Your choice.

        I expect you can see where to edit so it will remove other strings...



        You're the MAN!

        AWESOME CODE. Just what i needed...

        Thanx! Gracias!


        Salmon Trout

        • Guest
        Re: Code - remove part of file name
        « Reply #22 on: June 01, 2010, 01:36:21 PM »
        I miss Dias de verano...

        treasuregetter



          Newbie

          Re: Code - remove part of file name
          « Reply #23 on: July 13, 2010, 10:40:38 PM »
          Hello. I can only hope you still get this considering the age of this discussion. This script works wonderfully except it is only written to handle one folder/directory of files. Is there a way to run recursively on all subfolders of a primary folder? I'd really appreciate any help you can provide! I have to remove two characters,  "-2" from approximately 35,000 files.

          Salmon Trout

          • Guest
          Re: Code - remove part of file name
          « Reply #24 on: July 14, 2010, 12:08:05 AM »
          try this

          @echo off
          setlocal enabledelayedexpansion
          set deletestring=-2
          echo Ready to start
          echo.
          pause
          echo.
          for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
                  set oldfilename=%%~nxF
                  set pathname=%%~dpF

                  set newfilename=!oldfilename:%deletestring%=!
                  echo Ren "!pathname!!oldfilename!" "!newfilename!"
                  )
          echo.
          echo All done
          pause
          (

          treasuregetter



            Newbie

            Re: Code - remove part of file name
            « Reply #25 on: July 17, 2010, 02:32:59 AM »
            The code provided was very much appreciated. Unfortunately I am feeling slightly murderous at the moment. This worked very well but I encountered a large number (in the hundreds) of folders with duplicate files in them. I need this batch file to check the folders for a existing file first before renaming the "####_2.xxx" file to remove the _2. This will ensure removal of the old file first. Any more help you can provide would be greatly appreciated!

            My best attempts at the code:

            #1)

            @echo off
            setlocal enabledelayedexpansion
            set deletestring=_2
            echo Ready to start
            echo.
            pause
            echo.


            goto findbadfile

            :findbadfile
            for /f "delims==" %%F in ('dir /b /s /a-d /l *.mp3 ^| find "%deletestring%"') do (
               set oldfilename=%%~nxF
               set pathname=%%~dpF
               set newfilename=!oldfilename:%deletestring%=!
               if exist "!pathname!!newfilename!" (goto deletefirst) else (goto renamer)

               echo.
            echo All done
            pause
            )

            :deletefirst
               echo Found existing duplicate file !pathname!!newfilename! that should be removed first...   
               echo Deleting !pathname!!newfilename!
               del -f "!pathname!!newfilename!"
               goto renamer



            :renamer
            echo.
               echo Renaming....
               echo !pathname!!oldfilename!   to
               echo !pathname!!newfilename!....
               Ren "!pathname!!oldfilename!" "!newfilename!"
            rem   echo Press a key to continue or Ctrl-C to stop
               
               goto findbadfile
               
            #2)

            @echo off
            setlocal enabledelayedexpansion
            set deletestring=_2
            echo Ready to start
            echo.
            pause
            echo.


            goto findbadfile

            :findbadfile
            for /f "delims==" %%F in ('dir /b /s /a-d /l *.mp3 ^| find "%deletestring%"') do (
               set oldfilename=%%~nxF
               set pathname=%%~dpF
               set newfilename=!oldfilename:%deletestring%=!
               if exist "!pathname!!newfilename!" (goto deletefirst) else (goto renamer)
            rem   Ren "!pathname!!oldfilename!" "!newfilename!"
               echo.)
            echo All done
            pause


            :deletefirst
               echo Found existing duplicate file !pathname!!newfilename! that should be removed first...   
               echo Deleting !pathname!!newfilename!
            rem   del -f "!pathname!!newfilename!"
            rem   Ren "!pathname!!oldfilename!" "!newfilename!"
            rem   goto renamer



            :renamer
            echo.
               echo Renaming....
               echo !pathname!!oldfilename!   to
               echo !pathname!!newfilename!....
            rem   Ren "!pathname!!oldfilename!" "!newfilename!"
               echo Press a key to continue or Ctrl-C to stop
               
            rem   goto findbadfile
               

            I have made multiple attempts but to no avail...it either does nothing or ends up deleting files that it shouldn't. I guess I don't understand how the FOR-DO loops work when combined with an IF-THEN-ELSE very well.

            Thanks in advance!

            Salmon Trout

            • Guest
            Re: Code - remove part of file name
            « Reply #26 on: July 17, 2010, 02:40:10 AM »
            Do you mean that the pre-existing duplicate filename is found in the same folder as the file with -2 in its name which you want to rename, and that you wish to delete this pre-existing file first?

            If that is the case, try this


            @echo off
            setlocal enabledelayedexpansion
            set deletestring=-2
            echo Ready to start
            echo.
            pause
            echo.
            for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
                    set oldfilename=%%~nxF
                    set pathname=%%~dpF
                    set newfilename=!oldfilename:%deletestring%=!
                    if exist "!pathname!!newfilename!" (
                        echo Filename conflict
                        echo Folder     : !pathname!
                        echo Filename : !newfilename!
                        echo Deleting file
                        Del "!pathname!!newfilename!"
                        )
                    Ren "!pathname!!oldfilename!" "!newfilename!"
                    )
            echo.
            echo All done
            pause



            Salmon Trout

            • Guest
            Re: Code - remove part of file name
            « Reply #27 on: July 17, 2010, 05:01:06 AM »
            Or this

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set deletestring=-2
            echo Ready to start
            echo.
            pause
            echo.
            for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
                    set oldfilename=%%~nxF
                    set pathname=%%~dpF
                    set newfilename=!oldfilename:%deletestring%=!
                    if exist "!pathname!!newfilename!" (
                        echo Filename conflict
                        echo Folder            : !pathname!
                        echo Present  filename : !oldfilename!
                        echo Proposed filename : !newfilename!
                        echo This file exists.
                        echo Deleting file
                        Del "!pathname!!newfilename!"
                        )
                    Ren "!pathname!!oldfilename!" "!newfilename!"
                    )
            echo.
            echo All done
            pause