text processing help please

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Kenley
Newbie
Posts: 10
Joined: Sun Mar 14, 2004 11:10 am

text processing help please

Post by Kenley » Fri Mar 11, 2005 9:27 am

I need to learn how to use Macro Scheduler to sort thru a large text file.
I need to turn the following text file:


----------------------------------------------------------------------
ROBOCOPY v 1.96 : Robust File Copy for Windows NT
----------------------------------------------------------------------

Started : Thu Mar 10 22:55:22 2005

Source : \\mickeymouse\sourcefolder\
Dest : \\san\destinationfolder\
Files : *.exe
Exc Dirs : unLang
Options : /L /S /R:1000000 /W:30

---------------------------------------------------------------------

*EXTRA File 4204112 2ndLangfile-zhh.exe
New Dir 0 \\mickeymouse\sourcefolder\ARA\
New Dir 2 \\mickeymouse\sourcefolder\ARA\Lang\
New File 1228376 languagefile-ara.exe
New File 4209240 2ndLangfile-ara.exe
New Dir 0 \\mickeymouse\sourcefolder\CHP\
New Dir 2 \\mickeymouse\sourcefolder\CHP\Lang\
New File 1223248 languagefile-zhh.exe
New File 4204112 2ndLangfile-zhh.exe
New Dir 0 \\mickeymouse\sourcefolder\CHS\
New Dir 2 \\mickeymouse\sourcefolder\CHS\Lang\
New File 1222744 languagefile-chs.exe
New File 4204120 2ndLangfile-chs.exe
New Dir 0 \\mickeymouse\sourcefolder\CHT\
New Dir 2 \\mickeymouse\sourcefolder\CHT\Lang\
New File 1223256 languagefile-cht.exe
New File 4204120 2ndLangfile-cht.exe
New Dir 0 \\mickeymouse\sourcefolder\CSY\
New Dir 2 \\mickeymouse\sourcefolder\CSY\Lang\
New File 1229904 languagefile-csy.exe
New File 4211280 2ndLangfile-csy.exe


into example output that looks like this:

\\mickeymouse\sourcefolder\ARA\Lang\languagefile-ara.exe
\\mickeymouse\sourcefolder\ARA\Lang\2ndLangfile-ara.exe


I basically need to find out how to read the file and parse it to get formatted data. I don't want to have to use a separate text editor... I'd rather do it all by script.

I imagine that the parsing logic would be something along the lines of...
1. recursively finding every line that begins with "New Dir" then checking if the next word is '0'. If so, delete the line.

2.go back to the top of the file and find every line that begins with "New Dir" then checking if the next word is anything other than '0'. If so, copy to the end of the line and store the result in a variable.

3. Find next "New File" string, skip next word/number and copy the file name into a variable.

4. Write variable 1 and variable 2 to a file.

Is it possible to use MacroScheduler to perform this type of WYSIWYG text processing? Is there a better more efficient way to do it?

Any suggestions or code samples would be much appreciated.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Mar 11, 2005 12:30 pm

Hi,

It's nice to get easy ones once in a while :-) Here's the script you need:

Let>infile=f:\test.txt
Let>outfile=f:\newfile.txt

IfFileExists>outfile
DeleteFile>outfile
Endif

VBSTART
VBEND

Let>k=0
Label>readloop
Let>k=k+1
ReadLn>infile,k,line
If>line=##EOF##,doneread

Position>New Dir 2,line,1,pnd
Length>line,lenl
If>pnd>0
MidStr>line,11,lenl,newdir
VBEval>Trim("%newdir%"),newdir
else
Position>New File,line,1,pnf
If>pnf>0
MidStr>line,10,lenl,newfile
Position> ,newfile,1,pspace
Let>spos=pspace+1
MidStr>newfile,spos,lenl,newfile
VBEval>Trim("%newfile%"),newfile
WriteLn>outfile,res,%newdir%%newfile%
endif
endif
Goto>readloop
Label>doneread

//if you want to replace input file with output file uncomment next 2 lines:
//DeleteFile>infile
//MoveFile>outfile,infile

MessageModal>Done!

Just change the first two lines to reflect the path and filename of your input file and a valid path for the output file. Once you're happy with it uncomment the lines at the end to replace the input file with the output file if that is what you want to do.
MJT Net Support
[email protected]

Kenley
Newbie
Posts: 10
Joined: Sun Mar 14, 2004 11:10 am

getting Position errors and substring errors

Post by Kenley » Fri Mar 11, 2005 11:46 pm

Thanks for the response!

I'm getting many position errors and substring errors dialogs.

I think it has something to do with all the whitespace in the logfile.

The format of the logfile isn't as nice as appears in the post... there is quite a bit of space between "New dir" and 2.

The output I'm currently getting in the new file is:

%newdir% 4212824 langfile.exe

If I tweak the script to use "New dir" instead of "New dir 2" it does generate correct path. However I'm left with the '2' and string of numbers between the path and langfile.

The path and logfile \\path\somelangfile.exe is all I can use in the new file.

I'm reading through the documentation and having a tough time figuring out how the 'Position' keyword works. Am I getting all of the Position errors and substring errors because of the extraneous whitespace in the file?

What I think I'm missing is how to determine what is on a line once we have the Position. How does one know where one word stops and another ends? How do you search for strings moving forward and backward once you have the position?

Is the output not coming out correctly because we are not recognizing the .exe as a single word? Is there a way to make the cursor stop at the end of "New dir" , search forward for ".exe" and copy everything attached to the string ".exe" as a word (contiguous string of characters) and store it in a variable?

This way only the %newdir% and %filename% can be written to the newfile.

Is this how it should work? If so, how can the script search forward for a word containing ".exe" and only copy the word into a variable?

Is there a better way to do it?

Thanks for all the help.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sat Mar 12, 2005 12:59 am

I wrote my script based on the format of the log file supplied. It looked uniform so I used absolute positions to get started, but if the spaces are variable this won't work.

The obvious thing to do is to look for the first occurence of the \ character for the New Dir line and extract the string from that point. For New File, we can take off "New File" then look for the first numeric, then look for first space and then extract from there and then trim. Here's the modified version of my script which should cope with any number of spaces:

Let>infile=f:\test.txt
Let>outfile=f:\newfile.txt

IfFileExists>outfile
DeleteFile>outfile
Endif

VBSTART
VBEND

Let>k=0
Label>readloop
Let>k=k+1
//read a line from the file
ReadLn>infile,k,line
//if end of file, jump to end
If>line=##EOF##,doneread
//create a version of the line with no spaces in it
//so that we can reliably look for "NewDir2"
StringReplace>line, ,,tmpline
//if NewDir2 is in this line, use it
Position>NewDir2,tmpline,1,pnd
Length>line,lenl
If>pnd>0
//find position of first \ char and extract from there
Position>\,line,1,pnslash
MidStr>line,pnslash,lenl,newdir
//trim the string to remove leading and trailing spaces
VBEval>Trim("%newdir%"),newdir
else
//this must be a New File line
Position>New File,line,1,pnf
If>pnf>0
//remove all after "New File"
MidStr>line,9,lenl,newfile
//find position of first numeric
Let>startpos=0
Let>space={" "}
Let>i=0
Length>newfile,lennf
Repeat>i
Let>i=i+1
MidStr>newfile,i,1,char
If>charchar>0
Let>startpos=i
Let>i=lennf
endif
endif
Until>i=lennf
if>startpos>0
MidStr>newfile,startpos,lenl,newfile
Length>newfile,lennf
//now find position of first space
Let>s=0
Let>startpos=0
Repeat>s
Let>s=s+1
MidStr>newfile,s,1,char
If>char=space
Let>startpos=s
Let>s=lennf
endif
Until>s=lennf
if>startpos>0
//trim and output!
MidStr>newfile,startpos,lenl,newfile
VBEval>Trim("%newfile%"),newfile
WriteLn>outfile,res,%newdir%%newfile%
endif
endif
endif
endif
Goto>readloop
Label>doneread

//if you want to replace input file with output file uncomment next 2 lines:
//DeleteFile>infile
//MoveFile>outfile,infile

MessageModal>Done!

The only assumption is that the string "New File" is always "New File" with just one space in it. Otherwise it doesn't care how many spaces are between the 'fields'.

I have also added comments to help you understand what is going on. I suggest you read through this and cross reference with the documentation to understand what each command does.
MJT Net Support
[email protected]

Kenley
Newbie
Posts: 10
Joined: Sun Mar 14, 2004 11:10 am

Post by Kenley » Sat Mar 12, 2005 3:06 am

Thank you! Excellent support!

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