• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

batch script

I need a batch script to move the last file/order file in a folder called "data" but we need to check the date created d time stamp using WMIC we need to go down to miliseconds


WMIC DATAFILE WHERE "PATH='\\Log\\'" GET Name, LastModified, name
2 Solutions
Paul TomasiCommented:
To get the last file created you need the following batch file code:

Note: It's the middle line that does all the work. This will set a variable 'lastfile' to the name of the last file created in the '\data\' folder.

@echo off
for /f %%a in ('dir \data\*.* /b /od /tc') do set lastfile=%%a
echo %lastfile%
Paul TomasiCommented:
You can get the date and time of the file with this follow-on batch file command:

for %%a in ("%lastfile%") do set datetime=%%~ta
echo %datetime%

CalmSoulAuthor Commented:
but I need the oldest file in the folder not the last file created
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Bill PrewCommented:
==> but I need the oldest file in the folder not the last file created

That can be accomplished easily via the following bit of code, it sorts in reverse order by date created, and the last file (oldest) name will be stored in the variable at the exit of the FOR loop.

@echo off
for /F "tokens=*" %%A in ('dir "c:\data\*.*" /b /o-d /tc') do set MoveFile=%%A
move "%MoveFile%" "d:\target"

Open in new window

Paul TomasiCommented:
I don't know who's confused here - me or you.

You wrote:

   "I need... to move the last file/order file..."

as in the last file created/modified - meaning the NEWEST file....

Then you wrote:

   "but I need the oldest file in the folder not the last file created"

Now you're saying the OLDEST file...

In the first statement you say you want the LAST file and in the second statement you say you DO NOT want the LAST file...

The OLDEST file in the folder is gotten by the following code:

   for /f "tokens=*" %%a in ('dir "\data\*.*" /a-d /b /od') do set OLDESTFILE=%%a
   move "%OLDESTFILE%" "Destination Folder"

(NOTE: change 'Destination Folder' to your target folder)
(NOTE: provide full path and filespec for '\data\*.*')

A few issues to be aware of.

(1) What if there are no files in the 'source' folder?

The first line of code will return an error messege "File Not Found" and the variable %OLDESTFILE% will either NOT be defined OR, it will retain it's previous value.

Also, the second line of code will return an error messege "The system cannot find the file specified".

So, you might need to test for this possibility.

(2) If you place the batch file itself in the source folder, then the batch file may be treated as part of your filespec.

So, you'll need to guard against that too. The simplest way is NOT to save your batch file in the same folder as your source files. I've known occasions when the batch file itself has been MOVEd and the whole thing just falls over.

(3) What if there are sub-folders in the source folder?

No probs, the '/a-d' which is part of the DIR command (see the code above) takes care of this by forcing DIR to ignore foldernames.

QlemoBatchelor and DeveloperCommented:
There are many issues with this question, so we cannot know if it is resolved at all, and which answer is correct:
The original question has the requirements of milliseconds precision and using WMIC. Both are not fulfilled by any post. The asker might have been stating those requirements out of error or lack of knowledge of other ways, but regarding milliseconds this is something not visible with CMD commands, though the sorting options work fine on 1 ms precision (if on NTFS, FAT has two seconds precision only).
It does not matter much if the sort order needs to be /o:-d or /o:d. Of course only one of both is correct, and according to the last author comment it is /o:-d.
Assuming WMIC is no requirement, both answers should be accepted with an equal split:
   http:#a34179615   Paul
   http:#a34182927   Bill

Last but not least I would have used this:
@echo off
set MoveFile=
for /F "tokens=*" %%A in ('dir "c:\data\*.*" /b /o:d /tc') do if not defined MoveFile set MoveFile=%%A
move "%MoveFile%" "d:\target"

Open in new window

for the newest file. For the oldest, just change /o:d to /o:-d (exactly vice versa to the option to use in the previous posts).
Paul TomasiCommented:

I don't think the owner wanted code to actually move the file.

Judging the owner's response, it appears he merely wanted to know how to get the name and (possibly) timefield of the oldest file so that he can use it with WMIC.

That being the case, the first two replies are all he needs to set him on his way.

There appears to have been some confusion regarding the owner's use of the terms LAST and OLDEST and I was therefore able to offer further assistance.

In his question, the owner states he wants:

   "...the last file..."

I think it's fair to say, most coders would interpret this as meaning the newest file. I trust you do agree on this.

As you are aware, in a FOR loop, the newest file is obtained with 'DIR /od', and this was how I coded my initial response.

The owner then stated:

   "...but I need the oldest file in the folder not the last file."

At this point, I was able to offer further assistance and in doing so, I was also able to clear up the confusion by way of explaining how we interpret terms such as last and oldest.

I thnk it's fair to say that even bill would have initially used 'DIR /od' and I am therefore, slightly worried that we may be losing sight of why the owner chose one particular reply instead of another - or indeed, multiple replies.

I really don't think it boils down to the correct syntax of DIR but, merely the fact that 'DIR /o' is used in combination with FOR and '%%~t...' (regardless of 'd' and '-d') to obtain the information he needs.

Of course, without the owners clarification, this is just conjecture.
Bill PrewCommented:
I think the owner did actually intend for a MOVE of the file, reinforced by a comment to that effect in this very similar thread.


I also think that the only reason milliseconds were asked for was thinking by the owner that it was the only way to find the actual oldest file needed.  Since we know that DIR will sort that out even though it doesn't display that precision, we steered the owner in that direction.

The owner clearly wanted the OLDEST file as they stated in a comment, and post 34182927 is the first and only place I see a solution offered that delivers that.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now