Jump to content

Batch Scripts for ImageX Apply, Capture and Append


Recommended Posts


@echo off

set comp =

echo 1 for maximum

echo 2 for fast

echo 3 for none

echo " "

set /p type=Enter compression type:

if %type%==1 set comp=maximum

if %type%==2 set comp=fast

if %type%==3 set comp=none

echo %comp%

Link to comment
Share on other sites

Got it working. Thanks for help, Geezery.

Another question if you can help:

I want to check whether the folder where the new image will reside exists or not. For example, I tell my script I want image in D:\Images\Image.wim; how would I get it to check that D:\Images\ exists or not (just in case there was error and so I wouldn't have to repeat the whole process)? It would mean I'd have to delete Images.wim from the variable assigned to the whole D:\Images\Images.wim.

Is this possible?

Appreciate your help.

Link to comment
Share on other sites

@IcemanND, I couldn't get it to work. I even replaced %A to %%A (for some reason I've seen that before). The %~dpA expands to a 0.

Anyway, here are my working scripts for image capture and image apply. These have to be in the same folder as imagex.exe. It just won't check your path to see whether it is correct or not since I couldn't get it to work.

(I understand someone is working on or has made a GUI but this fits my purposes.)

imagex_apply.cmd imagex_capture.cmd

imagex_apply.cmd

@echo off
cd /d %~dp0
set imgx=%cd%
if not exist "%cd%\imagex.exe" goto _notexist
goto _main
:_notexist
echo.
echo IMAGEX.EXE executable not found. Please ensure that IMAGEX_APPLY.CMD
echo is in the same folder as IMAGEX.EXE before executing this script.
echo This script will now exit.
echo.
pause
exit

:_main
echo.
echo.
echo.
echo [I M A G E X A P P L Y M A I N M E N U]
echo.
echo 1) Specify or change source path (the path to you .wim image file)
echo Source: [%_asource%]
echo.
echo 2) Specify or change target path, where your image will be restored
echo Target: [%_atarget%]
echo.
echo 3) Specify or change index you want to restore (a number, such as 2)
echo Index: [%_index%]
echo.
if "%_asource%" == "" goto _menu1
if "%_atarget%" == "" goto _menu1
if "%_index%" == "" goto _menu1
echo 4) Apply your image now where ready
echo From [%_asource%] To [%_atarget%]
echo.

:_menu1
echo Q) Quit. R) Reset all variables.
echo.

:_mainch
set _ok=
set /p _ok=Enter your choice:
if "%_ok%" == "1" goto _getsrc
if "%_ok%" == "2" goto _gettarget
if "%_ok%" == "3" goto _getindex
if "%_ok%" == "4" goto _apply
if /I "%_ok%" == "q" goto _end
if /I "%_ok%" == "r" goto _reset
goto _mainch

:_getsrc
set _ok=
echo.
echo Specify location of source image to restore with full path.
set /p _ok=Enter Source path:
for %%A in (%_ok%) do set _ext=%%~xA
if not exist %_ok% (
echo.
echo ---------------------------------------------------------
echo Path does not exist. Please input a valid location again.
echo ---------------------------------------------------------
goto _getsrc
) else (
if /I "%_ext%"==".wim" (
set _asource=%_ok%
setx _asource %_ok%
goto _main
) else (
echo.
echo ---------------------------------------------------------------
echo Error: The path "%_ok%" does not seem to be a valid .wim image file.
echo ---------------------------------------------------------------
goto _getsrc
pause
)
)

:_gettarget
set _ok=
echo.
echo Specify target to install source image with full path.
set /p _ok=Enter Target path:
if exist "%_ok%" (
set _atarget=%_ok%
setx _atarget %_ok%
) else (
echo.
echo ---------------------------------------------------------------
echo Error: The path "%_ok%" is not a correct location.
echo ---------------------------------------------------------------
goto _gettarget
pause
)

goto _main

:_getindex
set _ok=
echo.
echo Specify image index with number
set /p _ok=Enter Index number:
set _index=%_ok%
setx _index %_ok%
goto _main

:_apply
echo.
echo.
"%imgx%\imagex.exe" /apply "%_asource%" %_index% "%_atarget%"
echo.
pause
goto _end

:_reset
set _asource=
set _index=
set _atarget=
goto _main

:_end

imagex_captureappend.cmd

@echo off
cd /d %~dp0
set imgx=%cd%
if not exist "%cd%\imagex.exe" goto _notexist
goto _main

:_notexist
echo.
echo IMAGEX.EXE executable not found. Please ensure that imagex_capture.cmd
echo is in the same folder as IMAGEX.EXE before executing this script.
echo This script will now exit.
echo.
pause
exit

:_main
echo.
echo.
echo.
echo [I M A G E X C A P T U R E / A P P E N D M A I N M E N U]
echo.
echo 1) Specify or change source path, the path you want to capture (e.g. C:\)
echo Source: [%_csource%]
echo.
echo 2) Specify or change target image to create/append (e.g. d:\myimage.wim)
if "%_append%"=="yes" echo *Image will be appended unless you specify a non-existing image file.*
echo Target: [%_ctarget%]
echo.
echo 3) Specify or change unique description of your image (e.g. Vista Backup)
echo Description: [%_descript%]
echo.
echo 4) Specify or change FLAG of your image (e.g. ULTIMATE)
echo FLAG: [%_FLAG%]
echo.
echo 5) Specify or change compression type (maximum, fast, none)
echo Compression: [%_compr%]
echo.
if "%_csource%" == "" goto _menu1
if "%_ctarget%" == "" goto _menu1
if "%_descript%" == "" goto _menu1
if "%_append%" == "yes" goto _skip
if "%_FLAG%" == "" goto _menu1
if "%_compr%" == "" goto _menu1
:_skip
echo 6) Capture or append your image now when ready
echo From [%_csource%] To [%_ctarget%]
echo.

:_menu1
echo Q) Quit. R) Reset all variables
echo.

:_mainch
set _ok=
set /p _ok=Enter your choice:
if "%_ok%" == "1" goto _getsrc
if "%_ok%" == "2" goto _gettarget
if "%_ok%" == "3" goto _getdescript
if "%_ok%" == "4" goto _getFLAG
if "%_ok%" == "5" goto _getcompr
if "%_ok%" == "6" goto _capture
if /I "%_ok%" == "q" goto _end
if /I "%_ok%" == "r" goto _reset
goto _mainch


:_getsrc
set _ok=
echo.
echo Specify location of source to capture as a .wim image.
echo Usually, this would be the root of a drive, such as C:\.
echo.
set /p _ok=Enter Source path:
if exist "%_ok%" (
setx _csource %_ok%
set _csource=%_ok%
) else (
echo.
echo ---------------------------------------
echo Error: The path "%_ok%" does not exist.
echo ---------------------------------------
goto _getsrc
pause
)

goto _main

:_gettarget
set targ=
echo.
echo Specify path and filename to store the target image, such as D:\image.wim.
echo.
set /p _ok=Enter Target image path:
for %%A in (%_ok%) do set _ext=%%~xA && set _path=%%~dpA
if /I "%_ext%"==" " (
echo.
echo -----------------------------------------------------
echo Error: The filename entered is not a valid .wim file.
echo -----------------------------------------------------
goto _gettarget
) else (
if exist %_ok% (
set _ctarget=%_ok%
setx _ctarget %_ok%
set _append=yes
setx _append yes
goto _main
) else (
if not exist %_path% (
echo.
echo -----------------------------------------------------
echo Path does not exist. Please input a valid path again.
echo -----------------------------------------------------
goto _gettarget
) else (
setx _ctarget %_ok%
set _ctarget=%_ok%
set _append=no
setx _append no
goto _main
)
)
)

:_getcompr
set comp =
echo.
echo Please select compression type.
echo.
echo 1 for maximum
echo 2 for fast
echo 3 for none
echo.
set /p type=Enter compression type:
if %type%==1 set _compr=MAXIMUM&& setx _compr MAXIMUM
if %type%==2 set _compr=FAST&& setx _compr FAST
if %type%==3 set _compr=NONE&& setx _compr NONE

goto _main

:_getFLAG
set _ok=
echo.
echo Specify image FLAG.
echo.
set /p _ok=Enter FLAG:
set _FLAG=%_ok%
setx _FLAG %_ok%

goto _main

:_getdescript
set _ok=
echo.
echo Type description of image
echo.
set /p _ok=Enter description:
set _descript=%_ok%
setx _descript "%_ok%"
goto _main

:_capture
echo.
if "%_append%"=="yes" goto _appendimage
echo Ready to capture image from source %_csource% and store image to
echo %_ctarget% using compression type %_compr%.
echo.
pause
echo.
echo on
"%imgx%\imagex.exe" /capture %_csource% "%_ctarget%" "%_FLAG%" "%_descript%" /COMPRESS %_compr%
echo off
pause
goto _end

:_appendimage
echo.
echo Ready to append image from source %_csource% and store image to
echo %_ctarget%.
echo.
pause
echo.
echo on
"%imgx%\imagex.exe" /append %_csource% "%_ctarget%" "%_descript%"
echo off
pause
echo.
goto _end

:_reset
@echo off
set _csource=
set _ctarget=
set _descript=
set _compr=
set _FLAG=
set _append=
goto _main

:_end

Edit: If you don't have setx, download from link provided by FireGeier in one of the following post. setx is not necessary. If you don't have it, you can delete the lines that point to setx and script will still work, but it won't remember your paths when you restart the script.

Edit2: Corrected some bugs. Added append if you point to an existing .wim file.

Edited by Shark007
change code tags to codebox
Link to comment
Share on other sites

for use in a batch file:

for %%A in (%_ok%) do if Exist %%~dpA (
set _target=%_ok%
) else (
echo.
echo Error: The path "%_ok%" does not exist.
goto _settarget
pause
)

set _target
pause

how's that for 1.5k in posts.

Link to comment
Share on other sites

Hello spacesurfer!

You can check for NUL, if you want to find out, if folder does exist:

IF EXIST D:\Images\NUL your command, if D:\Images does exist

OR

IF NOT EXIST D:\Images\NUL your command, if D:\Images does NOT exist

Regards,

Martin

Link to comment
Share on other sites

IcemanND, your script does check for the folder, but it will accept a folder without a filename specified whether it exists or not.

For example, if I input F:\windows\myimage, it will tell me F:\windows does not exist (in case it really doesn't exist).

On the other hand, if I input F:\windows\, it will take it (even though F:\windows\ does not exist).

I thought that was strange.

Addendum:

I got it working IcemanND.

But now the question is, is there a way to save the variables (to the paths) you entered and recall them next time you run the script after closing the command prompt?

Edited by spacesurfer
Link to comment
Share on other sites

Just for the record, the proper way to check for a directory should be checking for nul.ext instead of nul, at least on NT based systems, reference:

http://xset.tripod.com/tip5.htm

http://msmvps.com/blogs/martinzugec/archiv...-same-name.aspx

It is a remote possibility that there exists a file with same name as folder, but you never know.

jaclaz

Link to comment
Share on other sites

@FireGeier. Thanks for setx. I got it working now

But I had to use both set and setx. Reason is setx didn't seem to update the variables in the menu (so I wasn't sure if they were being assigned in real time). If I closed and restarted, then I saw the variables.

Also if I changed an already assigned variable with setx, then I had to restart the script to see the change.

I got around it by using both commands and it works fine.

Thanks for tip.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...