Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Help with a batch file xcopy

- - - - - remxcopy

  • Please log in to reply
10 replies to this topic

#1
sam240

sam240
  • Member
  • 2 posts
  • Joined 16-April 15
  • OS:Windows 7 x64
  • Country: Country Flag

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, 16 April 2015 - 01:07 PM.



How to remove advertisement from MSFN

#2
submix8c

submix8c

    Inconceivable!

  • Patrons
  • 4,479 posts
  • Joined 14-September 05
  • OS:none specified
  • Country: Country Flag

"%date% is a system variable. Why are you trying to "change" it"?

Use a different variable e.g. "Set DATEX=<and the remainder>" and use the DATEX in the other lines.


  • sam240 likes this

Someday the tyrants will be unthroned... Jason "Jay" Chasteen; RIP, bro!

Posted Image


#3
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,299 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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, 16 April 2015 - 01:19 PM.

  • sam240 likes this

#4
submix8c

submix8c

    Inconceivable!

  • Patrons
  • 4,479 posts
  • Joined 14-September 05
  • OS:none specified
  • Country: Country Flag

Meh... you explained additional stuff so... :thumbup


Someday the tyrants will be unthroned... Jason "Jay" Chasteen; RIP, bro!

Posted Image


#5
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,239 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
If you really need a Custom Date for your batch, try this batch
 
@Echo Off
CLS
MODE 75, 15
COLOR F2
TITLE Passing VBS Date To Cmd

Set Vbs=Test.vbs
::Create VBS File With Date Information
Echo Dim Fso :Set Fso = Createobject("Scripting.FileSystemObject") > %Vbs%
Echo Dim D, M, Y, Ts :D=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.cmd
Del %Vbs%
Del Temp.cmd
Echo.
Echo Custom Date : %MyDate%
Pause



GunSmokingMan



#6
sam240

sam240
  • Member
  • 2 posts
  • Joined 16-April 15
  • OS:Windows 7 x64
  • Country: Country Flag

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, 16 April 2015 - 02:57 PM.


#7
DosProbie

DosProbie

    MSFN Expert

  • MSFN Sponsor
  • 1,008 posts
  • Joined 16-October 12
  • OS:Windows 8.1 x64
  • Country: Country Flag

Donator

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, Yesterday, 04:24 AM.


#8
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,069 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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.



#9
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,299 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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


  • DosProbie likes this

#10
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,069 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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.



#11
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,928 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag

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


Posted Image





2 user(s) are reading this topic

1 members, 1 guests, 0 anonymous users


    gunsmokingman