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 38308 times)

0 Members and 1 Guest are viewing this topic.

Shandy

    Topic Starter


    Intermediate
  • Thanked: 7
    • Experience: Beginner
    • OS: Unknown
    Code - remove part of file name
    « on: January 13, 2009, 01:25:33 PM »
    Hi can someone create some code for me please to remove [www.lokotorrents.com] from the file name of a whole bunch of files in the same directory in one command line please? 

    example:
    C:\Documents and Settings\Administrator\My Documents\Downloads\filea[www.lokotorrents.com].nul
     and
    C:\Documents and Settings\Administrator\My Documents\Downloads\fileb[www.lokotorrents.com].nul

    to remove [www.lokotorrents.com] from all the files in one go :D thanks if you can help :P

    Dias de verano

    • Guest
    Re: Code - remove part of file name
    « Reply #1 on: January 13, 2009, 02:08:40 PM »
    Why has it got to be in one line?

    Shandy

      Topic Starter


      Intermediate
    • Thanked: 7
      • Experience: Beginner
      • OS: Unknown
      Re: Code - remove part of file name
      « Reply #2 on: January 13, 2009, 02:50:36 PM »
      okay, it can be on multiple lines in which case can you create a batch file for it to run from? :P noticing a pattern here? I'm lazy! :D
      I'm sure it could probably be done in one line anyways using a wild card. Basically I want to remove that part of the file name from 100's of files but not using lame-*censored* GUI windows which will take me hours. First person to give me the code wins a speed-boat.

      Dias de verano

      • Guest
      Re: Code - remove part of file name
      « Reply #3 on: January 13, 2009, 03:18:40 PM »
      Quote
      [www.lokotorrents.com]

      Is this correct, that the unwanted section starts with a [ and ends with a ] ... ?


      Shandy

        Topic Starter


        Intermediate
      • Thanked: 7
        • Experience: Beginner
        • OS: Unknown
        Re: Code - remove part of file name
        « Reply #4 on: January 13, 2009, 03:33:55 PM »
        Yuuuup! you can do it?? awesome.... oh there isn't actually a speed-boat by the way... :( sorry.   

        [www.lokotorrents.com]       <--- Remove all including '[' and ']' :D

        Dias de verano

        • Guest
        Re: Code - remove part of file name
        « Reply #5 on: January 13, 2009, 03:43:47 PM »
        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...


        Dias de verano

        • Guest
        Re: Code - remove part of file name
        « Reply #6 on: January 13, 2009, 03:47:14 PM »
        I'm sure it could probably be done in one line anyways using a wild card.

        I doubt it.

        Shandy

          Topic Starter


          Intermediate
        • Thanked: 7
          • Experience: Beginner
          • OS: Unknown
          Re: Code - remove part of file name
          « Reply #7 on: January 15, 2009, 08:09:16 AM »
          Awesome code  :o
          Didn't work though :/
          Here is what I ran as a .bat inside the folder:

          @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%=!
             Ren "!oldfilename!" "!newfilename!"
             )
          echo.
          echo All done
          pause

          Thanks anyway I'm still impressed :P  :o

          Dias de verano

          • Guest
          Re: Code - remove part of file name
          « Reply #8 on: January 15, 2009, 11:25:11 AM »
          Awesome code  :o
          Didn't work though :/

          Well, it worked for me on a folder full of test files named like you said.
          You don't actually say in what way it "didn't work" so I can't actually suggest what is wrong. Maybe the files are read only? Maybe you didn't describe what you wanted properly?

          Code: [Select]
          S:\Test\Batch>dir *.nul
           Volume in drive S is USBHD
           Volume Serial Number is 2C51-AA7F

           Directory of S:\Test\Batch

          13/01/2009  22:25                 0 Test-file02[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file03[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file04[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file05[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file06[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file07[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file08[www.lokotorrents.com].nul
          13/01/2009  22:25                 0 Test-file01[www.lokotorrents.com].nul
                         8 File(s)              0 bytes
                         0 Dir(s)  187,333,132,288 bytes free

          S:\Test\Batch>test8.bat
          S:\Test\Batch>dir *.nul
           Volume in drive S is USBHD
           Volume Serial Number is 2C51-AA7F

           Directory of S:\Test\Batch

          13/01/2009  22:25                 0 Test-file02.nul
          13/01/2009  22:25                 0 Test-file03.nul
          13/01/2009  22:25                 0 Test-file04.nul
          13/01/2009  22:25                 0 Test-file05.nul
          13/01/2009  22:25                 0 Test-file06.nul
          13/01/2009  22:25                 0 Test-file07.nul
          13/01/2009  22:25                 0 Test-file08.nul
          13/01/2009  22:25                 0 Test-file01.nul
                         8 File(s)              0 bytes
                         0 Dir(s)  187,333,132,288 bytes free

          S:\Test\Batch>



          Test8.bat

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




          Shandy

            Topic Starter


            Intermediate
          • Thanked: 7
            • Experience: Beginner
            • OS: Unknown
            Re: Code - remove part of file name
            « Reply #9 on: January 16, 2009, 11:44:30 AM »
            Does this code only work on files with the extension .nul? By the looks of it, it works on all file types. ::)

            Shandy

              Topic Starter


              Intermediate
            • Thanked: 7
              • Experience: Beginner
              • OS: Unknown
              Re: Code - remove part of file name
              « Reply #10 on: January 16, 2009, 11:49:32 AM »

              C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
              nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>setlocal enabledelayedexpansion

              C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
              nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>set deletestring=[www.lokotorrents.com]


              C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
              nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>for /F "delims==" %F in ('dir /b | find
              "[www.lokotorrents.com]"') do (
              set oldfilename=%F
               set newfilename=!oldfilename:[www.lokotorrents.com]=!
               Ren "!oldfilename!" "!newfilename!"
              )

              C:\Documents and Settings\Administrator\My Documents\Downloads\Hardcore Nation (2009) [WwW.LoKoTorre
              nts.CoM]\Hardcore Nation 2009 .[WwW.LoKoTorrents.CoM](CD-1)>pause
              Press any key to continue . . .


              That's what happened with the last code when i put @echo on at top and pause at the bottom.    ^_^

              Dias de verano

              • Guest
              Re: Code - remove part of file name
              « Reply #11 on: January 16, 2009, 12:39:03 PM »
              I have a feeling you just moved the goal posts, as we say in soccer countries.

              Is this the string you wish to find and remove?

              Quote
              [WwW.LoKoTorrents.CoM]

              Or is it this (as you originally stated)?

              Quote
              [www.lokotorrents.com]

              because there is a difference. As you should be able to see. Which might explain why my code "didn't work".

              And why, if I am right, you will need to change this line:

              Code: [Select]
              for /f "delims==" %%F in ('dir /b ^| find "%deletestring%"') do (
              to this:

              Code: [Select]
              for /f "delims==" %%F in ('dir /b /l ^| find "%deletestring%"') do (
              The /l switch for DIR forces the file listing to be all lower case.

              And the answer to this question

              Quote
              Does this code only work on files with the extension .nul? By the looks of it, it works on all file types.

              is: No, it doesn't only work on files with the extension .nul, and you are right, it works on all file types. If you wanted to restrict it to only certain file types, you could modify the FOR command line like this. Let's say you only want to rename .nul files...

              for /f "delims==" %%F in ('dir /b /l *.nul ^| find "%deletestring%"') do (



              « Last Edit: January 16, 2009, 01:07:24 PM by Dias de verano »

              Shandy

                Topic Starter


                Intermediate
              • Thanked: 7
                • Experience: Beginner
                • OS: Unknown
                Re: Code - remove part of file name
                « Reply #12 on: January 16, 2009, 01:11:48 PM »
                It's [WwW.LoKoTorrents.CoM] as you said, but I'll try that other line and see what happens :D this is fun! lol
                Thanks for the help  ;)

                Shandy

                  Topic Starter


                  Intermediate
                • Thanked: 7
                  • Experience: Beginner
                  • OS: Unknown
                  Re: Code - remove part of file name
                  « Reply #13 on: January 16, 2009, 01:13:46 PM »
                  It worked! Your a genius! :P
                  And it's called football not soccer :P I'm from England ^_^

                  Shandy

                    Topic Starter


                    Intermediate
                  • Thanked: 7
                    • Experience: Beginner
                    • OS: Unknown
                    Re: Code - remove part of file name
                    « Reply #14 on: January 16, 2009, 01:16:47 PM »
                    So is the /1 syntax to ignore letter case?? :) :S

                    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