Solved

batch script

Posted on 2010-11-19
11
629 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
ID: 34179615
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
ID: 34179627
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
ID: 34181829
but I need the oldest file in the folder not the last file created
0
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34182927
==> 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 11

Expert Comment

by:paultomasi
ID: 34186266
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
ID: 34223579
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
ID: 34223871
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 52

Expert Comment

by:Bill Prew
ID: 34224006
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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 …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

16 Experts available now in Live!

Get 1:1 Help Now