开发者

how can i write a script to find the latest updated file and copy to certain directory

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.


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)


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 ?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜