Batch file help please

Soldato
Joined
14 Apr 2009
Posts
4,329
Location
Location: Location
Hi, I'm a noob to batch files and thought I'd learn by automating a task I have to do every month, but part of it is driving me mad and I'm sure there must be a simple way of doing it! :confused:

An export process copies a file to a location every month or so, each time a new export of the file is run it is saved to a new folder with the date on, the file name stays the same.

So the locations of the files would be something like:
G:\test\10-02-11\users.txt
G:\test\09-03-11\users.txt
G:\test\11-04-11\users.txt

I want to create a batch file that copies only the newest version of the users.txt file to another location, overwriting any existing file which is there.

I can't see how I can do it with copy or xcopy and get the newest version of only the file (no directories) unless I'm missing something, I thought about using dir and piping the result into copy but I can't get it to take only the first (newest) result after sorting (dir g:\test users.txt /a:-d /s /o:-d /b).

Points to note, I have no control of the export process and can't change that, also I can only run standard shell cmds, I don't have access to install other functions or use powershell / anything like that.

Like I said I am a noob at this and I'm using it as a chance to learn some basic scripting, but I'm a bit stuck. Any help or suggestions to point me in the right direction or some commands if something more complex is required would be much appreciated.:)

Cheers
 
Code:
@echo off
dir "G:\Exports" /a:-d /s /o:-d /b > "%temp%\temp.txt"
set /p newest=<"%temp%\temp.txt"
xcopy "%newest%" "G:\Somewhere New" /v /i /y
del "%temp%\temp.txt"

You should obviously change the path for the dir and xcopy commands. Don't take my word for it that it works, so make sure you test it! :p

Cheers SB I'll give that a go, on another note did you ever hear anything from the EOC guy? If not I wouldn't mind giving the original news script you were testing a go. :)
 
Cheers chaosophy it's good to see a different take on it. I've heard there is a lot you can do with for commands.

Once I've built up some good basic knowledge I'll look at powershell as well.
 
Back
Top Bottom