Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

Help with a batch file xcopy


Recommended Posts

Well,

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

  

FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\domain\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%A
 

and have in the "imaging" one: 

IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%ECHO TheLast=%Today%>i:\lenovo\base\blank\last.ini
jaclaz

 

 

change to

FOR /F "tokens=2 %delims==" %%A IN ('FIND "TheLast"^<i:\lenovo\domain\blank\last.ini') DO SET Today=%%A

and

IF NOT EXIST i:\lenovo\base\blank\%Today%\ MD i:\lenovo\base\blank\%Today%>i:\lenovo\base\blank\last.ini ECHO;TheLast=%Today%
  • Upvote 1
Link to comment
Share on other sites


Along with jaclaz idea, a symlink could be created as well. May as well create a symlink for static path handling and use the last.ini for reference to the last date. The use of a static path can help with WAIK tools or similar and less maintenance of suggested code in other scripts that also access the image file.

 

This code is not tested so let me know if issues exist so I can fix it. I expect it to work though I have been wrong before.

@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONrem # Set the date to a variable named Today.SET Today=%date:~10,4%-%date:~4,2%-%date:~7,2%rem # Make a folder for Today.IF NOT EXIST i:\lenovo\base\blank\%Today%\ MD i:\lenovo\base\blank\%Today%rem # Capture the image to Todays folder.imagex /compress fast /capture D: i:\lenovo\base\blank\%Today%\image.wim "Laptop Base Image"rem # Write Todays date to last.ini and symlink the image if no error detected from imagex.IF ERRORLEVEL 1 (	rem # Output the detection of an imagex error.	>&2 ECHO Error !errorlevel! set by imagex.	>&2 IF EXIST i:\lenovo\base\blank\%Today%\image.wim ECHO Image does exist in Todays directory.) ELSE (	rem # Write the last date to last.ini.	(ECHO TheLast=%Today%)>i:\lenovo\base\blank\last.ini		rem # Remove the last symlink as mklink does not overwrite.	IF EXIST i:\lenovo\base\blank\image.wim DEL i:\lenovo\base\blank\image.wim		rem # Create symlink in the blank directory to Todays image.	mklink i:\lenovo\base\blank\image.wim i:\lenovo\base\blank\%Today%\image.wim	IF ERRORLEVEL 1 >&2 ECHO Error !errorlevel! set by mklink.)ENDLOCALGOTO :EOF

Edit: Added ENABLEDELAYEDEXPANSION to expand errorlevel variables used between () rounded braces. This affects only error message output displaying the value of the variable of errorlevel though is still misleading if error output is always seen as an unset value or as 0. Note, Reference of errorlevel variable is %errorlevel% changed to !errorlevel!.

 

As per usual for testing, add the word PAUSE on a separate line immediately before or after the use of ENDLOCAL to pause the script and see any error output if issues occur.

Edited by MHz
  • Upvote 1
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

 

 

 

 

Well,

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

  

FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\domain\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%A
 

and have in the "imaging" one: 

IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%ECHO TheLast=%Today%>i:\lenovo\base\blank\last.ini
jaclaz

 

 

change to

FOR /F "tokens=2 %delims==" %%A IN ('FIND "TheLast"^<i:\lenovo\domain\blank\last.ini') DO SET Today=%%A

and

IF NOT EXIST i:\lenovo\base\blank\%Today%\ MD i:\lenovo\base\blank\%Today%>i:\lenovo\base\blank\last.ini ECHO;TheLast=%Today%

 

 

 

Along with jaclaz idea, a symlink could be created as well. May as well create a symlink for static path handling and use the last.ini for reference to the last date. The use of a static path can help with WAIK tools or similar and less maintenance of suggested code in other scripts that also access the image file.

 

This code is not tested so let me know if issues exist so I can fix it. I expect it to work though I have been wrong before.

@ECHO OFFSETLOCALrem # Set the date to a variable named Today.SET Today=%date:~10,4%-%date:~4,2%-%date:~7,2%rem # Make a folder for Today.IF NOT EXIST i:\lenovo\base\blank\%Today%\ MD i:\lenovo\base\blank\%Today%rem # Capture the image to Todays folder.imagex /compress fast /capture D: i:\lenovo\base\blank\%Today%\image.wim "Laptop Base Image"rem # Write Todays date to last.ini and symlink the image if no error detected from imagex.IF ERRORLEVEL 1 (	rem # Output the detection of an imagex error.	>&2 ECHO Error %errorlevel% set by imagex.	>&2 IF EXIST i:\lenovo\base\blank\%Today%\image.wim ECHO Image does exist in Todays directory.) ELSE (	rem # Write the last date to last.ini.	(ECHO TheLast=%Today%)>i:\lenovo\base\blank\last.ini		rem # Remove the last symlink as mklink does not overwrite.	IF EXIST i:\lenovo\base\blank\image.wim DEL i:\lenovo\base\blank\image.wim		rem # Create symlink in the blank directory to Todays image.	mklink i:\lenovo\base\blank\image.wim i:\lenovo\base\blank\%Today%\image.wim	IF ERRORLEVEL 1 >&2 ECHO Error %errorlevel% set by mklink.)ENDLOCALGOTO :EOF

You guys are freaking genius! This made my life so much easier. Thank you so much jaclaz for the last.ini idea and thanks yzowl for tweaking it and also thank you MHZ for tweaking it. I did try jaclaz's script and that worked I haven't tried MHZ's script yet. I'll give it show tomorrow and get back to you. Each image takes a little time to capture so testing takes a while for each script.

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"
jaclaz

 

 

 

 

Well,

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

  

FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\domain\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%A
 

and have in the "imaging" one: 

IF NOT EXIST i:\lenovo\base\blank\%Today% MD i:\lenovo\base\blank\%Today%ECHO TheLast=%Today%>i:\lenovo\base\blank\last.ini
jaclaz

 

 

change to

FOR /F "tokens=2 %delims==" %%A IN ('FIND "TheLast"^<i:\lenovo\domain\blank\last.ini') DO SET Today=%%A

When I used the

 

SET Today=

FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\domain\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%A

IF NOT DEFINED Today ECHO ERROR...&PAUSE

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

 

I got an error which says " the syntax of the command is incorrect".

This is exactly what my script is like. Upto diskpart /s diskpartscript2.txt it works fine right after the cls, it gives me the syntax of the command is incorrect error. I tried both jaclaz's and Yzowl's

 

I was able to capture the image and it created an last.ini in this directory i:\lenovo\base\blank\last.ini and also created a folder with the image.wim inside the folder with date.

 

 

echo off

Title Deploying Laptop Base Image

diskpart /s diskpartscript2.txt

cls

SET Today=

FOR /F "tokens=2 %delims==" %A IN ('TYPE i:\lenovo\base\blank\last.ini ^| FIND "TheLast"') DO SET Today=%%A

IF NOT DEFINED Today ECHO ERROR...&PAUSE

imagex /apply i:\lenovo\base\blank\%today%\image.wim W:\

bcdboot W:\Windows /s S:

PAUSE

exit

 

 

Link to comment
Share on other sites

There are a couple typos here:

FOR /F "tokens=2 %delims==" %A 

should be:

FOR /F "tokens=2 delims==" %%A 

 

The snippets posted are not "tested and verified" batches, they are examples/ideas and should be double checked, particularly for (stupid, my bad :blushing: ) typos.

 

jaclaz

  • Upvote 1
Link to comment
Share on other sites

Updated my last post with an edit.

...

Edit: Added ENABLEDELAYEDEXPANSION to expand errorlevel variables used between () rounded braces. This affects only error message output displaying the value of the variable of errorlevel though is still misleading if error output is always seen as an unset value or as 0. Note, Reference of errorlevel variable is %errorlevel% changed to !errorlevel!.

 

As per usual for testing, add the word PAUSE on a separate line immediately before or after the use of ENDLOCAL to pause the script and see any error output if issues occur.

One of the rare times that I chose to omit ENABLEDELAYEDEXPANSION when it was actually needed. Hey, perhaps I deserve this threads award for a simple yet stupid mistake.

  • Upvote 1
Link to comment
Share on other sites

sam240, you simply had to replace the three lines from jaclaz' two codes with my versions to fix his three issues.

I did that very first and that's when I got the error. So I then I thought to replace it with jaclaz and still had the issue. I'm going to try it now and see if it works.

Link to comment
Share on other sites

As jaclaz mentioned I just had to remove "%" from front of delims. This seems to work but now but I think it's not recognizing %today% from the imagex line. I get the following msg from imagex

IMG_0300.jpg

 

 

echo off
Title Deploying Laptop Base Image
diskpart /s diskpartscript2.txt
cls
SET Today=
FOR /F "tokens=2 delims==" %%A IN ('FIND "TheLast"^<i:\lenovo\base\blank\last.ini') DO SET Today=%%A
IF NOT DEFINED Today ECHO ERROR...&PAUSE
imagex /apply i:\lenovo\base\blank\%today%\image.wim W:\
bcdboot W:\Windows /s S:
PAUSE
exit
 

Edited by sam240
Link to comment
Share on other sites

sam240,

 

Looks like your missng a parameter for imagex /apply. Look at the example at the bottom of the screen. 1 is used to set the image_number.

Try changing it to:

imagex /apply i:\lenovo\base\blank\%today%\image.wim 1 W:\
  • Upvote 1
Link to comment
Share on other sites

 

sam240,

 

Looks like your missng a parameter for imagex /apply. Look at the example at the bottom of the screen. 1 is used to set the image_number.

Try changing it to:

imagex /apply i:\lenovo\base\blank\%today%\image.wim 1 W:\

Well that did it. I also changed the %today% to %Today% not sure if that matters since Set Today has capital T.

Link to comment
Share on other sites

That was easy.

 

With the case of variables, you can test that easy.

 

Copied from a command prompt

C:\> set NAME=JackC:\> echo %name%Jack

Seems to be case insensitive. :)

  • Upvote 1
Link to comment
Share on other sites

That was easy.

 

With the case of variables, you can test that easy.

 

Copied from a command prompt

C:\> set NAME=JackC:\> echo %name%Jack

Seems to be case insensitive. :)

 

Well then that 1 did it. I never used that variable before. It always worked without it but I'm guess because of the script, it requires it.

Link to comment
Share on other sites

The dynamic variables created by a for loop are case sensitive. %%A is different from %%a. So keep those to the same case.

I think I wanna become a programmer! so cool =D

Programmer-clipart.jpg

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...