9

I have checked some examples on internet but I can't get my (first) batch file to work. I would like to copy automatically my file from a folder to another one but nothing happen.

@echo off
xcopy "C:\source\" "C:\target\" /c /d /i /y
exit

Could you see anything wrong?

Thanks!!

Update: I have done the command given by Bali C but it still doesn't work. See snapshot

xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y
xcopy C:\folder1 C:\folder2\ /t /e /i /y

Image:
image

I have to stop it with CTRL + C.

PS: I'm on Win 7

Update (Solution): It works! The problem was the name xcopy,bat on my Desktop, and I was running the command from there, so it was executing the xcopy.bat file of my desktop instead of the Windows one.. I had to rename the file with "myxcopy.bat" :

@echo off
xcopy "C:\source" "C:\target" /c /d /i /y
exit
aschipfl
  • 33,626
  • 12
  • 54
  • 99
remyremy
  • 3,548
  • 3
  • 39
  • 56
  • 1
    What is the name of your batch file? From your screenshot, I'm guessing it's `XCOPY.BAT`. Try renaming to something else, like `MyXcopy.bat`. – aphoria Feb 03 '12 at 13:30
  • I'm honestly not sure then, I tried the exact command you tried in the screenshot (top one from my answer) and it worked fine, straight away. Unless you don't have permissions on those folders? – Bali C Feb 03 '12 at 13:33
  • 1
    Yes, if it was named `XCOPY.BAT`, that would definitely cause a problem. Every time you called `XCOPY` in your batch file, you would be calling another instance of `XCOPY.BAT` instead of the `XCOPY` command. – aphoria Feb 03 '12 at 20:48

4 Answers4

8

After testing most of the switches this worked for me:

xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y

This will copy the folder folder1 into the folder folder2. So the directory tree would look like:

C:
   Folder1
   Folder2
      Folder1
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • I tried removing some options or just /c /y But it doesn't change anything, I get the cursor blinking on the next line as well – remyremy Feb 02 '12 at 15:49
  • Could you post the exact code you are using? With the filename included? – Bali C Feb 02 '12 at 16:01
  • Actually, `\S` copies directories and subdirectories _except_ empty ones. – aphoria Feb 02 '12 at 16:43
  • Your answer is still not right. `\S` will copy directories and subdirectories _except_ empty ones. If a directory contains files/folders, it will get copied...if a directory does not contain files/folders it will not get copied. – aphoria Feb 02 '12 at 16:47
  • Lol I'm getting confused, since I'm presuming the file will be there the s switch won't cause a problem, just removed it from my answer. – Bali C Feb 02 '12 at 16:53
  • I tried different combinaisons of Options but still, do you manage to do it on your own computer? I have updated my question with the content of my xcopy.bat file – remyremy Feb 02 '12 at 19:51
  • The command you are using is trying to copy a folder, if you are copying a file you need to specify the file like in my answer. Are you copying a folder or a file? – Bali C Feb 03 '12 at 09:06
  • I'am trying to copy a folder, containing others files and folders – remyremy Feb 03 '12 at 11:40
  • Edited answer, hopefully this should fix it :) – Bali C Feb 03 '12 at 11:51
  • here you can find what does /t e/ /i /y mean: https://technet.microsoft.com/en-us/library/cc771254.aspx – Lukas Liesis Aug 17 '15 at 11:30
5

Based on xcopy help, I tried and found that following works perfectly for me (tried on Win 7)

xcopy C:\folder1 C:\folder2\folder1 /E /C /I /Q /G /H /R /K /Y /Z /J
mivi
  • 348
  • 5
  • 8
4

If the requirement is to copy all files in "\Publish\Appfolder" into the parent "\Publish\" folder (inclusive of any subfolders, following works for me) The switch '/s' allows copying of all subfolders, recursively.

xcopy src\main\Publish\Appfolder\*.* /s src\main\Publish\

3

You must specify your file in the copy:

xcopy C:\source\myfile.txt C:\target

Or if you want to copy all txt files for example

xcopy C:\source\*.txt C:\target
  • I tried xcopy C:\source\*.txt C:\target But it doesn't change anything, I get the cursor blinking on the next line – remyremy Feb 02 '12 at 15:49