0

I'm writing a small batch file to copy my C# project to another drive. I'm using XCOPY to copy an entire folder (We have some XP machines still, so robocopy isn't an option). However, when I run the batch file, it tells me that it cannot find the FILE specified (why it's looking for a file and not a folder I have no clue).

Here's my folder structure. I'd like to copy folder to the program\dst folder on the O drive.

src
-folder
-batchFile.bat

O
-program
--dst    

My batchFile.bat contains the following line

XCOPY  ".\folder" "O:\program\dst" /E

When I run this, it says:

File Not Found - folder

even though it most definitely exists (as a folder, not a file).

3 Answers 3

1

As other answers suggest, using .\folder starts from the current directory, while you want the batch file's location:

XCOPY  "%~dp0folder" "O:\program\dst" /E

You could cd to the folder first, but this is more elegant IMO.

2
  • %~dp0 already contains a trailing backslash, so it should be "%~dp0folder" (the parser corrects this error, but you should use the correct form nevertheless) Besides that, best answer, imho.
    – Stephan
    May 30, 2021 at 6:43
  • You're right, edited.
    – Jonathan
    Jun 1, 2021 at 7:48
0

I tried this, and it works for me. I'm guessing you've created a shortcut to the batch file, and the start directory isn't correct. If so, adding a cd command to the start of the batch file to change directory into src should fix it, or alternatively use an absolute path to folder instead of a relative one:

cd "C:\Whatever\src"
XCOPY ".\folder" "O:\program\dst" /E

or

XCOPY "C:\Whatever\src\folder" "O:\program\dst" /E
5
  • No shortcut, I'm running it directly, so I'm not sure why the relative path isn't working (as it is preferred to use relative path for my needs) Jul 28, 2014 at 19:14
  • @audiFanatic have you tried it with an absolute path?
    – Cocowalla
    Jul 28, 2014 at 21:17
  • Yea, and it works. But absolute isn't the best solution for my needs unfortunately. Jul 29, 2014 at 1:09
  • @audiFanatic is creating a shortcut with the 'Start in' field set to the absolute path to 'src' a viable option for you?
    – Cocowalla
    Jul 29, 2014 at 7:50
  • Well, the thing is I'm trying to move these files between computers over a shared drive, so I'm not sure that I can use an absolute path at all because I cannot guarantee that it'll be valid. Jul 29, 2014 at 12:35
0

In your script put the below command to the very beginnig of your CMD file:

echo "%cd%"

Then observe what this command produces.

If your "folder" directory's full path is "C:\ABC\folder" then echo "%cd%" command should yield the output "C:\ABC". However, if you see something different from this then your XCOPY command fires file not found error. This is normal because XCOPY cannot determine whether "folder" is directory or file and assume it is default to a file.

You must log in to answer this question.

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