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 38306 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