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

Author Topic: Rename/replace  (Read 33860 times)

0 Members and 1 Guest are viewing this topic.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Rename/replace
« Reply #60 on: January 08, 2010, 11:39:50 AM »
Sounds like a manual method.  Use Notepad?  This guy is a mad joker.

ST and Helpmeh used an automatic one liner.
So how is an automatic one linerwritten without using a text editor?
Using the text editor to repeat and replace is using a program to automate a task.
Can you write a one liner that does not have to be tested more that once? If not, it is not really a one-liner. It took you two or more attempts to get it right. You had to write the line again and again.

Many power users prefer methods that others can easily understand and modify. Some one liners can do that very well. Some just confuse the average user.
A general rule is to have a solution that can be shown in about 25 lines of well arranged text. And inside normal margins.

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 #61 on: January 08, 2010, 11:46:40 AM »
um... geek... Notepad stays OPEN... none of it's actions are automated, you need to perform the replace from within the notepad GUI...
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Rename/replace
« Reply #62 on: January 08, 2010, 11:49:52 AM »
Quote
none of it's actions are automated,
My is, I click replace all. That's automation.
Look up automation. 8)

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 #63 on: January 08, 2010, 11:55:50 AM »
My is, I click replace all. That's automation.
Look up automation. 8)

it's only partial automation of the entire task; it replaces all in a single file; it doesn't scale.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Rename/replace
« Reply #64 on: January 08, 2010, 12:10:47 PM »
B C programmer,
  That's a good answer. Rather than pursue this any further, let me pick your brain for something I have thought about. It relates to the topic at hand.
In any situation like the one in this topic there are any number of scenarios where you might want to replace or change or rename a file or set of files in a group of directories. Perhaps some users have a need to do this on a regular basis. Now is there a way of making an automated program that will help the user do this without the need for the user to have to rewrite and edit a batch file.? In other words, a script that would be fully automated from the viewpoint of the end-user. To that end, I was wondering if there is a way to invoke notepad and make it do search and replace from a script? If not, would a person have to use one of the GNU utilities such as SED?

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 #65 on: January 08, 2010, 12:14:40 PM »
I believe so, I'm sure there are tools that can perform the replacement directly from the command line, maybe even a batch file could be constructed and called that does that.
I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: Rename/replace
« Reply #66 on: January 08, 2010, 01:00:46 PM »
There are various editors that can be scripted, UltraEdit is one. I gace up using Notepad a very long time ago. Scite is a useful scriptable free editor.


ghostdog74



    Specialist

    Thanked: 27
    Re: Rename/replace
    « Reply #67 on: January 08, 2010, 01:17:51 PM »
    B C programmer,
      That's a good answer. Rather than pursue this any further, let me pick your brain for something I have thought about. It relates to the topic at hand.
    In any situation like the one in this topic there are any number of scenarios where you might want to replace or change or rename a file or set of files in a group of directories. Perhaps some users have a need to do this on a regular basis. Now is there a way of making an automated program that will help the user do this without the need for the user to have to rewrite and edit a batch file.? In other words, a script that would be fully automated from the viewpoint of the end-user. To that end, I was wondering if there is a way to invoke notepad and make it do search and replace from a script? If not, would a person have to use one of the GNU utilities such as SED?


    in *nix world long ago, tools such as vi, emacs, sed, awk, ed etc already have the functionality to "change" (or rather edit) files on the command line, because the author of those programs programmed those functionalities into them. If the author of notepad.exe does the same, then yes, you can use notepad,exe to change files on the command line.
    Code: [Select]
    notepad.exe /F "word_to_find" myfile  #find word
    notepad.exe /R "word_to_replace" myfile  #find and replace
    Just for record, you can also use edlin to "script" editing a file automatically in windows platform.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Rename/replace
    « Reply #68 on: January 08, 2010, 06:04:41 PM »
    Quote
    Just for record, you can also use edlin to "script" editing a file automatically in windows platform.
    Works better is you make a patch to edlin.

    ghostdog74



      Specialist

      Thanked: 27
      Re: Rename/replace
      « Reply #69 on: January 08, 2010, 06:48:25 PM »
      Works better is you make a patch to edlin.
      if a "patch" is ever going to be made, its definitely not by me or anyone else but the original author. (its not open source). Anyway, editors are just tools that opens a file , do something to it and depending on users, save to new file or not. So any programming language with capability to open/close/edit files are also "editors" in a sense.
      You can make a native "sed" using vbscript. eg just for s///

      Code: [Select]
      Set objFS=CreateObject("Scripting.FileSystemObject")
      Set regEx = New RegExp
      Set objArgs = WScript.Arguments
      strPattern = objArgs(0)
      p = Split(strPattern,"/")
      strOld = p(1)
      strNew = p(2)
      regEx.Pattern = strOld
      If p(0) = "s" Then
      r = 1 'set replace
      Else
      r = 0
      End If
      If p(UBound(p)) = "g" Then
      regEx.Global=True 'set global flag
      Else
      regEx.Global=False
      End If
      strFile = objArgs(1)
      Set objFile = objFS.OpenTextFile(strFile)
      Do Until objFile.AtEndOfLine
      If r= 1 Then
      strLine = objFile.ReadLine
      strLine = regEx.Replace(strLine, strNew)
      WScript.Echo strLine
      Else
      'strLine = regex.Execute(strLine)
      WScript.Echo "-->" & strLine
      End If
      Loop

      output
      Code: [Select]
      C:\test>more file
      old new old
      blah
      old

      C:\test>cscript //nologo sed.vbs "s/old/new/" file
      new new old
      blah
      new

      C:\test>cscript //nologo sed.vbs "s/old/new/g" file
      new new new
      blah
      new

      C:\test>sed "s/old/new/" file
      new new old
      blah
      new

      C:\test>sed "s/old/new/g" file
      new new new
      blah
      new

      this way as you keep on adding features, you can create your own "sed" natively :)

      myshutterclicks



        Rookie

        Re: Rename/replace
        « Reply #70 on: January 10, 2010, 12:41:51 AM »
        Thanks to those who made it a great thread by their contributions. I have been searching and reading different posts on the net for the last 2 hours and this is the best forum I found so far on Windows Shell programming.

        My problem is half solved by the posts made by "Helpmeh" and "Salmon Trout" on the first page of this thread. The second part of the problem is that how do I execute this piece of code recursively for all subdirectories. I have few hundreds GB of image files that I need to rename and there are hundreds of subdirectories over different backup disks. So, I would appreciate any help.

        Thanks,
        mushutterclicks

        BillRichardson



          Intermediate

          Thanked: 15
          Re: Rename/replace
          « Reply #71 on: January 10, 2010, 06:23:15 AM »
          The second part of the problem is that how do I execute this piece of code recursively for all subdirectories.
          mushutterclicks


          C:\>dir /?
          Displays a list of files and subdirectories in a directory.

            /
            /S          Displays files in specified directory and all subdirectories.
            /


          for /f "delims=" %%a in ('dir /b') do (

          for /f "delims=" %%a in ('dir /s /b') do (
          Bill Richardson

          myshutterclicks



            Rookie

            Re: Rename/replace
            « Reply #72 on: January 10, 2010, 06:36:39 AM »

            C:\>dir /?
            Displays a list of files and subdirectories in a directory.

              /
              /S          Displays files in specified directory and all subdirectories.
              /


            for /f "delims=" %%a in ('dir /b') do (

            for /f "delims=" %%a in ('dir /s /b') do (



            Hi Bill,

            Thanks for your response. I just tried that and it did not work. I think there is a problem with /S option here as the first /f option in the FOR is going to return only the files. May be I need to call this from another batch file or something to make things easier.


            Thanks,
            myshutterclicks


            BillRichardson



              Intermediate

              Thanked: 15
              Re: Rename/replace
              « Reply #73 on: January 10, 2010, 07:13:12 AM »
              Please post the code you used.  We can modify from there.

              The code offered was one line from #1 post in this thread.  All of the batch file must be used:

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              rem for /f "delims=" %%a in ('dir /b') do (
              for /f "delims=" %%a in ('dir /s /b') do (
              set fileorig=%%a
              set filenew=!fileorig:_= !
              ren !fileorig! "!filenew!"
              )

              "Save it in the folder which contains the files you want to change. "

              Save the above code to a file with .bat extention.  For example:  namepic.bat.  Then type namepic.bat at the command line and press  return.


              C:\>namepic.bat
              « Last Edit: January 10, 2010, 07:27:34 AM by BillRichardson »
              Bill Richardson

              Helpmeh



                Guru

              • Roar.
              • Thanked: 123
                • Yes
                • Yes
              • Computer: Specs
              • Experience: Familiar
              • OS: Windows 8
              Re: Rename/replace
              « Reply #74 on: January 10, 2010, 08:23:15 AM »
              Just throwing this out there: isn't the /s switch supposed to come after the /b switch?
              Where's MagicSpeed?
              Quote from: 'matt'
              He's playing a game called IRL. Great graphics, *censored* gameplay.