I have a batch file that runs when I build my solution in VS2019. The .bat file uses xcopy to copy .dlls from my project's Debug folder to another destination direction.

I want to use xcopy to copy the files to a subfolder in that destination directory. The only problem is, the name of the subfolder that I want to copy to will always be changing, since the subfolders in the destination directory are created whenever I do a new build for distribution to customers.

For example, right now the destination directory contains the following folders, which are named after the builds of all the previously distributed .dlls:


Today the destination directory will have a new subdirectory like so:


and I want xcopy to copy the latest .dll to the newly created subdirectory only.

I could either use the newest subdirectory date or the subdirectory build number. I looked through the list of xcopy switches and couldn't find one applicable to this situation. Is this possible?

  • 1
    Wrong tool. Instead of xcopy use robocopy, which is included in Windows. It has way more options than xcopy.
    – Robert
    Mar 25, 2022 at 21:36
  • Thanks Robert will have a look Mar 25, 2022 at 21:40
  • Use RoboCopy parameter /Mon:1, /XO, or /MinAge:1 (example)
    – JW0914
    Mar 26, 2022 at 0:39

1 Answer 1

@echo off 

cd/d "%~dp0" & setlocal

for /f delims^=* %%i in ('dir/o-d/ad/b^|findstr/e [0-9]
')do xcopy /duy ".\*.dll" "%%~fi" & endlocal & goto:eof

I understand that you are looking for an execution like this:

1. Identifies the last sub-directory created

for /f delims^=* %%i in ('dir/o-d/ad/b^|findstr/e [0-9]')do echo\ Last dir is: "%%~fi"

2. Copy only the most recent .dll files and if they already exist in the new destination directory:

xcopy /duy ".\*.dll" "%%~fi"

  • Obs. 1. See What does %~dp0 mean, and how does it work? because I'm assuming that your bat is (and will be executed) in the same folder where you have the .dlls files, otherwise replace "%~dp0" with the full path to the folder where you have your source .dlls to copy
@echo off 

cd/d "D:\The\Full\Path\To\Dlls\Folder" & setlocal

for /f delims^=* %%i in ('dir/o-d/ad/b^|findstr/e [0-9]
')do xcopy /duy ".\*.dll" "%%~fi" & endlocal & goto :eof

Copy files and/or directory trees to another folder. XCOPY is similar to 
the COPY command except that it has additional switches to specify both
the source and destination in detail.

While still included in Windows 10, XCOPY has been deprecated in favor of 
Robocopy, a more powerful copy tool, which is now built into Windows 
Server and Desktop operating systems [x]

      XCOPY source [destination] [options]

   Copy files changed on or after the specified date.
   If no date is given, copy only files whose
   source date/time is newer than the destination time.

/Y    Suppress prompt to confirm overwriting a file.

/L    List only - Display files that would be copied.

/U    Copy only files that already exist in destination.

  • Obs. 2. To test the execution, checking the command and not allowing any copying, you can try using xcopy with the /dul flag:
@echo off 

cd/d "D:\The\Full\Path\To\Dlls\Folder" & setlocal

for /f delims^=* %%i in ('dir/o-d/ad/b^|findstr/e [0-9]
')do xcopy /dul ".\*.dll" "%%~fi" & endlocal & goto :eof

Additional resources:

You must log in to answer this question.

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