C# · Dotnet · General · Tools

Copy files from latest modified folders – Batch script

Hello Reader,

Part of a tool making process, i had a sub task, where i need to copy certain files from a few (in this case 3 in numbers) directories from recently modified directories (date-time descending sort) onto another path where again a latest single directory needs to be searched, under which the predefined files are copied to.

In other words and detailed: A build machine runs its scheduled job, creates few files (if failure) in a 3 different folders: components/modules wise, the names are not in fixed structure. This central repository shall have daily 3 folders added into it.

Hence, i was in need of a batch script where it gets the latest (descending sorted) modified 3 folders and inside each of them, search for few files (pre-defined), when found, does a blind copy onto a destination path: where again with a date-time descending sort, latest folder is found (just 1); which i consider as Output folder of all logs.

Later on, another tool looks for these few pre-defined files to determine the failure condition of the build and generates few reports. WIth few hours of looking all over google, i found very minimal solution available nor suitable as per my needs and then reading quite a good documents of windows commands and understanding it, i could come up with below script.


Code description:

Line 4, is required for the below FOR loop where in we need to retain the state of the variables and update it all through the loop.

Line 12, is the loop where in i am using /f argument to iterate over the out of the command executed in the loop header, in this case its DIR command. In this case, i need only the Folders which are non-hidden and non-system folders needs to be sorted in descending order based on modified time. Hence the argument /ad-h-s /TW /O-d. More details can be found in the help document of Dir command.
Line 12, loop shall first find out the single latest directory in the destination path where a files needs to be copied out.

Line 19, loop sorts the source directory contents (folders only) in descending order based on modified datetime.  Then picks up first 3 top directories from it and copies files (if present) inside these 3 folders to destination directory found from loop in Line 12.
Since the state of the loopcount is retained via delayedexpansion concept, the accessing of the variable is done with !<variable>! than the normal %<variable>% way at Line 21. If done via traditional way, then the variable value when accessed wont be having the latest value in it.

Cheers! Zen


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s