DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

questorfla

  • Fighting Slime all the Time
  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
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)
for /f "tokens=1,* delims=¶" %%A in ( '"type *.txt"') do (
SET string=%%A
SET modified=!string:domain1.com=domain2.net!
echo !modified! >> out.txt
)

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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Some advice with recursively applying changes to multiple text files
« Reply #1 on: December 07, 2019, 11: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:

Quote
... 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
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: December 07, 2019, 12:31 PM by 4wd »

Original address: https://www.donationcoder.com/forum/index.php?topic=49180.0