Solved Need to copy and rename a file based on two criteria

September 18, 2013 at 11:35:02
Specs: Windows Server 2008R2
I know how to do this based upon either of the criteria by itself, but Im not sure how to use them in conjunction.

I have an FTP folder where a vendor uploads some reports every night. I need to create a script that will select the most recently created report whose file name begins with "CustomExports_2" (the rest of the filename is a system generated time/date stamp).

I know to do a partial name match, you can do this "%CustomExports_2%" as the selector, but this would return several dozen files.

I know to do the date, you could use a token like this:
for /f "tokens="" %%a in ('dir /b /od *.csv 2^>NUL') do set lastmod=%%a
But that wouldn't filter out the other custom reports (i.e CustomExports_1).

How do I do these together, and make it poop out a file named "Output.csv"?


See More: Need to copy and rename a file based on two criteria

Report •

#1
September 18, 2013 at 12:16:51
✔ Best Answer
::====== script starts here ===============
::
:: cust.bat 2013-09-19 2:06:42.59
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b/o-d CustomExports_2*') do (
set outfile=%%a
goto :done
)
:done
echo !outfile!
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
September 18, 2013 at 12:23:48
Much appreciated! I just changed NEWFILE to Output.csv and it worked like a charm!

You are awesome!


Report •

#3
September 20, 2013 at 08:29:32
Ok, I spoke too soon. While this is copying a file with the naming parameter as described, it does not appear to be selected the newest (last modified) one to do it with. As a matter of fact, Im not sure how it is selected the one it is using. I put three .csv files of varying sizes and modification dates into a test directory and ran the script against them, and for some reason it appears to be consistently grabbing the middle one while ignoring the newest and the oldest ones.

Report •

Related Solutions

#4
September 20, 2013 at 09:42:56
Not obvious why.

Do this at the prompt and see if the latest is listed first:

dir/b/o-d CustomExports_2*

=====================
M2 Golden-Triangle


Report •

#5
September 20, 2013 at 17:05:34
MIght be spaces in filenames. You might also try removing the entire "delims= " modifier from the command:
for /f "tokens=*" %%a in ('dir/b/o-d CustomExports_2*') do (

Report •

Ask Question