space in path

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

space in path

Post by CData »

greeting Hans, hope all is well

this works:

Shell "cmd /c xcopy /y C:\Users\MyName\Documents\NewFolder\FileName.txt C:\Users\MyName\Documents\"

this does not work:

Shell "cmd /c xcopy /y C:\Users\MyName\Documents\New Folder\FileName.txt C:\Users\MyName\Documents\"

NewFolder versus New Folder

have tried seemingly every method I can think of to encapsulate the space in the path name, and googled quite a bit but no joy....
a senior moment perhaps...

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: space in path

Post by HansV »

How about

Shell "cmd /c xcopy /y ""C:\Users\MyName\Documents\New Folder\FileName.txt"" C:\Users\MyName\Documents\"
Best wishes,
Hans

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: space in path

Post by CData »

nothing fires
it throws no error...but does nothing either (both cases newfolder -or- new folder)
also tried with or without the \ at the end

if I leave a space between " " it won't set up: Expected end of statement

....hmmm may have to set this up on a different PC/infrastructure and check - - while I see many q/a online on this topic, haven't found the handle to this

User avatar
Leif
Administrator
Posts: 7193
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: space in path

Post by Leif »

I'm not sure, but I think there may be an error in Hans' suggestion:
[code]...FileName.txt"" C:\Users\MyName\Documents\[/code]
should be
[code]FileName.txt" "C:\Users\MyName\Documents\[/code]
(i.e. the space before C:\ is in the wrong place.)


Please ignore the above - apologies Hans!


Otherwise, I would try running:

Code: Select all

xcopy "C:\Users\MyName\Documents\New Folder\FileName.txt" "C:\Users\MyName\Documents\"  /y
from a Command Prompt and see what errors, if any, are displayed.
Note: based on BATch files I use, the "/y" would come at the end.

(And I assume you are changing "MyName" to something legitimate?)
Leif

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: space in path

Post by CData »

hi Leif ... in reverse order:

xcopy "C:\Users\MyName\Documents\New Folder\FileName.txt" "C:\Users\MyName\Documents\" /y
- - this won't set up if I leave a space between " " it won't set up: Expected end of statement highlighting the 2nd clause

which is the same issue on the 2nd/middle example:
FileName.txt" "C:\Users\MyName\Documents\

and then in regard to the top/1st example:
...FileName.txt"" C:\Users\MyName\Documents\
- - - I did not set it up that way, didn't see it that way probably due to the line wrap and instead used:
...FileName.txt""C:\Users\MyName\Documents\
- - - this without spaces sets up, but nothing fired, as reported

baffled...

User avatar
Leif
Administrator
Posts: 7193
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: space in path

Post by Leif »

My apologies - Not realising you were online, I had corrected my original post!

I'm surprised you are getting that error when running from a Command prompt...
Leif

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: space in path

Post by HansV »

Do you have a specific reason for using Shell and xcopy instead of the built-in VBA command FileCopy?
Best wishes,
Hans

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: space in path

Post by CData »

Leif - sorry misunderstanding - am in a sub did not move to command prompt
Hans - just came out of the gates with xcopy ... must have used before and was in my personal library...will redo via FileCopy and report in....

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: space in path

Post by HansV »

The syntax is

FileCopy "sourcefile", "targetfile"

No Shell needed.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: space in path

Post by HansV »

Leif is correct that the /y switch should come at the end:

Code: Select all

Shell "cmd /c xcopy ""C:\Users\MyName\Documents\New Folder\FileName.txt"" C:\Users\MyName\Documents\ /y"
When I used paths and a file on my computer, this worked.
Best wishes,
Hans

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: space in path

Post by CData »

To follow up:

FileCopy works
...which is to say, I experimented with spaces in the strings and all worked fine

anyone reading this string however should be aware that the FileCopy Destination string MUST include the file name. Didn't see that point in the documentation and so I learned this the hard way. An obvious thing if one is changing the file name such as appending a back-up date - - - but not intuitive when one is keeping the same file name and my first attempts just went as far as the folder which did not work....

have not circled back to try and figure out XCopy so that's still a mystery - - for another time....

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: space in path

Post by ChrisGreaves »

CData wrote:
18 Nov 2021, 18:45
... anyone reading this string however should be aware that the FileCopy Destination string MUST include the file name ...
Hi CData.
I have learned that it can be worthwhile
(a) to check that the target file does NOT exist before the FileCopy and
(b) to check that the target file DOES exist after the FileCopy.
Especially if I am writing an application that runs unattended.
Cheers
Chris
An expensive day out: Wallet and Grimace

User avatar
John Gray
PlatinumLounger
Posts: 5401
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: space in path

Post by John Gray »

CData wrote:
18 Nov 2021, 14:16
xcopy "C:\Users\MyName\Documents\New Folder\FileName.txt" "C:\Users\MyName\Documents\" /y
I may be being obtuse, but you are placing the whole of that command on a single line, aren't you?
The error message you report makes me think that you are using two separate lines, which is guaranteed to fail...
John Gray

Venison is quiet deer, and quite dear.

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: space in path

Post by CData »

hi CG ... actually in my case the target file will always pre-exist as the need is to over-write the prior file with a newer file..... so I may have more work to do....

hi JG, yes was always one code line. though note in my original first post that the code line began w 'Shell .... which could well be part of the issue.... haven't gone back to wrestle with the xcopy method any further