This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Backup of txt file

Old posts that have not been replied to for several years.
Locked
T
TomSommer

Backup of txt file

Post by TomSommer »

Hello, I want to make a backup of a file called words.txt to words.bak

So I was wondering how to do this... perhaps there is a copy function or something?
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

open the txt file and read it into a variable/list, then open a new file (the bak file) and put the variable in there.... [open $somefile a+] <--- that should make a new file if it does not already exists.... u might wanna check if the file exists ;)
Elen sila lúmenn' omentielvo
T
TomSommer

Post by TomSommer »

Thanks.. how do I read the whole file into a variable.. gets only gets one line
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

I think this:

Code: Select all

set op [open $thefile r]
set fs [read $op]
should put everything in the file into the $fs var

if not...

Code: Select all

while {![eof $file]} {
  set f "[gets $file]"
  lappend somelist $f
}
then this should read every line of the file and put them into $somelist
Elen sila lúmenn' omentielvo
T
TomSommer

Post by TomSommer »

nice, thanks... how do I loop through the list? and put it in.. the read thing does not work
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

u could just put the whole list into the file as it is... if u want to apply each element to the list :

Code: Select all

foreach l $somelist {
  puts $file $l
}
this should do the trick :)
Elen sila lúmenn' omentielvo
T
TomSommer

Post by TomSommer »

No, because then there would be lots of {}

But I tried putting all data into a string.... but that does not keep the newlines....

So maybe put it all into an array and load the lines into the new file?

so my question is...

array set x {}
how do I just add an element to x...?
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

u tried this?

Code: Select all

while {![eof $file]} { 
  set f "[gets $file]" 
  puts $backupfile $f 
}
Elen sila lúmenn' omentielvo
T
TomSommer

Post by TomSommer »

DOH!!!

Works like a charm.... Thanks
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

You can simply use the "[file copy]" command provided with Tcl.

It's far quicker, and it doesn't given any of the hastle assosiated with {}'s in badly handled strings.
Locked