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

Author Topic: Rename/replace  (Read 32486 times)

0 Members and 1 Guest are viewing this topic.

one stupid guy

    Topic Starter


    Greenhorn

    Rename/replace
    « on: December 30, 2009, 12:41:46 PM »
    I can't figure it out...

    I'd like to rename a number of files, replacing underscores with a space.
    For example, I'd like to rename "the_first_file.txt"  to "the first file.txt"
     
    I thought the following would work, but it didn't:

    RENAME *_*>* *" "*.*


    Can anyone help a brother out?


    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Rename/replace
    « Reply #1 on: December 30, 2009, 01:25:18 PM »
    This might work...

    @echo off
    setlocal enabledelayedexpansion
    for /f "delims=" %%a in ('dir /b') do (
    set fileorig=%%a
    set filenew=!fileorig:_= !
    ren !fileorig! "!filenew!"
    )
    Save it in the folder which contains the files you want to change.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    Salmon Trout

    • Guest
    Re: Rename/replace
    « Reply #2 on: December 30, 2009, 01:27:20 PM »
    Code: [Select]
    setlocal enabledelayedexpansion
    for /f "delims=" %%A in (*.txt) do (
        set oldname=%%A
        set newname=!oldname:_= !
        ren "!oldname!" "!newname!"
        )

    Salmon Trout

    • Guest
    Re: Rename/replace
    « Reply #3 on: December 30, 2009, 01:27:50 PM »
    Great minds think alike

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Rename/replace
    « Reply #4 on: December 30, 2009, 01:28:46 PM »
    Why thank you ST, strangest thing, I was just about to say that too.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    BillRichardson



      Intermediate

      Thanked: 15
      Re: Rename/replace
      « Reply #5 on: December 30, 2009, 01:34:21 PM »

      C:\>type  under2.bat

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      dir /b the*.txt  >  _txtfiles.txt


      for /f  "delims=" %%i in (_txtfiles.txt)  do (
      echo  %%i

      set var=%%i

      For /F "delims=" %%a in ('echo %%i ^| sed 's/_/ /g') do set var3=%%a

      echo var =!var!

      echo var3 =!var3!



      ren  "!var!" "!var3!"

      rem ren   "!var3!" "!var!"

      )


      Output:

      C:\> under2.bat
      the_first_file.txt
      the_fourth_file.txt
      the_second_file.txt
      the_third_file.txt
       the_first_file.txt
      var =the_first_file.txt
      var3 =the first file.txt
       the_second_file.txt
      var =the_second_file.txt
      var3 =the second file.txt
       the_third_file.txt
      var =the_third_file.txt
      var3 =the third file.txt
       the_fourth_file.txt
      var =the_fourth_file.txt
      var3 =the fourth file.txt
      C:\>
      « Last Edit: December 31, 2009, 01:35:03 PM by BillRichardson »
      Bill Richardson

      Salmon Trout

      • Guest
      Re: Rename/replace
      « Reply #6 on: December 30, 2009, 01:55:09 PM »
      Oh God! he's found about sed now! I shudder to think what's next...

      OP: Dear Mr bill... I tried running your script and it said "'sed' is not recognized as an internal or external command,
      operable program or batch file."

      BillRichardson



        Intermediate

        Thanked: 15
        Re: Rename/replace
        « Reply #7 on: December 30, 2009, 02:06:39 PM »
        "'sed' is not recognized as an internal or external command,
        operable program or batch file."


        http://www.computerhope.com/unix/used.htm

        ghostdog74 is the expert with sed and awk (gawk)

        I'm sure ghostdog74  will help you.
        Bill Richardson

        Salmon Trout

        • Guest
        Re: Rename/replace
        « Reply #8 on: December 30, 2009, 02:15:56 PM »
        Suppose they don't want to use your weird sounding program?

        BillRichardson



          Intermediate

          Thanked: 15
          Re: Rename/replace
          « Reply #9 on: December 30, 2009, 03:53:52 PM »
          Suppose they don't want to use your weird sounding program?

          http://www.computerhope.com/unix/used.htm

          Sed has been used many times on the Hope Board.

          Sed is a quick and easy stream editor

          Speak with Ghostdog74.
          Bill Richardson

          ghostdog74



            Specialist

            Thanked: 27
            Re: Rename/replace
            « Reply #10 on: December 30, 2009, 05:30:23 PM »
            I can't figure it out...

            I'd like to rename a number of files, replacing underscores with a space.
            For example, I'd like to rename "the_first_file.txt"  to "the first file.txt"
             
            I thought the following would work, but it didn't:

            RENAME *_*>* *" "*.*


            Can anyone help a brother out?


            if you have many underscores, eg file_____test.txt, do you want to replace them with just 1 space as well?

            one stupid guy

              Topic Starter


              Greenhorn

              Re: Rename/replace
              « Reply #11 on: December 31, 2009, 08:40:48 AM »
              I better fess-up on my newbieness; I know less than you think.
              For the first proposed solution above (thanks to Helpmeh and Salmon Trout), do I just type it into a DOS emulator, or is it more involved?
              Keep in mind that I want this to go through a few hundred files and make this change to them all (swap an underscore for a space).

              Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

              I tried to identify the elements of the above solution so as to educate myself.
              Below is a line-by-line explanation. Is my understanding correct?


              setlocal enabledelayedexpansion - the activity of the command will be kept to a specified folder

              for /f "delims=" %%A in (*.txt) do - 'for' is like an "IF" command, but I don't know what /f does.
                                                 - I don't get the "delims=" either.
                                                 - Are the percentage signs are wildcards?
                                                 - I know that *.txt means 'all text files'

                  set oldname=%%A         - does this specify the format of the old name?

                 set newname=!oldname:_= !  - this makes sense (I think), it changes the underscore to a space

                  ren "!oldname!" "!newname!"  - this renames the file, with the new (underscoreless) name


              Thank you for your insight and patience,

              Helpmeh



                Guru

              • Roar.
              • Thanked: 123
                • Yes
                • Yes
              • Computer: Specs
              • Experience: Familiar
              • OS: Windows 8
              Re: Rename/replace
              « Reply #12 on: December 31, 2009, 09:15:21 AM »
              I better fess-up on my newbieness; I know less than you think.
              For the first proposed solution above (thanks to Helpmeh and Salmon Trout), do I just type it into a DOS emulator, or is it more involved?
              Keep in mind that I want this to go through a few hundred files and make this change to them all (swap an underscore for a space).

              Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

              I tried to identify the elements of the above solution so as to educate myself.
              Below is a line-by-line explanation. Is my understanding correct?


              setlocal enabledelayedexpansion - the activity of the command will be kept to a specified folder

              for /f "delims=" %%A in (*.txt) do - 'for' is like an "IF" command, but I don't know what /f does.
                                                 - I don't get the "delims=" either.
                                                 - Are the percentage signs are wildcards?
                                                 - I know that *.txt means 'all text files'

                  set oldname=%%A         - does this specify the format of the old name?

                 set newname=!oldname:_= !  - this makes sense (I think), it changes the underscore to a space

                  ren "!oldname!" "!newname!"  - this renames the file, with the new (underscoreless) name


              Thank you for your insight and patience,
              Honestly, you understood very well for a beginner. I'll just confirm and fix what you said about the script:
              Setlocal enabledelayedexpansion I'm not exactly sure what this means, but regular variables in for commands are wrapped in ! and not %.
              "Delims=" That means that there are no delimiters (things that seperate the information) to be used.
              %%a (or %%A) That is a FOR variable. It is only used in the FOR command. For every line of data, it will become that. So if a line was file_name.txt, then %%a would become file_name.txt .
              in (*.txt) That is where the data comes from. For each file, there is a new line. * is a wildcard. If you knew how long the desired file is, you can use a certain amount of ? (question marks are a 1-character wildcard).
              Set oldfilename=%%a Doing that allows me to work with the name.
              Set newfilename=!oldfilename:_= ! That will make newfilename the same as oldfilename, but every _ becomes a space.
              Ren !oldfilename! !newfilename! Pretty explanitory.

              Understand a little better now?
              Where's MagicSpeed?
              Quote from: 'matt'
              He's playing a game called IRL. Great graphics, *censored* gameplay.

              Salmon Trout

              • Guest
              Re: Rename/replace
              « Reply #13 on: December 31, 2009, 09:27:20 AM »
              That's about it, helpmeh, but if I can just jump in with the FOR line...

              for /f "delims=" %%A in (*.txt) do

              The FOR command works like this

              FOR [switch] [option block] %%Variable in (dataset) do (something)

              You can type FOR /? at the prompt to get several screens full of detailed help but I will briefly break down what that line does

              /f ... switch that tells FOR that (dataset) is to be treated as a series of lines to be processed. In this case, dataset is *.txt which means "one by one, each file with the .txt extension in the current directory".

              "delims=" ... option block that tells FOR that each line of output is to be taken whole and not split into "tokens"

              %%Variable ... A loop variable is a single letter, a-a or A-Z and the contents of (dataset) are assigned one by one to this variable, in this case %%A will contain, one after the other, the name of each .txt file.


              ghostdog74



                Specialist

                Thanked: 27
                Re: Rename/replace
                « Reply #14 on: December 31, 2009, 09:39:04 AM »
                Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.

                You can use vbscript. this replaces multiple _ (and consecutive _ ) with single space.
                Code: [Select]
                Set objFS = CreateObject("Scripting.FileSystemObject")
                Set regEx = New RegExp            ' Create regular expression.
                regEx.Global=True
                strFolder = WScript.Arguments.Item(0)
                strOld = WScript.Arguments.Item(1)
                strNew = WScript.Arguments.Item(2)
                regEx.Pattern = strOld&"+"
                Set objFolder = objFS.GetFolder(strFolder)
                For Each strFile In objFolder.Files
                strFileName = strFile.Name
                If objFS.GetExtensionName(strFileName) = "txt" And InStr(strFileName,strOld)>0 Then
                strFileName = regEx.Replace(strFileName, strNew)
                strFile.Name = strFileName
                End If
                Next


                save as replace.vbs and on command line
                Code: [Select]
                c:\test> cscript //nologo replace.vbs c:\test _ " "