Jump to content

Help with a batch file xcopy


Recommended Posts

Hello

 

Could someone please help me fix this batch script below. I would really apperciate your help.

 

We use imagex from microsoft for our imaging. This batch script copies captures the image I prepare which works fine. The 3 steps that don't work are "REM" lines in the script. 

 

First REM should use the name as the current date

Second REM should copy the previous image.wim file to an archive folder before deleting

Third REM should delete the image.

 

The way it works right now is the new image.wim file just gets overwritten on the previous image.wim and the script doesn't copy the previous image.wim file into the achrive folder before overwritting. Achrive is required just in case we need to go back to the old image.

This is a windows 7 image. Please let me know if you need any additional information.

 

 

echo off
Title Capturing Laptop Base Image
REM SET date="%date:~10,4%-%date:~4,2%-%date:~7,2%"
REM START /wait xcopy i:\lenovo\base\blank\image.wim i:\lenovo\base\blank\archive\%date%\
REM START /wait del i:\lenovo\base\blank\image.wim

cls
imagex /compress fast /capture D: i:\lenovo\base\blank\image.wim "Laptop Base Image" /verify
PAUSE
exit

Edited by sam240
Link to comment
Share on other sites


Well, you cannot really really set "date" to a rearranged part of itself, "date" is an internal variable, you cannot redefine it ...

 

Try with:

SET MYdate=="%date:~10,4%-%date:~4,2%-%date:~7,2%"

START /wait xcopy i:\lenovo\base\blank\image.wim i:\lenovo\base\blank\archive\%MYdate%\

 

I am also not much convinced of the actual need to use XCOPY nor about needing to use START /WAIT (not even for the del command), you can simply MOVE the file, or if the idea is to first thing COPY it, then verify that the copy was successful (let's say by hashing both source and target) before DELeting the source, the verifying part is completely missing.

 

jaclaz

 

Edit: Ooops, crossposting with submix8c  :blushing:

Edited by jaclaz
  • Upvote 1
Link to comment
Share on other sites

If you really need a Custom Date for your batch, try this batch

 

@Echo OffCLSMODE 75, 15COLOR F2TITLE Passing VBS Date To CmdSet Vbs=Test.vbs::Create VBS File With Date InformationEcho Dim Fso :Set Fso = Createobject("Scripting.FileSystemObject") > %Vbs%Echo Dim D, M, Y, Ts =Day(Date) :M=Month(Date) :Y=Year(Date) >> %Vbs%Echo Set Ts = Fso.CreateTextFile("Temp.cmd") >> %Vbs%Echo  If Len(M) = 1 Then M = "0" ^& M >> %Vbs%Echo  If Len(D) = 1 Then D = "0" ^& D >> %Vbs%Echo Ts.WriteLine "Set MyDate=" ^& Y ^& "/" ^& M ^& "/" ^&  D >> %Vbs%Echo Ts.Close >> %Vbs%Call %Vbs%Call Temp.cmdDel %Vbs%Del Temp.cmdEcho.Echo Custom Date : %MyDate%Pause
Link to comment
Share on other sites

Well, you cannot really really set "date" to a rearranged part of itself, "date" is an internal variable, you cannot redefine it ...

 

Try with:

SET MYdate=="%date:~10,4%-%date:~4,2%-%date:~7,2%"

START /wait xcopy i:\lenovo\base\blank\image.wim i:\lenovo\base\blank\archive\%MYdate%\

 

I am also not much convinced of the actual need to use XCOPY nor about needing to use START /WAIT (not even for the del command), you can simply MOVE the file, or if the idea is to first thing COPY it, then verify that the copy was successful (let's say by hashing both source and target) before DELeting the source, the verifying part is completely missing.

 

jaclaz

 

Edit: Ooops, crossposting with submix8c  :blushing:

 

Thank you! I'll give this a try. I don't mind just moving the file instead. What command should I use for moving instead of xcopy? Also I forgot to mention, I do not no nothing about programing or batch script lol I just do this for fun and to make my life easier at work.

Edited by sam240
Link to comment
Share on other sites

I added a 'Make Directory' Command along with the 'Move' command in case the dated directory was not already created first else you will get a 'cannot find specified path error' and your file will not be moved over.

~DP


Set MYdate="%date:~0,3%_%date:~4,2%-%date:~7,2%-%date:~10,4%"
MD "i:\lenovo\base\blank\archive\%MYdate%"
Move "i:\lenovo\base\blank\image.wim" "i:\lenovo\base\blank\archive\%MYdate%\"


Edited by DosProbie
Link to comment
Share on other sites

XCopy was superseded by RoboCopy:

@SETLOCAL@SET "SRC=I:\lenovo\base\blank"@SET "DST=I:\lenovo\base\blank\archive\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"@SET "WIM=image.wim"@ROBOCOPY "%SRC%" "%DST%" "%WIM%" /Z /J /MOV

This should move the file a little more robustly.

Link to comment
Share on other sites

Well, to be fair there is no real need for robocopy, the file is before and after on a same volume, MOVE (which will simply update the path of the file should be robust enough, or if you prefer in the given scenario there is not actually any "data transfer".

 

But, even before that, it is the procedure that in itself can be simplified.

 

The base assumption is that the "last previous" image is created in a "nameless folder", i.e. "I:\lenovo\base\blank" and when the batch is run the pre-existing image in the "nameless" folder is moved to a new, "named after date" subfolder, and then a new image is created in the same "nameless" folder.

 

Wouldn't it be easier to simply create the image (also the "first" one) directly in a ""named after date" subfolder?

I.e. all in all, something *like* :

SET Today=%date:~10,4%-%date:~4,2%-%date:~7,2%IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%imagex /compress fast /capture D: i:\lenovo\base\blank\%Today%\image.wim "Laptop Base Image" /verify

Please note how this way the "named after date" folder will contain an image taken on the same date of the name of the folder, whilst in the original implementation the "named after date" folder would have contained "the image that was taken last time BEFORE the date in the name of the folder"

 

 

jaclaz

  • Upvote 2
Link to comment
Share on other sites

Well, to be fair there is no real need for robocopy, the file is before and after on a same volume, MOVE (which will simply update the path of the file should be robust enough, or if you prefer in the given scenario there is not actually any "data transfer".

Agreed, if on the rare occasion that the OP has stated the actual paths.

 

The intent of my post remains, to show that the command used in the subject title is deprecated so if there is a need not to use the move or copy commands then why not use robocopy.

Link to comment
Share on other sites

The intent of my post remains, to show that the command used in the subject title is deprecated so if there is a need not to use the move or copy commands then why not use robocopy.

 

Very true.  Or as jaclaz suggested, always store the image in a dated folder, then no copy or move is needed at all.

 

Cheers and Regards

Link to comment
Share on other sites

Well, to be fair there is no real need for robocopy, the file is before and after on a same volume, MOVE (which will simply update the path of the file should be robust enough, or if you prefer in the given scenario there is not actually any "data transfer".

 

But, even before that, it is the procedure that in itself can be simplified.

 

The base assumption is that the "last previous" image is created in a "nameless folder", i.e. "I:\lenovo\base\blank" and when the batch is run the pre-existing image in the "nameless" folder is moved to a new, "named after date" subfolder, and then a new image is created in the same "nameless" folder.

 

Wouldn't it be easier to simply create the image (also the "first" one) directly in a ""named after date" subfolder?

I.e. all in all, something *like* :

SET Today=%date:~10,4%-%date:~4,2%-%date:~7,2%IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%imagex /compress fast /capture D: i:\lenovo\base\blank\%Today%\image.wim "Laptop Base Image" /verify

Please note how this way the "named after date" folder will contain an image taken on the same date of the name of the folder, whilst in the original implementation the "named after date" folder would have contained "the image that was taken last time BEFORE the date in the name of the folder"

 

 

jaclaz

I like the idea,

 

The only problem with this is that I have another batch file which deploys the captured image and that script pulls from the main folder such as i:\lenovo\base\blank\image.wim so say now the new image is in %Today% folder, I'll have to manually edit the script each time or copy and paste the image outside of the %Today% folder.

Link to comment
Share on other sites

The only problem with this is that I have another batch file which deploys the captured image and that script pulls from the main folder such as i:\lenovo\base\blank\image.wim so say now the new image is in %Today% folder, I'll have to manually edit the script each time or copy and paste the image outside of the %Today% folder.

 

Not really-really.

 

I mean there are several possibilities, one being that the batch (the one that creates the image in the %today% folder) also  creates a hard link (I presume that you are using NTFS) in the main directory or, possibly even better/easier as hardlinks are a bit more difficult to manage, use a softlink (to the file) or a junction point from the %today% directory to a "fixed" place, let's say i:\lenovo\base\blank\last\

http://ss64.com/nt/mklink.html

http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html

 

The poor man's way (or on non NTFS) would be to have the "imaging batch" write a simple plain text file, let's say  last_to_be_deployed.ini and have the "deploy batch" to parse it and get the actual value of the %today%, i.e. the folder name from which to get the image, or you could get rid of the subdirectories have all the images in i:\lenovo\base\blank\ and assign a meaningful name to the images themselves, like naming the actual image to %today%.img and have the deploying batch use last created one (which would be another advantage as you wouldn't have a zillion of files all named "image.img" and only differentiated - besides creation timestamp - only by the name of the folder in which they are).

 

There are endless possibilities.

 

jaclaz

Link to comment
Share on other sites

 

The only problem with this is that I have another batch file which deploys the captured image and that script pulls from the main folder such as i:\lenovo\base\blank\image.wim so say now the new image is in %Today% folder, I'll have to manually edit the script each time or copy and paste the image outside of the %Today% folder.

 

Not really-really.

 

I mean there are several possibilities, one being that the batch (the one that creates the image in the %today% folder) also  creates a hard link (I presume that you are using NTFS) in the main directory or, possibly even better/easier as hardlinks are a bit more difficult to manage, use a softlink (to the file) or a junction point from the %today% directory to a "fixed" place, let's say i:\lenovo\base\blank\last\

http://ss64.com/nt/mklink.html

http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html

 

The poor man's way (or on non NTFS) would be to have the "imaging batch" write a simple plain text file, let's say  last_to_be_deployed.ini and have the "deploy batch" to parse it and get the actual value of the %today%, i.e. the folder name from which to get the image, or you could get rid of the subdirectories have all the images in i:\lenovo\base\blank\ and assign a meaningful name to the images themselves, like naming the actual image to %today%.img and have the deploying batch use last created one (which would be another advantage as you wouldn't have a zillion of files all named "image.img" and only differentiated - besides creation timestamp - only by the name of the folder in which they are).

 

There are endless possibilities.

 

jaclaz

 

Thank you for your help.

 

this is what my script looks like for deploying

"imagex /apply i:\lenovo\domain\blank\image.wim"

 

and to be really honest, I have no expierence in scripting which is why I'm here. I tried your script above and it worked beautifully. It created a folder with today's date and create the new image in it.

Edited by sam240
Link to comment
Share on other sites

Well,

you can have in the "deploy" batch (say):

  

SET Today=FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\domain\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%AIF NOT DEFINED Today ECHO ERROR...&PAUSE"imagex /apply i:\lenovo\domain\blank\%today%\image.wim"
 

and have in the "imaging" one: 

SET Today=%date:~10,4%-%date:~4,2%-%date:~7,2%IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%ECHO TheLast=%Today%>i:\lenovo\base\blank\last.iniimagex /compress fast /capture D: i:\lenovo\base\blank\%Today%\image.wim "Laptop Base Image"
jaclaz
  • Upvote 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...