copy files in one location to another and modify the copied file by placing some data at particular location in tcl? -
i have perform following operation..
copy file 1 location another
search word in given file
and move file pointer beginning of line
place data in location copied other file...
3 files follows:
c:\program files(x86)\route\*.tcl c:\sanity_automation\route\*.tcl c:\script.tcl first need copy files route folder in program files sanity_automation\route*.tcl
then need search "closealloutputfile keyword in
c:/sanity_automation/route/systemtest.tcl once found, move cursor beginning of line "closealloutputfile " keyword found.
and place data found on script.tcl location.
firstly, first "file" pattern. need expand list of real filenames. glob.
# in braces because there backslashes set pattern {c:\program files(x86)\route\*.tcl} # de-fang backslashes set pattern [file normalize $pattern] # expand set sourcefilenames [glob $pattern] then want copy them. with:
set target {c:\sanity_automation\route\} file copy {*}$sourcefilenames [file normalize $target] but want build list of moved files can process them in next step. this:
set target {c:\sanity_automation\route\} foreach f $sourcefilenames { set t [file join $target [file tail $f]] file copy $f $t lappend targetfilenames $t } ok, we're going insertion processing. let's start getting data insert:
set f [open {c:\script.tcl}] set insertdata [read $f] close $f now, want go on each of files, read them in, find insertion, insertion if find place, , write files out. (you text edits read/modify-in-memory/write rather trying modify file directly. always.)
# iterating on filenames foreach t $targetfilenames { # read in set f [open $t] set contents [read $f] close $f # search (this easiest way!) if {[regexp -indices -line {^.*closealloutputfile} $contents where]} { # found it, insert set idx [lindex $where 0] set before [string range $contents 0 [expr {$idx-1}]] set after [string range $contents $idx end] set contents $before$insertdata$after # did insert, write out set f [open $t "w"] puts -nonewline $f $contents close $f } } normally, i'd modify part of copy, we'll way here.
Comments
Post a Comment