Batch file with variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dave
Posts: 2
Joined: 30 Jan 2008 12:46

Batch file with variables

#1 Post by Dave » 14 Mar 2008 12:11

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!

jaffamuffin
Posts: 40
Joined: 25 Jan 2008 14:05

#2 Post by jaffamuffin » 15 Mar 2008 20:16

try

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

Chase
Posts: 4
Joined: 09 Mar 2008 12:27

#3 Post by Chase » 16 Mar 2008 05:57

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 :)

vickster
Posts: 1
Joined: 19 Mar 2008 14:45

#4 Post by vickster » 19 Mar 2008 14:57

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 19 Mar 2008 20:18

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:

Post Reply