views:

40

answers:

4

i have a process that downloads a file from a webbrower. it has the same name always (can't change that) so each file gets downloaded as file([latestnumber])

so in this directory i have:

joe.pdf
joe(1).pdf
joe(2).pdf
etc . . .

I now would like a script to take the "latest file" (joe(2).pdf in this case) and copy it to another directory.

something like GetLatestFile("joe") and copy to "X:\mydirectory"

can anyone think of an easy way to do this.

+1  A: 

Do you have a preference as to what language you write your script in?

I wouldn't go by the name of the file, I'd choose whatever scripting language you are going to use, loop through the directory and look at the file attributes for each file to pick out the latest one, then move it to your target directory. This would be fairly trivial in a .NET console application with the classes available in the System.IO namespace. (namely the DirectoryInfo, FileInfo and File classes)

jaltiere
@jaltiere - i wanted to see if i could do this in a .bat script but i am open to any language
ooo
I'm sure that this is possible with a .bat script, C# would be my first choice though. (probably because that's what I work with the most)
jaltiere
+1  A: 

Hi,

Try this: XCOPY C:\BATCH\*.* C:\UPLOAD /M

Put the code in a text file and rename it as whateveryouwant.bat and execute.

Be sure to edit the source and destination folder to your liking.

Is this what you're looking for ?

Trefex
I think he only wants to copy the latest file, not all of them.
jaltiere
@Trefex - i only want to copy the latest file (not all of them)
ooo
This will only copy all the first time.After that it will always copy only the latest ones. Not good?
Trefex
@Trefex - there is a whole bunch of other stuff in that directory that i do not want to copy over
ooo
Mhhh, XCOPY C:\BATCH\joe*.pdf C:\UPLOAD /Mlike this ?
Trefex
+1  A: 

So, as it is enough to get the latest filename sorted by date, I suggest something like:

@echo off & setLocal enabledelayedexpansion

for /f "tokens=* delims= " %%a in ('dir /b/a-d/o-d') do (
set N=%%~Fa
goto :done
)
:done
echo !N!

Replace the last echo command for the "copy ..." or whatever you want to do with the newest file.

HTH!

Edit> If the files are not in the current directory, change the "dir" command accordingly

belisarius
+1  A: 

this uses sed, and regular expressions http://gnuwin32.sourceforge.net/packages/sed.htm

it generates a bat file that does the job. i've put the bat file in c:\crp so it doesn't become a latest file.

as a demonstration, i've created a latest file latestfile.txt

you can see the line that generates copyit.bat and you can amend it so the files goes exactly where you want.

C:>md c:\crp

C:>copy /y con latestfile.txt

fgfdgd^Z

1 file(s) copied.

C:>dir /o-d/a-d/b | find /N /V "QWERTY" | find "[1]" | sed -e s/[1](.*)/cop y\d32\1\d32c:\newdir/>c:\crp\copyit.bat

C:>type c:\crp\copyit.bat

copy latestfile.txt c:\newdir

barlop