|
Answer» How do i code a batch script to GET the most recent file from a folder. Also, I want to move this file to another location?
THANKS for the help.If you do a dir list, you can order it in reverse date order (newest at the top) - thus DIR /O-D > filelist.txt
This command puts the first line of a file (the newest filename) into the environment variable
SET /P NewestFile= Now you can do whatever you need with the filename
GrahamThanks for the reply.
didn't seem to work for me...
The contents in the filelist.txt are
Volume in drive C has no label. Volume Serial Number is 18C1-0B4A
Directory of c:\Copy1
03/25/2008 01:00 PM 0 3.dat 03/25/2008 10:08 AM 4 2.dat 2 File(s) 4 bytes 0 Dir(s) 52,907,216,896 bytes free
It is sorted by the date, but when I try to get the first file using "SET /P RecentFile=I fixed it..
I added the dir search with /b /s (dir /b /s /O-D *.dat >filelist.txt).
Thanks for the help again..Sorry, forgot the /B
well spotted !! GrahamCan you give the detailed code.Here is the tweaked version that ignores directory entries (and puts the temporary file in the TEMP directory)
Code: [Select]DIR /B /O-D /A-D>%TEMP%\filelist.txt SET /P NewestFile=<%TEMP%\filelist.txt ECHO %NewestFile% DEL %TEMP%\filelist.txt GrahamQuote from: cbvidya on March 25, 2008, 10:31:52 AM How do i code a batch script to get the most recent file from a folder. Also, I want to move this file to another location?
Thanks for the help.
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject") strFolder = "c:\test" Set objFolder = objFS.GetFolder(strFolder) strLatestFile = 0 For Each F In objFolder.Files d = F.DateLastModified diff= DateDiff("d",Now,d) If diff >= temp Then strLatestFile = F.Name End If Next WScript.Echo "Latest file is : " & strLatestFile ' move file objFS.MoveFile strLatestFile, "C:\temp\" & strLatestFile save as script.vbs and on command line c:\test> cscript /nologo script.vbs
|