Appending data to numerous documents

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

Appending data to numerous documents

Post by netmediasurfer »

Hi, how can I do the following ..

Need to append additional data to either the top & or bottom of numerous document files within a directory. How can I instruct NEOBOOK to detect all those files and append additional data to either the top, bottom or both ?


I know FileWrite allows for this individually but I'm trying to append to numerous documents simultaneously with the same information.




Example


setvar = [additional data]


[additional data] = some new information




sample.txt


Contains ..


some default data



I need ..

"some new information" to be inserted into all the documents within a directory.



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

Re: Appending data to numerous documents

Post by Gaev »

netmediasurfer:

Try this ...

Code: Select all

... get list of files in folder
FileList "![PubDir]*.txt" "Files" "[DocFiles]"
... parse list into array; [#13] is code for carriage return
StrParse "[DocFiles]" "[#13]" "[FileNamesArray]" "[FileCount]"

... loop through array
Loop "1" "[FileCount]" "[FileCounter]" 
   ... write to file name in array item
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "1" "[AdditionalData]"       ... at start of file
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "Append" "[AdditionalData]"  ... at end of file
EndLoop
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Re: Appending data to numerous documents

Post by netmediasurfer »

I'll give it a try. Thanks!
Tony Kroos
Posts: 419
Joined: Thu Oct 15, 2009 3:43 pm

Re: Appending data to numerous documents

Post by Tony Kroos »

note that FileWrite replaces text at given line number, if u want to insert new data to 1st line u need to rebuild whole file:

Code: Select all

... loop through array
Loop "1" "[FileCount]" "[FileCounter]"
   ... save file contents
   FileToVar "![PubDir][FileNamesArray][FileCounter]" "[FileVar]"
   ... erase file contents
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "All" ""
   ... write data to 1st line
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "1" "[AdditionalData]"
   ... append saved file contents back
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "Append" "[FileVar]"
   ... write more data to bottom if needed
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "Append" "[AdditionalData]"
EndLoop
more accurate:

Code: Select all

... loop through array
Loop "1" "[FileCount]" "[FileCounter]"
   ... save file contents
   FileToVar "![PubDir][FileNamesArray][FileCounter]" "[FileVar]"
   ... build new content as you want
   Setvar "[NewData]" "[AdditionalData][#13][FileVar][#13][AdditionalData]"
   ... rewrite file with new data
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "All" "[NewData]"
EndLoop
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Appending data to numerous documents

Post by Gaev »

Tony Kroos:
note that FileWrite replaces text at given line number
Ooops ... forgot about that.
, if u want to insert new data to 1st line u need to rebuild whole file:
Here is a simpler way around this ...

Code: Select all

FileInsLine "![PubDir]... get list of files in folder
FileList "![PubDir]*.txt" "Files" "[DocFiles]"
... parse list into array; [#13] is code for carriage return
StrParse "[DocFiles]" "[#13]" "[FileNamesArray]" "[FileCount]"

... loop through array
Loop "1" "[FileCount]" "[FileCounter]"
   ... write to file name in array item
   ... at start of file
   FileInsLine "![PubDir][FileNamesArray][FileCounter]" "1"
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "1" "[AdditionalData]"
   ... at end of file
   FileWrite "![PubDir][FileNamesArray][FileCounter]" "Append" "[AdditionalData]"
EndLoop
... i.e. insert blank line ... then replace it.
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Re: Appending data to numerous documents

Post by netmediasurfer »

Hi, doesn't work properly. Any ideas ? : )


It creates 8 new files with the "[AdditionalData]" content on line 1 for each file but none of the original data is imported into the new files & no extension is assigned either. Continue to get access denied error. Something about syntax or cannot open file *.pls



I blocked the append below option for now.

.FileWrite "![PubDir]\PLAYLISTS\[FileNamesArray][loop_position]" "Append" "[AdditionalData]"



CODE


FileInsLine "![PubDir]\PLAYLISTS\*.pls" ""

FileToVar "![PubDir]\PLAYLISTS\*.pls" "[DocFiles]"

FileList "![PubDir]\PLAYLISTS\*.pls" "Files" "[DocFiles]"

StrParse "[DocFiles]" "[#13]" "[FileNamesArray]" "[pcount]"

Loop "1" "[pcount]" "[loop_position]"

FileInsLine "![PubDir]\PLAYLISTS\[FileNamesArray][loop_position]" "1"

FileWrite "![PubDir]\PLAYLISTS\[FileNamesArray][loop_position]" "1" "[AdditionalData]"

.FileWrite "![PubDir]\PLAYLISTS\[FileNamesArray][loop_position]" "Append" "[AdditionalData]"

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

Re: Appending data to numerous documents

Post by Gaev »

netmediasurfer:
Hi, doesn't work properly. Any ideas ? : )
Double Oops ... :oops: ... try this ...

Code: Select all

... get list of files in folder
FileList "![PubDir]*.txt" "Files" "[DocFiles]"
... parse list into array; [#13] is code for carriage return
StrParse "[DocFiles]" "[#13]" "[FileNamesArray]" "[FileCount]"


... loop through array
Loop "1" "[FileCount]" "[FileCounter]"
   AlertBox "[FileCounter]" "![PubDir][FileNamesArray[FileCounter]]"
   ... write to file name in array item
   ... at start of file
   ...FileInsLine "![PubDir][FileNamesArray[FileCounter]]" "1"
   ...FileWrite "![PubDir][FileNamesArray[FileCounter]]" "1" "[AdditionalData]"
   ... at end of file
   ...FileWrite "![PubDir][FileNamesArray[FileCounter]]" "Append" "[AdditionalData]"
EndLoop
Note that ...

1) [FileNamesArray[FileCounter]] is first resolved to ...

[FileNamesArray1] ... e.g. abc.txt
[FileNamesArray2] ... e.g. klm.txt
[FileNamesArray3] ... e.g. xyz.txt
etc.

2) Then, assuming [PubDir] is "c:\path\to\folder\", the first parameter (in FileWrite) becomes ...

[PubDir]abc.txt >>> c:\path\to\folder\abc.txt
[PubDir]klm.txt >>> c:\path\to\folder\klm.txt
[PubDir]xyz.txt >>> c:\path\to\folder\xyz.txt
User avatar
virger
Posts: 540
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: Appending data to numerous documents

Post by virger »

Hola, esta es mi contribucion.
Gracias por leerla.

Hello, this is my contribution.
Thanks for reading.

Este boton tiene un ejemplo
This button has a example

Code: Select all

{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=3
Name=BtFillFiles
X=2
Y=2
W=72
H=36
Anchor=0
Text=Fill Files
Align=2
ImageStyle=0
ObjAction=...ElApe list with 200 lastnames¶...ElNom list with 200 names¶filelen "[Pubdir]ElApe.txt" "LenApes"¶filelen "[Pubdir]ElNom.txt" "LenNoms"¶¶...genera 250 files names: File###.txt¶...with 10 to 30 lastname ,names each¶loop "1" "250" "[f]"¶    Random "20" "[rnd]"¶    math "[rnd]+10" "" "[rnd]"¶    loop "1" "[rnd]" "[d]"¶        Random "[LenApes]" "[rape]"¶        math "[Rape]+1" "" "[rape]"¶        fileread "[Pubdir]ElApe.Txt" "[n]" "[dt1]"¶        random "[LenNoms]" "[rnom]"¶        math "[rNom]+1" "" "[Rnom]"¶        fileread "[Pubdir]ElNom.Txt" "[rNom]" "[dt2]"¶        filewrite "[Pubdir]paja\File[f].txt" "Append" "[dt1],[dt2]"¶    endloop¶endloop¶¶..============================¶¶... take all file_names¶FileList "[Pubdir]paja\file*.txt" "Files" "[lista]"¶... temporal LISTBOX (hidden)¶ListBoxSize "LstFiles" "[len]"¶¶...read every file¶loop "1" "[len]" "[c]"¶    ListBoxGetItem "LstFiles" "[c]" "[file]"¶..random if file is insert in 1rst. line (rnd==1)¶..                         in last line (rnd==3)¶..                         in 1rst., last and midlines of file == 2¶    Random "119" "[rnd]"¶    if "[rnd]" "<" "40"¶        setvar "[rnd]" "1"¶    else¶        if "[rnd]" ">" "79"¶            setvar "[rnd]" "3"¶        else¶            setvar "[rnd]" "2"¶        endif¶    endif¶¶    if "[rnd]" "=" "1"¶        Random "500" "[uno]"¶.. random number to include in 1rst. line¶        FileInsLine "[Pubdir]paja\[file]" "1"¶        FileWrite   "[Pubdir]paja\[file]" "1" "[uno]"¶    endif¶¶    if "[rnd]" "=" "3"¶        Random "500" "[tres]"¶.. random number to include in last line¶        math "[tres]+500" "" "[tres]"¶        FileWrite   "[Pubdir]paja\[file]" "Append" "[tres]"¶    endif¶¶    if "[rnd]" "=" "2"¶        Random "500" "[dos]"¶.. random number to include in 1rst, midlines  and last line¶        math "[dos]+1000" "" "[dos]"¶        FileInsLine "[Pubdir]paja\[file]" "1"¶        FileWrite   "[Pubdir]paja\[file]" "1" "[dos]"¶        FileWrite   "[Pubdir]paja\[file]" "Append" "[dos]"¶        filelen "[Pubdir]paja\[file]" "[xlen]"¶        math "[xlen]/2" "" "[xlen]"¶        FileInsLine "[Pubdir]paja\[file]" "[xlen]"¶        FileWrite   "[Pubdir]paja\[file]" "[xlen]" "[dos]"¶    endif¶¶endloop
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
Desde Costa Rica
Pura Vida
Somos Leyenda
Brasil 2014
COSTA RICA
PURA VIDA
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Re: Appending data to numerous documents

Post by netmediasurfer »

Hi, this time it worked flawless! : ) I disabled the alert box & it's doing exactly what I needed.

Would it be possible to capture the title names & when the .pls files are updated with the "[AdditionalData]" the files themselves are renamed with the associated station titles ?


So instead of having ..


STATION1.pls

It would take on it's actual content title name -- > Club FM Live.pls or Club FM Live.m3u



Thanks a million to all whom helped!


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

WORKING CODE


... get list of files in folder
FileList "![PubDir]*.txt" "Files" "[DocFiles]"
... parse list into array; [#13] is code for carriage return
StrParse "[DocFiles]" "[#13]" "[FileNamesArray]" "[FileCount]"

... loop through array
Loop "1" "[FileCount]" "[FileCounter]"
AlertBox "[FileCounter]" "![PubDir][FileNamesArray[FileCounter]]"
... write to file name in array item
... at start of file
...FileInsLine "![PubDir][FileNamesArray[FileCounter]]" "1"
...FileWrite "![PubDir][FileNamesArray[FileCounter]]" "1" "[AdditionalData]"
... at end of file
...FileWrite "![PubDir][FileNamesArray[FileCounter]]" "Append" "[AdditionalData]"
EndLoop
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: Appending data to numerous documents

Post by Gaev »

netmediasurfer:
So instead of having ..

STATION1.pls

It would take on it's actual content title name -- > Club FM Live.pls or Club FM Live.m3u
I am assuming this question is related to your other topic Creating new files from a document containing data sets ?? ... if so ...

1) Why not create the file(s) with the Title in the first place ?

2) If you choose to do it later, note that NeoBook does not have a FileRename command ... but you can "simulate" it by ...

- doing a FileCopy command
- followed by a FileErase on the original file

3) In either case, you will need to make sure there are no duplicate titles ... or the second copy may overwrite the first one.

You could guard against this by having both the STATIONx and the Title in the file name.


As for the method, you can ...

a) FileRead the line containing the content (3rd line BEFORE you insert [AdditionalData])

b) Parse the content with Delimiter of = ... the second array item will be the title

c) use this value in your FileCopy command
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Re: Appending data to numerous documents

Post by netmediasurfer »

1) Why not create the file(s) with the Title in the first place ?

It's a huge number of files with arbitrary names so I would be forced to manually change the titles for each one. I'm trying to automate the process with my own custom NeoBook app which will save me weeks of changing the file names manually. The files already exist, I'm not creating them on the fly. I'm simply trying to clean it up to make it more useful & easier to manage.


---

How can I instruct NEOBOOK to use the suggested File Copy command or FileRead efficiently based on the parsed count ?


Hard coding a set number of File Read lines is not a good solution for me because the number of files within a target directory vary constantly. If it were static it would not be an issue but I'm trying to avoid hard coding a specific number of "File Copy" and or "File Read" lines because the file count within the directories I'm working with vary & are dynamic.


The objective is to allow NEOBOOK to determine exactly how many "File Copy" and or "File Read" lines are required based on the parsed count. That way I can instruct NEOBOOK to properly capture the data required ( in this case the titles ) from the external files so that I can rename the files with the proper titles found within each file.


-----

The reason I struggle with programming in NeoBook is because I tend to work with numerous external files & many of the functions in NeoBook are targted towards single file processing. So unless I use a loop code or some sort of sub routine I get stuck on handling multiple files.


Example


FileWrite blah blah to a file

FileCopy a file

FileErase a file



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

Re: Appending data to numerous documents

Post by Gaev »

netmediasurfer:
1) Why not create the file(s) with the Title in the first place ?

It's a huge number of files with arbitrary names so I would be forced to manually change the titles for each one. I'm trying to automate the process with my own custom NeoBook app which will save me weeks of changing the file names manually. The files already exist, I'm not creating them on the fly. I'm simply trying to clean it up to make it more useful & easier to manage.
In this other post ... viewtopic.php?f=7&t=20655 ... you sought help in creating files with names like ...

STATION1.pls or STATION1.m3u
STATION2.pls or STATION2.m3u
STATION3.pls or STATION3.m3u

My point is that ... instead of creating files with names like OPTIONx.pls ... why don't you just create them with the names like Club FM Tirana Live or COOLcelsius 91.5 or Metro Oslo ? ... you already have the logic/commands to extract the title information of each entry/file ... so just use it as the name of the file in your FileWrite command ?
So unless I use a loop code or some sort of sub routine I get stuck on handling multiple files
Welcome to the world of programming.

Best to learn how the Loop/EndLoop command block works ...

1) read the Help file

2) make up examples and (using the Debugger and/or AlertBox command) watch how the various variables change values in each iteration
netmediasurfer
Posts: 18
Joined: Thu Aug 21, 2014 4:21 am

Re: Appending data to numerous documents

Post by netmediasurfer »

I only used STATION1.pls ect.. as a way to explain what I was trying to accomplish. The objective there was to generate the new playlist files by importing them into an array and using a delimiter to clean up the data before exporting them out again as new files.


Didn't bring up the question about individual file title renaming because I was trying to figure it out on my own. I only post questions when I have trouble figuring it out or can't find a potential solution in the support forum.


Thanks for all your help! : )


I'll see what I can come up with.
Locked