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

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

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy