Batch file command to copy files from one location/directory to another location based on condition

batch-file

Scenario:

I want to copy the particular file to another locaton.

Destination folder path: D:\Correct\email

Source folder path: C:\Revert\email.

Here in email folder there will be many subfolders and each subfolder contains exactly two files like abc.csv and xyz.csv
so I want to copy the file abc.csv from the latest created folder(ie in the subfolders of email folder) to destination folder and need not to know the name of subfolder.

Thank you in advance.

Best Answer

You can get a sorted list of directories with

dir /B /TC /O-D /AD

You can get the first result into a variable with

for /f "usebackq delims=" %%D in (`dir /B /TC /O-D /AD C:\Revert\email`) do if not defined Newest set Newest=%%~fD

You can then copy the files with

copy /Y %Newest%\*.csv D:\Correct\email