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

Author Topic: Rename/replace  (Read 32924 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: Rename/replace
« Reply #45 on: January 04, 2010, 10:50:10 AM »

To further my understanding, what does the switch b do (/b)?

open a command window, and type

dir (ENTER)

and then

dir /b (ENTER) and you'll see the difference.

Type dir /? to see an explanation.

Quote
Now, just to be picky, I'd love to have certain [first] letters capitalized.

This is really beyond the capabilities of a batch file, you should be looking at a bulk file rename utility such as Bulk Rename Utility

http://www.bulkrenameutility.co.uk/Main_Intro.php




Salmon Trout

  • Guest
Re: Rename/replace
« Reply #46 on: January 04, 2010, 01:06:23 PM »
I never could resist a challenge...


Code: [Select]

@echo off
echo Ready to rename files
echo.
pause
echo.
echo Renaming files...
echo.
setlocal enabledelayedexpansion
for /f "delims=" %%A in ( 'dir /b *.txt' ) do (
    set oldname=%%A
    set newname=!oldname:_= !
    set char1=!newname:~0,1!
    set rchars=!newname:~1!
    if "!char1!"=="a" set char1=A
    if "!char1!"=="b" set char1=B
    if "!char1!"=="c" set char1=C
    if "!char1!"=="d" set char1=D
    if "!char1!"=="e" set char1=E
    if "!char1!"=="f" set char1=F
    if "!char1!"=="g" set char1=G
    if "!char1!"=="h" set char1=H
    if "!char1!"=="i" set char1=I
    if "!char1!"=="j" set char1=J
    if "!char1!"=="k" set char1=K
    if "!char1!"=="l" set char1=L
    if "!char1!"=="m" set char1=M
    if "!char1!"=="n" set char1=N
    if "!char1!"=="o" set char1=O
    if "!char1!"=="p" set char1=P
    if "!char1!"=="q" set char1=Q
    if "!char1!"=="r" set char1=R
    if "!char1!"=="s" set char1=S
    if "!char1!"=="t" set char1=T
    if "!char1!"=="u" set char1=U
    if "!char1!"=="v" set char1=V
    if "!char1!"=="w" set char1=W
    if "!char1!"=="x" set char1=X
    if "!char1!"=="y" set char1=Y
    if "!char1!"=="z" set char1=Z
    set newname=!char1!!rchars!
    echo Renaming !oldname! to !newname!
    ren "!oldname!" "!newname!"
    )
echo.
echo All done
echo.
pause
   

ghostdog74



    Specialist

    Thanked: 27
    Re: Rename/replace
    « Reply #47 on: January 04, 2010, 05:52:19 PM »
    luckily the english alphabets only have 26 characters :)

    Salmon Trout

    • Guest
    Re: Rename/replace
    « Reply #48 on: January 05, 2010, 12:24:54 AM »
    luckily the english alphabets only have 26 characters :)

    Indeed!

    one stupid guy

      Topic Starter


      Greenhorn

      Re: Rename/replace
      « Reply #49 on: January 05, 2010, 10:51:18 AM »

      I tried it with my example, and couldn't get it to work. It still deals with the underscore properly, and most of the coding made some sense to me (the IF statemetns are intuitive).

      My example was:

      If the files are currently as below:
      file 1 - test one.txt
      File 2 - test two.txt
      file 3 - test three.txt

      I would like:
      File 1 - Test one.txt
      File 1 - Test two.txt
      File 1 - Test three.txt

      And I don't get the results I wanted.


      So that I 'get it', can you help me understand what these two lines mean:

          set char1=!newname:~0,1!
          set rchars=!newname:~1!

      Specifically, what does the squiggle mean (~), and what does it mean to have the zero and one separated by a comma (~0,1)?
      For that matter, what does "rchars" mean? I looked on the ComputerHoper glossary page and couldn't find it.

      Thank you again,

      Salmon Trout

      • Guest
      Re: Rename/replace
      « Reply #50 on: January 05, 2010, 11:20:02 AM »
      Well, I named some files according to your description and got these results...

      Before

      Code: [Select]
      S:\>dir /b *.txt
      test_file_002.txt
      test_file_003.txt
      test_file_001.txt

      Run batch

      Code: [Select]
      Ready to rename files

      Press any key to continue . . .

      Renaming files...

      Renaming test_file_002.txt to Test file 002.txt
      Renaming test_file_003.txt to Test file 003.txt
      Renaming test_file_001.txt to Test file 001.txt

      All done

      Press any key to continue . . .

      After

      Code: [Select]
      S:\>dir /b *.txt
      Test file 002.txt
      Test file 003.txt
      Test file 001.txt

      Quote
      If the files are currently as below:
      file 1 - test one.txt
      File 2 - test two.txt
      file 3 - test three.txt

      I would like:
      File 1 - Test one.txt
      File 1 - Test two.txt
      File 1 - Test three.txt

      What's this "File 1" prefix? You never mentioned that before.

      Quote
      set char1=!newname:~0,1!

      create a variable called char1 and make it equal to the first character (0 from the start) of newname

      Quote
      set rchars=!newname:~1!

      create a variable called rchars and make it equal to the characters from the 2nd (1 from the start) to the end of newname

      A colon and "squiggle" is used in string slicing in batch language

      !apple:~0,3! means the first 3 characters of the variable named apple

      rchars is just a name I made up for the variable.

      « Last Edit: January 05, 2010, 11:45:18 AM by Salmon Trout »

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Rename/replace
      « Reply #51 on: January 05, 2010, 12:39:06 PM »
      Salmon Trout
      It would;d be helpful is somebody would document where you find the definition for the batch file mid  string extraction function.
      If you try to Google "DOS :~" you won't find it. Instead you are lead to a site about a beer called Dos Equis.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Rename/replace
      « Reply #52 on: January 05, 2010, 12:43:27 PM »
      use "cmd string manipulation" as a search term. seems to bring up relevant results.
      I was trying to dereference Null Pointers before it was cool.

      Salmon Trout

      • Guest
      Re: Rename/replace
      « Reply #53 on: January 05, 2010, 12:57:43 PM »
      It is available from the command prompt by typing SET /? - it's immediately after the section explaining SET /P, but if folks prefer to read it in a browser, this is as good a place as any...

      http://www.computerhope.com/sethlp.htm

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Rename/replace
      « Reply #54 on: January 06, 2010, 09:50:14 AM »
      Thank you both.
      I have been using DOS for years and never saw that documented.
      Just did not know where to find it.
      The XX Beer commercial got me side tracked.  :D

      the_mad_joker



        Apprentice

        Thanked: 8
        Re: Rename/replace
        « Reply #55 on: January 07, 2010, 08:02:35 AM »
        I think It shld a bit long way
        correct me if i am wrong

        First make list of files by using :

        1} dir PLACE UR DIRECTORY HERE\*.txt /b >list.txt

        2}edit list.txt with notepad

        3}replace "_" with "space" [without quotes]

        4}add "ren" be4 every file's name and then the file

        for ex
        ren the_example.txt the example

        save it as BATCH and apply it

         

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Rename/replace
        « Reply #56 on: January 07, 2010, 12:44:41 PM »
        the_mad_joker,
        I like your method.
        If you don't know the academic answer...
        just use the pragmatic solution.

        BillRichardson



          Intermediate

          Thanked: 15
          Re: Rename/replace
          « Reply #57 on: January 07, 2010, 03:50:15 PM »
          the_mad_joker,
          I like your method.
          If you don't know the academic answer...
          just use the pragmatic solution.


          Sounds like a manual method.  Use Notepad?  This guy is a mad joker.

          ST and Helpmeh used an automatic one liner.
          Bill Richardson

          Helpmeh



            Guru

          • Roar.
          • Thanked: 123
            • Yes
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 8
          Re: Rename/replace
          « Reply #58 on: January 07, 2010, 03:59:49 PM »
          I think It shld a bit long way
          correct me if i am wrong

          First make list of files by using :

          1} dir PLACE UR DIRECTORY HERE\*.txt /b >list.txt

          2}edit list.txt with notepad

          3}replace "_" with "space" [without quotes]

          4}add "ren" be4 every file's name and then the file

          for ex
          ren the_example.txt the example

          save it as BATCH and apply it

           
          do you know how long that will take? Try doing it with hundreds of files. What about thousands?
          Where's MagicSpeed?
          Quote from: 'matt'
          He's playing a game called IRL. Great graphics, *censored* gameplay.

          Salmon Trout

          • Guest
          Re: Rename/replace
          « Reply #59 on: January 08, 2010, 12:10:47 AM »
          Quote from: Billy Boy
          do you know how long that will take? Try doing it with hundreds of files. What about thousands?

          Many editors allow you to do search and replace. If you have column select, copy and paste then the above would take a few seconds.