1

I have a file which is in folder structure like this E:\project\b\c\d\e\my.js I need to copy this file to another folder along with it's directory structure

e.g. I want to copy this file in E:\project\patch. So the command should do copy in such a way that when I run it the file should be at E:\project\patch\b\c\d\e\my.js. I can not copy folder b directly to the folder patch using file explorer cause b has so many other sub folders and files. I don't want to copy those.

Currently I am doing creating folder manually and copying the file. But I pretty much sure there will a command for this.

4
  • How many such files do you have to copy from different paths?
    – Prasanna
    Commented Jan 18, 2017 at 5:53
  • only one file. Currently what I am doing is creating directory structure using mkdir path\to\file then copy the file manually. Commented Jan 18, 2017 at 5:55
  • I'm @ work and cannot work out the full answer. When I searched - it looks like linux has a command called readlink. You have to find the equivalent for it in [Windows](www.bryanpendleton.blogspot.com/2010/07/windows-and-file-links.html) This will give you the complete path. Once it is a text, you can pick what you need and use mkdir command to create the directory and copy the file name using another command.
    – Prasanna
    Commented Jan 18, 2017 at 6:05
  • Thanks for sharing link. I'll go through it and see what I can do... You can workout the full answer later, it's not urgent. Commented Jan 18, 2017 at 6:17

3 Answers 3

1

You can do this in windows.

Copy a file:

echo F| XCOPY C:\utils\MyFile.txt D:\Backup\CopyFile.txt

Copy a folder:

XCOPY C:\utils D:\Backup\utils /i

Copy a folder including all subfolders.

XCOPY C:\utils* D:\Backup\utils /s /i

0

Use robocopy command

Copy a directory including sub directories and the files in sub directories

Robocopy /S D:\dir1\data E:\backup\data

This command does not copy empty directories. To copy them, you need to add /E switch.

Robocopy /S /E  D:\dir1\data E:\backup\data

The syntax is robocopy [options] [source] [destination]

4
  • The OP does not want the other files in the subdirectories to be copied. Will robocopy do that?
    – Prasanna
    Commented Jan 18, 2017 at 6:09
  • There is a /MIR switch specifies that Robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.
    – YanivK
    Commented Jan 18, 2017 at 6:18
  • OP can experiment with the commands and see... but please create a backup:)
    – YanivK
    Commented Jan 18, 2017 at 6:19
  • OP you can read more about the available switches here social.technet.microsoft.com/wiki/contents/articles/…
    – YanivK
    Commented Jan 18, 2017 at 6:32
0

A way to do that is store the path into a variable and then use it with the new destination.

Use something like:

SET MY_PATH_SRC=E:\project\b\c\d\e\

and

SET MY_PATH_DST=E:\project\patch\b\c\d\e\

then just copy using the new strings as base path.

Example:

copy MY_PATH_SRC\my.js MY_PATH_DST\my.js

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .