将一个位置的文件复制到另一个位置并通过在tcl中的特定位置放置一些数据来修改复制的文件?

时间:2013-05-14 09:02:11

标签: tcl

我必须执行以下操作..

  1. 将文件从一个位置复制到另一个位置

  2. 搜索给定文件中的单词

  3. 并将文件指针移动到该行的开头

  4. 将数据放置在从其他文件复制的位置...

  5. 3个文件如下:

    C:\program Files(X86)\Route\*.tcl 
    
    C:\Sanity_Automation\Route\*.tcl
    
    C:\Script.tcl 
    

    首先我需要将Program Files中的Route文件夹中的文件复制到 Sanity_Automation \路线* .tcl

    然后我需要在

    中搜索“CloseAllOutputFile”关键字
    C:/Sanity_Automation/Route/SystemTest.tcl
    

    找到后,将光标移动到找到“CloseAllOutputFile”关键字的那一行的开头。

    将script.tcl上的数据放到该位置。

2 个答案:

答案 0 :(得分:2)

首先,第一个“文件”实际上是一种模式。我们需要将其扩展为真正的文件名列表。我们使用glob

# In braces because there are backslashes
set pattern {C:\Program Files(X86)\Route\*.tcl}
# De-fang the backslashes
set pattern [file normalize $pattern]
# Expand
set sourceFilenames [glob $pattern]

然后我们要复制它们。我们可以这样做:

set target {C:\Sanity_Automation\Route\}
file copy {*}$sourceFilenames [file normalize $target]

但实际上我们还想建立一个已移动文件的列表,以便我们可以在下一步中处理它们。所以我们这样做:

set target {C:\Sanity_Automation\Route\}
foreach f $sourceFilenames {
    set t [file join $target [file tail $f]]
    file copy $f $t
    lappend targetFilenames $t
}

好的,现在我们要进行插入处理。让我们从插入数据开始:

set f [open {C:\Script.tcl}]
set insertData [read $f]
close $f

现在,我们要查看每个文件,读取它们,找到插入位置,如果找到该位置,实际执行插入,然后将文件写回。 (您通过读取/修改内存/写入进行文本编辑,而不是直接尝试修改文件。始终。)

# Iterating over the filenames
foreach t $targetFilenames {

    # Read in
    set f [open $t]
    set contents [read $f]
    close $f

    # Do the search (this is the easiest way!)
    if {[regexp -indices -line {^.*CloseAllOutputFile} $contents where]} {

        # Found it, so do the 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

        # We did the insert, so write back out
        set f [open $t "w"]
        puts -nonewline $f $contents
        close $f
    }
}

通常情况下,我会将修改作为副本的一部分,但我们会按照您的方式进行修改。

答案 1 :(得分:0)

试试这个:

set sourceDir [file join / Files(x86) Route]
set destinationDir [file join / Sanity_Automation Route]

# Read the script to be inserted

set insertFnm [file join / Script.tcl]
set fil [open $insertFnm]
set insertData [read $fil]
close $fil

# Loop around all the Tcl scripts in the source directory

foreach inFnm [glob [file join $sourceDir *.tcl]] {
    # Determine the name of the output file

    set scriptName [file tail $inFnm]
    set outFnm [file join $destinationDir $scriptName]

    # Open source and destination files, for input and output respectively

    set inFil [open $inFnm]
    set outFil [open $outFnm w]

    while {![eof $inFil]} {
    set line [gets $inFil]
    if {[string match *CloseAllOutputFile* $line]} {
        puts $outFil $insertData
        puts $outFil "";         # Ensure there's a newline at the end
                                     # of the insertion
    }
    puts $outFil $line
    }

    # Close input and output files

    close $inFil
    close $outFil
}

这似乎对我有用。