I have one folder on my desktop Test, and in this folder I have two folders, WorkingFolder\Bilder and Station 1. So the structure looks like this:

Test\Station 1

Within Test\ (the main folder) I have the batch file. I need to be able to move or copy the .jpg files from Test\Station 1 into Test\WorkingFolder\Bilder. For this I have been using XCopy.

The problem is that I CANNOT use the name of the copy-from folder in XCOPY, because the batch file will be used on several similarily structured folders, but with different path names. So I have been trying to use %~dp0 instead:
Explaining Image

My code is:

md WorkingFolder\Bilder
xcopy /s "%~dp0\*.jpg"  WorkingFolder\Bilder\

What I try to write is, copy all .jpg within the subfolders of %~dp0, %~dp0 being the folder where the batch file is in, and its subfolder where the images are located Station 1, and put them in WorkingFolder\Bilder.

Problem is no files are copied. Only when I DIRECTLY refer to Station 1 in Xcopy, then the files are copied. I need to be able to refer to all subdirectories to %~dp0 and not like subdirectories to %~dp0\example.

Please help me!

  • Not a solution, but you forgot to include %~dp0 in your destination directory...
    – aschipfl
    Sep 30, 2019 at 11:40

1 Answer 1


I believe that xcopy reports the following error:

Cannot perform a cyclic copy

This happens because the destination directory is located in the source directory tree.

You could try to use the robocopy command instead and exclude the destination directory:

robocopy "%~dp0." "%~dp0WorkingFolder\Builder" "*.jpg" /S /XD "%~dp0WorkingFolder"

You must log in to answer this question.

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