Search and Replace

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
wiredsolaris
Newbie
Posts: 10
Joined: Fri Oct 01, 2010 11:57 am

Search and Replace

Post by wiredsolaris » Wed Oct 13, 2010 12:35 pm

Good day,

Needing some assistance with a search and replace issue.

Let's say I have a folder with 62 files in it, tab delimited but with a txt extention. I have to currently use Windows search to located the ones that have a value of 0E-7 in them. No problem there.

I then have to open WordPad (Notepad hangs as some of the files are 60MB in size) and do a manual find and replace. I have found some ways to automate this (sort of). The issue that I am running into is this.

I can have a script parse the file, one line at a time, and replace the 0E-7 entry with what I want but... Some lines have that value multiple times. I have only been able to get anything to replace only one instance of the 0E-7 per line. Say if one line has it 4 times, it only replaces on intance of it.

I have done this using KixTart thus far not MS. Just got MS, so was wondering if anybody has a method of doing this.

SO basically what I need to do is have a script look at a directory and in each Tab delimited text file, search and replace every instance of a value adn then save the file. Is this possible?

Kind Regards,

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Oct 13, 2010 12:55 pm

Try this:

Code: Select all

//modify these three lines
Let>path=c:\my path\*.txt
Let>search_string=0E-7
Let>replacement_string=freddy

Let>WLN_NOCRLF=1

//get array of matching files
GetFileList>path,files
Separate>files,;,file_array

//loop through the array
If>file_array_count>0
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>this_file=file_array_%k%
    //output something so we know something is happening
    Message>Processing file: %k%/%file_array_count% - %this_file%
    
    //read the file contents into memory
    ReadFile>this_file,file_text
    
    //if file contains search string perform a search and replace
    Pos>search_string,file_text,1,pSrch
    If>pSrch>0
      //perform the search and replace on ALL occurrences
      StringReplace>file_text,search_string,replacement_string,file_text
    
      //now write the new file contents back out
      DeleteFile>this_file
      WriteLn>this_file,res,file_text
    Endif
  Until>k=file_array_count
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

wiredsolaris
Newbie
Posts: 10
Joined: Fri Oct 01, 2010 11:57 am

Post by wiredsolaris » Thu Oct 14, 2010 12:06 pm

Awesome and pretty close.
It does locate every instance of 0E-7, but it only replaces the -7 and not the entire string 0E-7.

However, if I do not use a variable in stringreplace it works. I.E.
Instead of (this only replaces the -7 and not 0E-7):
StringReplace>file_text,search_string,replacement_string,file_text

I use (this works):
StringReplace>file_text,0E-7,freddy,file_text

It is fine if I do not use a variable, but found it kind of funny. :-)

Thank you very much for the assistance.

mtettmar wrote:Try this:

Code: Select all

//modify these three lines
Let>path=c:\my path\*.txt
Let>search_string=0E-7
Let>replacement_string=freddy

Let>WLN_NOCRLF=1

//get array of matching files
GetFileList>path,files
Separate>files,;,file_array

//loop through the array
If>file_array_count>0
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>this_file=file_array_%k%
    //output something so we know something is happening
    Message>Processing file: %k%/%file_array_count% - %this_file%
    
    //read the file contents into memory
    ReadFile>this_file,file_text
    
    //if file contains search string perform a search and replace
    Pos>search_string,file_text,1,pSrch
    If>pSrch>0
      //perform the search and replace on ALL occurrences
      StringReplace>file_text,search_string,replacement_string,file_text
    
      //now write the new file contents back out
      DeleteFile>this_file
      WriteLn>this_file,res,file_text
    Endif
  Until>k=file_array_count
Endif

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Oct 14, 2010 12:12 pm

Ah, it's doing math on OE-7. Instead set the var with:

Let>search_string={"0E-7"}
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

wiredsolaris
Newbie
Posts: 10
Joined: Fri Oct 01, 2010 11:57 am

Post by wiredsolaris » Thu Oct 14, 2010 5:23 pm

Yeh,
Figured that out finally as well after messing with the string.
Great help as always man.

Finally had work purchase MS. Gonna save me at least 3 hours worth of manual work a night. Schweett product.
mtettmar wrote:Ah, it's doing math on OE-7. Instead set the var with:

Let>search_string={"0E-7"}

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts