簡體   English   中英

帶空格的 VBScript CopyFile

[英]VBScript CopyFile with spaces

VBScript 正在接收在路徑和文件名中包含空格的輸入參數:

SOURCE = "c:\temp\testabc"
TARGET = "F:\work\dir space"
PARM   = "Test file.txt"
DEST   = TARGET & "\" & PARM
'…
fso.CopyFile SOURCE, DEST

DEST看起來像這樣: "F:\\work\\dir space"\\"Test file.txt" ,但 VBScript 產生一個錯誤:

錯誤和描述的編號為 52 錯誤的文件名或編號

當我這樣做時, copy命令沒有問題:

copy "c:\temp\testabc" "F:\work\dir space"\"Test file.txt"

我無法控制目錄和文件名,因為我正在將批處理腳本轉換為使用 VBScript。 任何想法如何在不調用xcopycopy命令的情況下copy到路徑和文件名中都包含空格的目標?

================================================== ====================== 添加更多信息,我嘗試了您的建議,但仍然出現相同的錯誤。 該腳本通過命令提示符執行,例如,

cscript.exe test001.vbs "F:\Work\datafile.txt"

代碼片段:

…
Set fso = CreateObject("Scripting.FileSystemObject")
Dest = fso.BuildPath(Target, Parm)
…
Do While retry_counter < retry_max
    WScript.Echo "Retry count ", retry_counter
    fso.CopyFile Source, Dest
    if Err.Number <> 0 Then
        Wscript.Echo "Number of the Error and Description is ", Err.Number, " ", Err.Description
        Err.Clear
    End if
    retry_counter = retry_counter + 1
    WScript.echo now()
    WScript.Sleep retry_sleeper
    WScript.echo now()
Loop
…

顯示列表:

--------------------------------------------
Display dictionary contents :
my_id  :  "someuser"
my_source  :  "c:\temp\testabc"
my_target  :  "F:\work\dir space"
my_parm  :  "Test file.txt"
my_idt  :  A12175803
my_idtu  :  IDTU5803
--------------------------------------------

--------------------------------------------
Display variable contents :
Source:  "c:\temp\testabc"
Target:  "F:\work\dir space"
Parm:    "Test file.txt"
Dest:    "F:\work\dir space"\"Test file.txt"
--------------------------------------------

Retry count  0
Number of the Error and Description is  52   Bad file name or number
1/13/2019 5:49:05 PM
1/13/2019 5:49:10 PM
Retry count  1
....

您發布的代碼不會產生您聲稱的錯誤。 事實上,它完全按照你的意願去做。

要獲得您描述的錯誤,您必須在路徑中添加額外的雙引號,例如:

DEST = """" & TARGET & """\""" & PARM & """"

或(也許更有可能)像這樣:

TARGET = """F:\work\dir space"""
PARM   = """Test file.txt"""
DEST   = TARGET & "\" & PARM

不要那樣做。 FileSystemObject 方法可以很好地處理帶有空格的路徑。 無需嘗試自己處理。 您可能想要更改的唯一一件事是您構建目標路徑的方式:

SOURCE = "c:\temp\testabc"
TARGET = "F:\work\dir space"
PARM   = "Test file.txt"
DEST   = fso.BuildPath(TARGET, PARM)

fso.CopyFile SOURCE, DEST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM