Find most recently created sub directory Windows via batch file

1.3k Views Asked by At

I'm looking to create a Visual Studio post build script that uses xcopy or similar to copy the latest build files into another directory. I've created a batch script that returns the most recent filename, but haven't found a solution to find the most recent folder (latest build) within the builds folder.

Any advice or potentially another method to modify the build definition to output to two directories?

Path for each build is similar to below:

\server\x\Builds\x\xxx20141212.6\

\server\x\Builds\x\xxx20141212.5\

so I need some way to find and copy the latest folder (in this case xxx20141212.6).

Thanks :)

UPDATE:

Have managed to get it working :) Script posted below. It finds the most recent file OR folder based on date modified. In my case, there are only folders so this is fine.

@echo off

REM Find the directory containing the latest build

for /f "delims=" %%x in ('dir "\server1\x\x\" /od /b') do set latest=%%x

REM Mirror the latest folder onto server

robocopy "\server1\x\x\%latest%" "\server2\x\x\x" /MIR

The "delims=" allows it to find file names containing spaces. So with a folder "New Folder" it would return "New Folder" rather than just "New".

0

There are 0 best solutions below