Solved

batch script

Posted on 2010-11-19
11
628 Views
Last Modified: 2012-05-10
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

example:

WMIC DATAFILE WHERE "PATH='\\Log\\'" GET Name, LastModified, name
0
Comment
Question by:CalmSoul
11 Comments
 
LVL 11

Accepted Solution

by:
paultomasi earned 250 total points
Comment Utility
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%
0
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
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%

0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
but I need the oldest file in the folder not the last file created
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
Comment Utility
==> 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

~bp
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
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.

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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).
0
 
LVL 11

Expert Comment

by:paultomasi
Comment Utility
Qlemo

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.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_26627476.html

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.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now