topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday April 6, 2020, 7:27 am
  • Proudly celebrating 15 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Some advice with recursively applying changes to multiple text files  (Read 708 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 555
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
i think I'm getting lost in the iterations which is normal for me :(
i started out trying Find And Replace" (FAR) but never got it to work properly, perhaps due to the unusual characters.
I end up getting a working script using the Set command but hit problems when i tried to run it recursively over all 100+ text files in the folder while keeping the file names unchanged.

Each file is contains a single string repainting a URL and there are no spaces in the string.  The domain portion of each has changed and tht is the only part i need to modify  -  replace the section that says "domain1.com" with "domain2.com" ,change nothing else and keep the original filename.

This has to be done to over a hundred urls stored as named text files in a master folder.

So far this is what i got:

Code: Text [Select]
  1. for /f "tokens=1,* delims=ΒΆ" %%A in ( '"type *.txt"') do (
  2. SET string=%%A
  3. SET modified=!string:domain1.com=domain2.net!
  4. echo !modified! >> out.txt
  5. )

This works on a filename by filename basis but requires temporarily renaming the files while they are processed.   I feel sure there is a way to walk the directory recursively and keep the filenames intact during the modification.

As written, the code above requires an infile and an outfile a bunch of renaming and is far more 'work" than should be needed.

I bet 4WD will have what I need right at hand if he is on line.  :).   
I would like for the result to be flexible enough to save the code for use on future projects where i might need to do something similar.

Thanks

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,309
    • View Profile
    • Donate to Member
Re: Some advice with recursively applying changes to multiple text files
« Reply #1 on: December 07, 2019, 05:12 AM »
So you're talking about search and replace of a string within 100+ text files in one folder?

I say one folder because you're kind of contraindicating with this which indicates one folder:

... all 100+ text files in the folder while keeping the file names unchanged.

...

This has to be done to over a hundred urls stored as named text files in a master folder.

Followed by this which indicates a whole directory tree:

... walk the directory recursively ...


Anyways:

gsar.exe -sdomain1.com -rdomain2.com -o *.txt

Disclaimer: Thou who shalt not test before shall weather the consequences of thy actions.

PS. SOLVED: Recursive Search and Replace in Nested Folders
« Last Edit: December 07, 2019, 06:31 AM by 4wd »