WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: pamsniffer on March 15, 2015, 12:05:27 PM

Title: search en replace
Post by: pamsniffer on March 15, 2015, 12:05:27 PM
Hi,

How can i can in a text file on every word the last two numbers or remove then.

Pam
Title: Re: search en replace
Post by: JTaylor on March 15, 2015, 02:12:27 PM
Assuming you consider a "word" to be group of characters separated from other groups by a SPACE, I would retrieve the contents and then a FOR Loop to loop through the words doing what I want with them. 

Some functions of interest would be FileGet(), StrReplace(), ItemExtract() and a few others.   Should be a fairly simple operation.

Jim
Title: Re: search en replace
Post by: pamsniffer on March 22, 2015, 09:19:58 AM
look easy but not for me.
I have words and a want to remove in a file the last two numbers on the end.

beef01
beef045
horse23

pam
Title: Re: search en replace
Post by: JTaylor on March 22, 2015, 11:06:59 AM
If you post an sample file someone may be willing to post some code that might ease your way into a solution.

Jim
Title: Re: search en replace
Post by: stanl on March 23, 2015, 07:28:10 AM
Quote from: pamsniffer on March 22, 2015, 09:19:58 AM
look easy but not for me.
I have words and a want to remove in a file the last two numbers on the end.

beef01
beef045
horse23

pam

last 2 -  so beef01 becomes beef and beef045 becomes beef0

Be sure what you are asking for.

Title: Re: search en replace
Post by: JTaylor on March 23, 2015, 07:54:33 AM
You may need to do a bit of tweaking but something like the following should work.  It could be shortened but I went the long route for readability.

old_file = "c:\......"
new_file = "c:\....."

txt = FileGet(old_file)
txt = StrReplace(txt,@CRLF,@CR)
cnt = ItemCount(txt,@CR)
nlist = ""
For x = 1 to cnt
  line = ItemExtract(x,txt,@CR)
  line = StrTrim(line)
  line = StrSub(line,1,StrLen(line)-2)
  nlist = nlist:line:@CR
Next
FilePut(new_file,nlist)