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
 
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. :)
 
He never got back to me. I will email him again and see if I can find out what happened.

I need to take another look at my news script and see what state I left it in! :p
 
Set is for creating variables.

So:
Code:
set variablename="something"

Set /p is for getting user input. However, a nifty trick is to use a text file as input, as it takes the first line of the text file and assigns it to the variable.

Batch has no head command, like Unix, so it can be difficult to get the first result from the output of another command. You could have done a for loop, but set /p is quick and simple!
 
Have just started playing with powershell recently for work and agree it is a lot more interesting and would be ideal for something like this. As it would build the list of files completely before the sort allowing you to properly sort on the files date. Where as here all you are doing is getting the file from the last folder created. OK in this instance that amounts to the same thing but is technically different. :P

Anyway my stab at it...

Code:
SETLOCAL ENABLEEXTENSIONS

FOR /F %%A IN ('DIR "g:\test\users.txt" /S /T:C /O:-D /B') DO (
	xcopy "%%A" "g:\NewFolder" /v /i /y
	GOTO:EOF
)

ENDLOCAL
 
Last edited:
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.
 
Almost nothing in batch is transferable to PowerShell! :p

My batch skills are pretty basic as I tend to favour PS and bodge together batch scripts on the rare occassion I need them [or when something is just simpler as a batch script! :)]

However, you can run native commands within a PS console, so you can still use batch commands. Some of the PS commands have aliases to common CMD and even Unix names [dir and ls for example are aliases of PS's Get-ChildItem cmdlet].
 
Back
Top Bottom