Solved

batch script

Posted on 2010-11-19
11
637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 55

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
 
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 70

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 55

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

726 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