Page 1 of 1 1
Topic Options
#193541 - 2009-04-22 08:03 AM Generating new file contents based on two input files
Willmaykett Offline
Fresh Scripter

Registered: 2007-01-07
Posts: 21
Hi,

Newbie here, so please be kind

I have a small problem which I am trying to work out, (and think logally the best way to attack the structure.)

Once a week, I generate a file that reads a file and outputs a new file using this code (with lots of help from a previous helper . )
 Code:
 
$InputFile=("C:\Output2.txt")	;Modify location for path
$OutputFile=("C:\List.txt") 	;Modify location for path
$Pick=5		;Select the number of lines you want
Break ON

$fso = CreateObject("Scripting.FileSystemObject")
$txt = $fso.OpenTextFile("$InputFile",Not 0)
$Array=Split($txt.ReadAll,@CRLF)
$txt.Close

$=SRND(@MSECS)
$=RND(0)

For $Output=1 To $Pick
	$Array=Split(udfSqueeze(Join($Array,@CRLF),@CRLF),@CRLF)
	$iChose=IIf(UBound($Array)>0,RND(UBound($Array)),0)

;		"Selection "+$Output+": "+$Array[$iChose]+@CRLF		;Debug to show on screen what is selected

	$ = Open(1,"$OutputFile",5)					;Generating output file list
      $ = WriteLine(1,$Array[$iChose]+@CRLF)	
	
	$Array[$iChose]=""
	
Next
$ = Close(1)

Function udfSqueeze($s,$q)
	$udfSqueeze=$q+$s+$q
	While InSTR($udfSqueeze,$q+$q)
		$udfSqueeze=Join(Split($udfSqueeze,$q+$q),$q)
	Loop
	$udfSqueeze=Split($q+$udfSqueeze+$q,$q+$q)[1]
EndFunction



Now, when I run a second job a week later, it generates a new output file. this output file MAY have the same entry in it as the previous week, so I want it to skip that entry and continue picking another entry to write into the file.

So, if the first week file generates
Word1
Word2
Word3
Word4

and the second weeks file has Word3 in it, I need the new output file to skip Word3 and continue picking a new word instead.

Does that make sense?

I am thinking about reading in the first file into an array, then reading the second file, select a random line in that file, check it, if not in the array, write the output, otherwise, discard that line, then pick another one, check it etc until I have my five lines in it.

Does that make sense? And does my method of using an array make sense? Or is there a better way to do it.

All help appreciated \:\)

Top
#193542 - 2009-04-22 09:45 AM Re: Generating new file contents based on two input files [Re: Willmaykett]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4945
Loc: Leatherhead, Surrey, UK
Tidied up the code a little.

This may not be the best solution as you didn't actually say what the script is being used for, but it is a literal solution for your outline requirement.
 Code:
Break ON

$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")
$=SetOption("ANSI","ON")

Dim $sDebug $sDebug=1			; Set to 1 to display debug messages, 0 to suppress
Dim $sFolderPath,$sInputPath,$sOutputPath, $sLastPath
Dim $iPick,$fso,$oInputFile,$oLastFile
Dim $asInputList, $sLastList, $iChose

$sFolderPath="D:\TEMP\"				; Folder containg data files
$sInputPath=$sFolderPath+"InputList.txt"	; File containing list to select from
$sLastPath=$sFolderPath+"InputList.old"		; Last week's file (copy of above
$sOutputPath=$sFolderPath+"OutputList.txt" 	; Selected items
$iPick=5					; Number of items to select

$=SRND(@MSECS)
$=RND(0)

; Copy last week's selections to backup file
Copy $sOutputPath $sLastPath

; Load up the original list
$fso = CreateObject("Scripting.FileSystemObject")
$oInputFile = $fso.OpenTextFile($sInputPath,Not 0)
$asInputList=Split($oInputFile.ReadAll,@CRLF)
$oInputFile.Close

; Load up last weeks selections
$oLastFile = $fso.OpenTextFile($sLastPath,Not 0)
$sLastList=@CRLF+$oLastFile.ReadAll+@CRLF
$oLastFile.Close

$=Open(1,$sOutputPath,5)					;Generating output file list

While $iPick
	$asInputList=Split(udfSqueeze(Join($asInputList,@CRLF),@CRLF),@CRLF)
	$iChose=IIf(UBound($asInputList)>0,RND(UBound($asInputList)),0)
	If $sDEBUG "Selection "+$iPick+": "+$asInputList[$iChose]+@CRLF EndIf	; *DEBUG*
	If $asInputList[$iChose]=""
		If $sDEBUG "   Input list exhausted - aborting."+@CRLF EndIf	; *DEBUG*
		Exit 259
	EndIf

	; If the item was previously selected then abandon it
	If InStr($sLastList,@CRLF+$asInputList[$iChose]+@CRLF)
		If $sDebug "  Selection dropped as used last week"+@CRLF EndIf	; *DEBUG*
	Else
		$=WriteLine(1,$asInputList[$iChose]+@CRLF)	
		$iPick=$iPick-1
	EndIf
	$asInputList[$iChose]=""
Loop
$=Close(1)

Function udfSqueeze($s,$q)
	$udfSqueeze=$q+$s+$q
	While InSTR($udfSqueeze,$q+$q)
		$udfSqueeze=Join(Split($udfSqueeze,$q+$q),$q)
	Loop
	$udfSqueeze=Split($q+$udfSqueeze+$q,$q+$q)[1]
EndFunction

Top
#193543 - 2009-04-22 11:49 AM Re: Generating new file contents based on two input files [Re: Richard H.]
Willmaykett Offline
Fresh Scripter

Registered: 2007-01-07
Posts: 21
Wow, quick and brilliant response.

I will give it a test and let you know how it goes.

Once again, thanks!!

Top
#193544 - 2009-04-22 03:36 PM Re: Generating new file contents based on two input files [Re: Willmaykett]
Willmaykett Offline
Fresh Scripter

Registered: 2007-01-07
Posts: 21
Richard H - you the man.

Thanks for the code, I just changed the file names around and it now does EXACTLY what I wanted.

100/100 for you mate.

Top
#193546 - 2009-04-22 04:48 PM Re: Generating new file contents based on two input files [Re: Willmaykett]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4945
Loc: Leatherhead, Surrey, UK
You are welcome, thank-you for the feedback.

There is a possible bug in the script - it depends on how you are using it. Because the script copies last week's file the new selections get appended to the existing list.

If you change the file COPY command to a MOVE command that should fix it.

Top
#193553 - 2009-04-23 01:20 AM Re: Generating new file contents based on two input files [Re: Richard H.]
Willmaykett Offline
Fresh Scripter

Registered: 2007-01-07
Posts: 21
Thanks for letting me know.

I had to make a small change to that line anyway, and substituted the COPY with a MOVE already.
I worked that one out when working through the code so I can get a better understanding of what it does.

I also liked the way you enable Debugging by simply turning it on in one place. Never thought of that... but hey, like I said, I am a newbie

I really seriously need to find a "scripting for Dummies" book so I can get a better understanding of scripting, as I am still in kindergarten when it comes to this stuff... nothing more frustrating that knowing what you want done but not being able to do it ....

Thanks again.

Top
#193557 - 2009-04-23 03:21 AM Re: Generating new file contents based on two input files [Re: Willmaykett]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4378
Loc: USA
 Quote:
I really seriously need to find a "scripting for Dummies"


Not that we the mods, or everyone else around here are dummies... but if you hang around and participate... you would be surprised how much you learn.

Top
#193558 - 2009-04-23 06:14 AM Re: Generating new file contents based on two input files [Re: Allen]
Willmaykett Offline
Fresh Scripter

Registered: 2007-01-07
Posts: 21
Sorry, didn't mean to offend...just calling myself a dummy cause I don't have a good understanding of this stuff, and only wish I could do half the things many of these talented people could do.

I was really just implying (my mistake) that I need to dig around for a while to find some good, basic learning materials that doesn't assume any previous knowledge about scripting or programming.

And yes, you are right, I have started to learn that with this stuff, you are only limited by your imagination

Top
#193564 - 2009-04-23 02:04 PM Re: Generating new file contents based on two input files [Re: Willmaykett]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4378
Loc: USA
no worries there... I'm sure we've all been called worse ;\)

Seriously, I just meant this site is far better than a book.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 37 anonymous users online.
Newest Members
FlyHorton, mschwj, Barchester, Evgeniy, kvanacker
17498 Registered Users

Generated in 0.031 seconds in which 0.01 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org