≡ Menu

Copy directory from command line

Windows has two command line utilities to copy files/directories from command line. Copy command can be used to copy files from one folder to another folder. It can’t be used to copy a complete folder to another location on the disk. Xcopy allows us to do this. Let’s see how we can copy a directory along with all the files and sub directories to another location.

Xcopy /E /I SourceFolder DestinationFolder

Let’s say we need to copy a directory called C:\dir1\sourcedir to a location named D:\data\destinationdir.

Now you can run the below command to copy the complete sourcedir to D:\data\destination

Xcopy /E /I C:\dir1\sourcedir D:\data\destinationdir

In the above command we can also use relative paths of the folders with respect to the current directory.

Understanding the command

/E – This option makes sure that empty subfolders are copied to the destination.
/I – Avoids prompting if the destination is a folder or file. Not required if you are adding a trailing ‘\’ to the destination folder, like below.

Xcopy /E C:\dir1\sourcedir D:\data\destinationdir\

/S – This option is not required if you are using /E. /E ensures that all subfolders are also copied to the destination.

24 comments… add one
  • Sam

    yOU Are the Best….i just tried it now…it came very handy wen i needed it…

  • mr.hostile

    0 files copied

    • admin

      What was the exact command you tried? Can you paste the output of dir on the source folder

    • Brian

      @mr.hostile – 0 files copied MAY mean that you just copied a folder that contained no files. Folders being copied will not be counted; only files.
      @Mayank .K.P – to copy from an IP like that you are going to need to use a different format. Your options are to map the IP as a drive letter and then use the drive letter to do the xcopy OR you can use “\\12.33.58.31\dir1\sourcedir”

  • JBK

    Worked like a charm.

  • Vik

    xcopy/E

    worked fine

    Thanks :)

  • Aditya Thodge

    I want a cmd command to copy folder from Remote to Local computer using Ip address.
    Pl do the needful

  • Larokotuppoo

    I used this cmd before it copies the emty dir n files. But i was looking forward to copy the entire source dir like the folder named “aaa” present in desktop to “c:/ myself”
    It doesn’t do the job.
    Anyother method.

  • Shaileen

    I have folder A that includes 10 subfolders (1…10). I want to copy folders 3,5,8 & 9 (including subfolders) to Folder B. How can I do that?

  • Rince

    This only copies the files and not the directory in which the files reside.
    After Xcopy /E C:\dir1\sourcedir D:\data\destinationdir\

    i want destinationdir to contain sourcedir and all its files and subfolders
    so D:\data\destinationdir\sourcedir\FilesThatWereInSourcedir

    Exactly as if I had dragged (in windows explorer) sourcedir onto destinationdir
    How can I do that?

  • Stephani

    Can not get this to work. I need to get a directory called Germane located in C:\Build\ to copy to C:\Drivers so I end up with C:\Drivers\Germane and all of its contents.. instead I end up with C:\Drivers with all of the files contained in C:\Build\Germane… which .. since this is part of a process to copy a lot of things into drivers.. ends up really messy.

  • Walt Garff

    You are fabulous. My Windows Explorer 7 crashes on anything…
    I feel like Bruce Lee, just as straight command punch and all my music files are on a USB without spending a lifetime debugging windows..;c)

  • John

    If you want to copy entire directory trees, use robocopy. It’s included with Windows. Type “robocopy\?” at the command line to learn about it.

  • Kim

    Thanks buddy, works great!

  • Ashim Adhikari

    my source forder includes space in its name. i am unable to the the given commands.
    help!!!

  • Mayank .K.P

    Can I do
    Xcopy /E /I 12.33.58.31:\dir1\sourcedir D:\data\destinationdir ?

    If not, any other way to do that?

    • Brian

      xcopy /e /s \\12.33.58.31\dir1\sourcedir d:\data\destinationdir
      However, you will probably have to mount \\12.33.58.31 as a mapped drive.
      use /s to get the all the sub-directories too.

  • Reginold

    Thanks a lot. This is awesome. Helped me in day where I had to backup my data when my windows got corrupted. ❤

  • Bradford Robert McCormick

    Yes, thank you for this article. My ISP has said Go to hell. I have 10 years of work there, some of which is of value to the academic community. I’m not sure what I’m going to do with it (in the 1970s I was an IBM/S370/MVS Systems Programmer and wrote part of MVS, today I’m helpless). But I always kept the source for everything in my website on my local computer which now has a dead keyboard and I don’t dare power it off for fear it will not power up again.

    Anyway your example command line file let me copy my original source directory for everything onto a pluggable backup drive (I don’t know what it’s called), in a lot less time than it’s taking me to write this thank you to you, Sir. [email protected] ~ yes, again, thank you.

    Thank you

  • Pritam Ausarkar

    I want to copy only pdf files located in subfolders on a directory to a single folder on another directory
    Would you be able to help ?

  • Hi

    Thanks. This code helped me

  • Michael Jørgensen

    Explorer kept crashing for me when i tried to copy some files, but this method works like a charm. Thank you very much!

  • SIddu

    Can anyone help me how to copy folder address to a notepad
    i need to copy multiple folders address simultaneously…any help would be greatly appriciated.

  • Lawrence Knowlton

    In Windows 11, I’m trying to create a batch file to copy a folder in my %APPDATA% directory to another folder on my desktop as a backup, but xcopy won’t copy the folder to another folder, just the contents of it, which is highly annoying as I need to copy 2 folders from there to the same location. I’ve tried using the /E /I switches and quotes. /code @ECHO OFF
    @xcopy /I “%AppData%\Microsoft\Windows\Recent\CustomDestinations\” “C:\Users\user\Desktop\jumplist_backup”
    @xcopy /I “%AppData%\Microsoft\Windows\Recent\AutomaticDestinations\” “C:\Users\user\Desktop\jumplist_backup” /code
    Is it because the folders are ‘hidden’? I wouldn’t think the hidden part would the problem as it has no problem copying the contents and not the folder itself. Any suggestions? I’d really appreciate it! Thank you!

Leave a Comment