Page 1 of 1

Batch file with variables

Posted: 14 Mar 2008 12:11
by Dave
What I'm trying to do is create a batch file that will take the highest numbered file

eg.
Directory containing the files
Image 8.jpg
Image 9.jpg
Image 10.jpg
Image 11.jpg

and I want the batch to take the highest numbered file, in this case Image 11.JPG and move it to a new directory, then delete the rest. I have used xcopy in the form

XCOPY "C:\Test\Image *.jpg" "C:\Imaged\TEST.jpg /y

and it does exactly what I want, except it gives me a prompt if I want it as a (d)directory or (f)file? and I have to manually type F in order for it to complete, I need it to be totally automated. Any help is greatly appretiated!

Posted: 15 Mar 2008 20:16
by jaffamuffin
try

echo F | XCOPY "C:\Test\Image *.jpg" "C:\Imaged\TEST.jpg /y

Posted: 16 Mar 2008 05:57
by Chase
I'm not sure, but be careful with the order of file names there: It could be that "Image 10.jpg" is copied before "Image 1.jpg". Don't take my word for it, though :)

Posted: 19 Mar 2008 14:57
by vickster
I am newbie to scripting, so I cannot give the script at this point, but at first glance at the question I believe your going to have to read each filename in that directory and then second parse the file names. Once you parse the filename you will need to separate the filename and the extension and then likely parse again to isolate the numeric in the filename (first part)...well there is more then likely an easier solutions but this gives you some starting point.

Posted: 19 Mar 2008 20:18
by DosItHelp
Dave,

Looks like your XCOPY relies on the processing order of files.
The concern jaffamuffin mentioned is valid:
Your XCopy command in fact copies all files on top of each other and TEST.jpg ends up to be the last file copied.
If "Image 11.jpg" for some reason is not processed last then TEST.jpg will not be "Image 11.jpg".

If you don't want to give up the simplicity of your approach and if the file you want TEST.jpg to be is always the one with the latest time stamp then you can script a bit more save by using the /D flag with your XCOPY like this:

Code: Select all

echo F|XCOPY "C:\Test\Image *.jpg" "C:\Imaged\TEST.jpg /y /d

It ensures that TEST.jpg will end up being the file with the latest time stamp.

Hope this is useful :wink: