-2

A VBScript is receiving input parameters containing spaces in both the path and filename:

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

The DEST would look like this: "F:\work\dir space"\"Test file.txt", but VBScript produces an error:

Number of the Error and Description is 52 Bad file name or number

The copy command has no issues when I do this:

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

I cannot control the directory nor the file name as I am converting a batch script to use VBScript. Any ideas how I can copy to a destination that contain spaces in both the path and filename WITHOUT calling the xcopy or copy commands?

======================================================================== Adding more information, I tried what you had suggested, but I am still getting the same error. The script is executed via the command prompt, eg.,

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

Code snippet:

…
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 listing:

--------------------------------------------
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
....
et_phonehome
  • 137
  • 12
  • Seriously, how many times does this question have to come up on this site? Its been answered numerous time before, VBScript is over 20 years old now and you think that nobody has ever needed to know how to encapsulate a file path to support file paths with spaces? – user692942 Jan 13 '19 at 22:43
  • Lankymart, provide a link where this has been addressed. – et_phonehome Jan 13 '19 at 23:01
  • 1
    @et_phonehome Again, you're creating the variables `TARGET` and `PARM` with ***NESTED DOUBLE QUOTES***. Stop doing that. – Ansgar Wiechers Jan 14 '19 at 00:13

1 Answers1

0

The code you posted does not produce the error you claim it does. As a matter of fact, it does exactly what you want it to do.

To get the error you described you'd have to add additional double quotes to your path, e.g. like this:

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

or (perhaps more likely) like this:

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

Don't do that. FileSystemObject methods can handle paths with spaces just fine. No need for trying to handle that yourself. The only thing you may want to change is the way you build the destination path:

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

fso.CopyFile SOURCE, DEST
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328