2017-03-16 103 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

所以,請選擇。