2017-03-16 96 views
0

我有一些问题:脚本优化

  1. 如何为报价支持;设置数据“(”C:\ text.txt“,True)”。

  2. 请修改以下优化程序代码并加快速度。

  3. 如何保持或暂停,直到txt文件在继续或读取此文件之前创建。

set A_mark {"} 
set B_mark {(} 
set C_mark {)} 
set D_mark {,} 
set path  [pwd] 
set pathFile [file join $path text.txt] 
set strgdt [join [list "Set objFile = objFSO.CreateTextFile" "$B_mark$A_mark$pathFile$A_mark$D_mark True$C_mark"] ""] 

#HERE THE FIRST QUESTIONS 
#I would like to write the format string example like this: Set objFile = objFSO.CreateTextFile("C:\text.txt", True) 

set vbs [list \ 
     {On Error Resume Next} \ 
     {Const wbemFlagReturnImmediately = &h10} \ 
     {Const wbemFlagForwardOnly = &h20} \ 
     {Dim objFSO, objFile} \ 
     {Set objFSO = CreateObject("Scripting.FileSystemObject")} \ 
     "$strgdt" \ ;#The variable from above 
     #so on.. 
     ] 

#Write the vbsFile 
set oFile [open "vscr.vbs" w] 

#Run the vbs file then create a txt file 
set res [exec {cmd.exe /c [file join $path vbscrpting.vbs]}] 

#HERE THE SECOND QUESTIONS 
#How to make sure the file already created and if the txt file still on processing then wait until finish create it. 

#Open txt file 
set txtFile [open "text.txt" r] 

回答

0

至于第一个问题,只需使用模板:

set script [format { 
    On Error Resume Next 
    Const wbemFlagReturnImmediately = &h10 
    Const wbemFlagForwardOnly = &h20 
    Dim objFSO, objFile 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.CreateTextFile("%s", True) 
    # So on 
} $pathFile] 

然后,你需要真正你的脚本的脚本文件。 您正在打开脚本文件,但不要写任何内容。 这样做:

set sfname [file join $path myscript.vbs] 
set fd [open $sfname w] 
puts $fd $script 
close $fd 

然后调用解释就可以了。 有没有感觉走很长的路—像你一样的往返 通过cmd.exe这将最终调入ShelleExecuteEx() Win32 API调用弄清楚如何处理.vbs文件,—只是 调用interpterer —:

set res [exec [list cscript.exe /nologo $sfname]] 

的时候exec结束,这意味着WSH解释完为好, 等文本文件,它本来是(重新)写,创建— 除非 解释器在调用objFSO.CreateTextFile()之前或在处理 调用之前检测到运行脚本中提供的 中的任何命令时出错。

所以,你需要真正看到你的电话exec怎么样

set rc [catch [list exec [list cscript.exe /nologo $sfname]] out] 
if {$rc != 0} { 
    puts stderr "WSH script failed: $out" 
    exit 42 
} 

进行 通过做一些事情,我建议你参考 catch manualexec manual

我也会考虑执行它—无论是从您的Tcl脚本来或 的WSH脚本—是一个很好的做法之前移除了WSH脚本的目标文件 句柄。 当你正在处理这个问题时,请确保你已准备好该文件不存在的情况。


注意,我想这导致你问你的第二个问题 可能与你不写剧本的脚本文件 这样做,它最终被空的,因此一个WSH解释称问题 就无所事事,马上退出。

一面说明,我明确地呼吁cscript.exe这是一个命令行WSH解释器。使用它的一个好处是,如果它检测到 和错误,它会在退出前将其写入其标准错误流, 和exec会读取该数据并将其返回给您的脚本。 缺点是执行cscript.exe会弹出一个控制台窗口— 除非您的Tcl脚本由tclsh运行,并且因此已由 托管过程。

您可以使用wscript.exe而不是cscript.exe:它是WSH解释器的“窗口”版本 。它不会弹出控制台窗口,但作为回报, 它会显示使用愚蠢的GUI窗口检测到的任何错误,其中有人必须单击“确定”按钮以让解释器死亡。对于非交互式的东西来说这非常愚蠢的 。当你“执行”WSH脚本时,默认运行wscript.exe

所以,请选择。