How to use xcopy to copy a file to a non-existing directory?

As part of an automated script, I am trying to do something like this on a windows command prompt:

xcopy /I /Y resources\xyz\pqrs.txt %TEMP%\resources\xyz\pqrs.txt.bak 

% TEMP% does not have a resource directory.

This is the interactive output I get:

 Does C:\Users\username\AppData\Local\Temp\resources\xyz\pqrs.txt.bak specify a file name or directory name on the target (F = file, D = directory)? 

The thing is that I want to create a directory and copy the file without interaction.

If I use mkdir, I will have to extract the directory path before using mkdir. I just wondered if xcopy can be used to simultaneously implement a copy of mkdir +.

xcopy link for everyone.

Thanks for the help.

+4
source share
2 answers

Try the following:

echo d | xcopy / f C: \ abc \ file1 C: \ abc \ newfolder \ file2

+2
source

If the final path ends with a backslash, it will be automatically processed as a directory:

 xcopy /f C:\abc\file1 C:\abc\newfolder\file2 

will ask

 Does C:\abc\newfolder\file2 specify a file name or directory name on the target (F = file, D = directory)? f 

but

 xcopy /f C:\abc\file1 C:\abc\newfolder0\newfolder1\newfolder2\ 

will do the right thing.

+1
source

All Articles