Filtering files

Questions or information that don't quite fit anywhere else

Moderator: Neosoft Support

Locked
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Filtering files

Post by netmediasurfer »

Suppose I have a text input field and the user can input any value.

How can I utilize that value to instruct NEOBOOK to read that amount of line data from a specific text file & have the data count written to their own individual files.


EXAMPLE

I have a text file with 20 lines of data

At the frontend I input the number 5 & hit a button.

5 now becomes the criteria for keeping every 5 lines of data within a specific file together within a new text file.

So the objective is to loop through the main text file & create new files containing 5 lines of data each until the main text file is divided into individual files.


----


Now suppose the user inputs an odd number which doesn't divide into the primary text file data line number. Sticking with this example.

20 is the official primary line count but the front end user decides to input 7 ...

How can I instruct NEOBOOK to split the main file & take that into account ?


Thanks!
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Filtering files

Post by Gaev »

netmediasurfer:

This code should give you an idea of how to approach the problem ...

Code: Select all

Setvar "[inputFile]" "![PubDir]abcd.txt"
SetVar "[outputFilePrefix]" "![PubDir]subFile"

... ask user for number of lines per file
InputBox "Enter Number of Lines Per File" "[linesPerFile]"
Math "[linesPerFile]*1" "0" "[linesPerFile]" 
If "[linesPerFile]" "<=" "0" 
  AlertBox "Sorry" "That’s not a valid number" 
Else 
  ... number of lines to read
  FileLen "![inputFile]" "[linesToCopy]"

  ... setup suffix of output file 
  SetVar "[suffixThisFile]" "1"

  ... setup line number in this suffix
  SetVar "[linesThisFile]" "0"

  ... loop through all lines in input file
  Loop "1" "[linesToCopy]" "[thisLineNumber]"
     ... read next line of text from input file
     FileRead "![inputFile]" "[thisLineNumber]" "[thisLineText]"
     ... check lines in current output file
     If "[linesThisFile]" "=" "[linesPerFile]"
        ... reached max line count for current output file; reset to 0
        SetVar "[linesThisFile]" "0"
       ... advance suffix of output file
        Math "[suffixThisFile]+1" "0" "[suffixThisFile]"     
     EndIf
     ... advance line counter for current output file
     Math "[lineThisFile]+1" "0" "[lineThisFile]"
     FileWrite "![outputFilePrefix][suffixThisFile]" "[linesThisFile]" "[thisLineText]"
  EndLoop
EndIf
Note that this code has not been tested/validated ... so if you encounter any typos or unexpected results, try and debug it by yourself first.
User avatar
virger
Posts: 540
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: Filtering files

Post by virger »

This helps you? I hope so.

Code: Select all

{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=3
Name=BtGeneraTx
X=105
Y=20
W=80
H=30
Anchor=0
Text=Genera Tx
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=StrParse "Alfa,Beta,Gama,Delta,Epsilon,Theta,Zita,Pi,Miu" "," "[DatLine]" "[x]"¶setvar "[DatLine0]" "Omega"¶loop "1" "100" "[n]"¶.Genera numero (Texto)¶    Random "135790" "[DtLine]"¶¶.Genera complemento de linea¶    Random "9" "[c]"¶¶.Salva data (Tx)¶    filewrite "[PubDir]AllData.Txt" "Append" "[DtLine] [DatLine[c]]_[n]"¶endloop
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=3
ObjectType=3
Name=BtCrFiles
X=100
Y=91
W=90
H=30
Anchor=0
Text=CreaFiles ([NumLines])
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=filelen "[PubDir]AllData.Txt" "[TotLines]"¶Math "[TotLines]/[NumLines]" "" "[FileCount]"¶Math "[TotLines]-([NumLines]*[FileCount])" "" "[Rest]"¶¶setvar "[nLine]" "1"¶setvar "[Ceros]" "0"¶loop "1" "[FileCount]" "[nFile]"¶    loop "1" "[NumLines]" "[cLine]"¶        fileRead "[PubDir]AllData.Txt" "[nLine]" "[Line]"¶        if "[nFile]" ">" "9"¶            setvar "[Ceros]" ""¶        endif¶        filewrite "[PubDir]NewFile_[Ceros][nFile].Txt" "Append" "([nFile]..[cLine])  [Line]"¶        math "[nLine]+1" "" "[nLine]"¶    endloop¶endloop¶¶if "[Rest]" ">" "0"¶    loop "[nLine]" "[TotLines]" "[nLine]"¶        FileRead "[PubDir]AllData.Txt" "[nLine]" "[Line]"¶        filewrite "[PubDir]NewFile_Rest.Txt" "Append" "(Rest..[nLine])  [Line]"¶    endloop¶endif
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=1
ObjectType=9
Name=EnNumLines
X=123
Y=55
W=45
H=30
Anchor=0
Text=7
VarName=[NumLines]
Align=1
EditNumber=Yes
EditLen=1
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=16
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
FROM COSTA RICA
PURA VIDA
COSTA RICA
PURA VIDA
Locked