How can I copy files to the “newest subdir” in a directory using a batch file?

StackOverflow https://stackoverflow.com/questions/848536

  •  21-08-2019
  •  | 
  •  

Question

I need to copy files from recent build folder to another folder used for testing. I'm having a hard time getting the name of the most recent build folder.

My current attempt is this:

@for /D %%i in ('dir e:\builds\projectA\* /O:D') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s 

I was hoping target would be the newly created folder from which I could then copy the files from. However, when I echo target to the console it just says:

/O:D'

Does anyone know how I can get this to work (or know of an alternative)?

Was it helpful?

Solution

Replace the /D with /F and add /B to the bracketed dir command.

@for /F %%i in ('dir e:\builds\projectA\* /O:D /B') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s

OTHER TIPS

pushd E:\builds\projectA
for /f "delims=" %%d in ('dir /b /a:d /o:d') do @echo %%d>latest.txt
for /f "delims=" %%l in (latest.txt) do xcopy "%%l\*.*" \\devbox\projectA /y /s
del latest.txt
popd
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top