VBScript CopyFile带空格

时间:2019-01-13 16:51:31

标签: vbscript file-copying

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命令?

================================================ ======================== 添加更多信息,我尝试了您的建议,但仍然遇到相同的错误。该脚本通过命令提示符执行,例如

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
....

1 个答案:

答案 0 :(得分:0)

您发布的代码不会产生您声称的错误。事实上,它确实可以精确地完成您想要的操作。

要获取您所描述的错误,您必须在路径中添加其他双引号,例如像这样:

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