Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
how can i use a script to copy the newest file in a directory? i.e. I have a number of files with similar names (SCAN1,SCAN2,etc) the highest index is the newest file and there can be a maximum of 150 of these files. I dont want to copy all of them because they are fairly large and at some point there could be 150 of these files, but I just want the most recent one.
Thanks,J

Something along the line of doing a DIR SCAN* /o-d /b with the output redirected to a temp file or pipe. Then use batch commands to strip the first entry in the list and use it, as it will be oldest dated file.

This will do EXACTLY what you're after, here's the README from the D/L:
THE HANDY DANDY ANALOGX NEWMOVE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Have you ever needed to move only certain files out of a directory, based on their last modified date? Or have you wanted to move all the files out of a given directory, except for the latest three? If so, then you've finally got a utility to do just that (a not much else)!
AnalogX NewMove is about as straight-forward as any commandline utility ever is; from the command prompt you can type the name by itself to get a list of options:
F:\tools\newmove> newmove
AnalogX NewMove version 1.00 (Release)
The latest version can always be found at URL:
http://www.analogx.com/Usage: NEWMOVE [options]
Options:
/TARGET [path] Destination for files
/SOURCE [path] Source for files (def: current)
/COUNT [number] Number of files to leave (def: 1)
/JUSTCOPY Copies the files (does not move)
/MOVEOLD Move older files first (default)
/MOVENEW Move newer files firstHere's a brief description of each option:
TARGET This is the path where you want the files to be copied to. It supports both relative (..\) paths or literal paths (C:\). I would recommend using literal whenever possible to eliminate any possible ambiguity, unless you are comfortable with using relative paths.
SOURCE This is the directory it is going to be working in. If no source directory is specified, then it will use the current mounted path.
COUNT This is the number of files to actually leave, so if you told it to move newer files first, with a count of 5, it would leave the five OLDEST files. In contrast, if it was set to move the older files first, it would leave the five NEWEST files.
JUSTCOPY Use the COPY command instead of MOVE, leaving the source files intact where they are. By default the program uses MOVE.
MOVEOLD This specifies the order in which files are copied; the directory is sorted with the older files first, and it copies from older moving towards newer.
MOVENEW Exactly the same as MOVEOLD, except the files are sorted with newer files first, and it copies from newer moving towards older.
Pretty simple, eh? So here is a quick example of one of the more common uses of this utility.
Let's say you have a directory which stores log files for something like IIS (set to create a new file each day), and you perform analysis using Analog of the last week (7 days) of activity. Now, you don't want Analog to process the log files for anything prior to 7 days (sure, you can set this in Analog's config file, but it still reads through the log files, which takes time). Here's the command to tell it to move anything that's older than 7 days:
NEWMOVE /TARGET C:\logs\archive /COUNT 7 /MOVEOLD
This assumes it's executed in the directory where the logs exist, it then reads the directory, sorts it with the older files first, and moves all but 7 files into the directory C:\logs\archive.
Direct link to details & D/L page is in "HomePage" above.
Works like a charm!Baldy.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |