ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Some advice with recursively applying changes to multiple text files

(1/1)

questorfla:
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=%%ASET 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:
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.-questorfla (December 07, 2019, 01:47 AM)
--- End quote ---

Followed by this which indicates a whole directory tree:

... walk the directory recursively ...
--- End quote ---


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

Navigation

[0] Message Index

Go to full version