Link to home
Start Free TrialLog in
Avatar of wajhiuddin
wajhiuddin

asked on

add IF command in autoexec.bat

I created a bat file to copy an some .exe and folders on our laptops which seem to be working fine but I need ot add couple of funtions to it
(1) IF any of the  command failed it shoud stop the process instead of going further and print the message saying "operation failed "
(2) I also need to add to bring a message asking "is this a TABLET ? "Y" or "N"
if user enters Y then run command  "copy sysprep.inf c:\sysprep"  and then go to next  if N  go to to next which is the following command
"C:\sysprep\sysprep -mini -reseal"
please don't throw the links out of google unless you tried them already and have an experience because I had tried them and they are kind of went over my head

Audit.txt
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Here is one way you could do it with some feedback to the person running the script etc:

OK what you need then is something like this:

@Echo off

echo *** Copying AutoCP.EXE ***
copy autocp.exe /y /v c:\cuptools
 if errorlevel 1 (set error=autocp.exe & goto error)
 pause

echo *** Copying NVSPBIND.EXE ***
copy nvspbind.exe /y /v c:\cuptools
 if errorlevel 1 (set error=nvspbind.exe & goto error)
 pause

echo *** Running Install.bat ***
Call install.bat
 if errorlevel 1 (set error=install.bat & goto error)
 pause

echo *** Copying Sep ***
Xcopy /s sep c:\cuptools\sep /y
 if errorlevel 1 (set error=sep & goto error)
 pause

echo *** Copying vdefhub.zip ***
copy vdefhub.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=vdefhub.zip & goto error)
 pause

echo *** Copying IPSDef.zip ***
copy IPSDef.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=IPSDef.zip & goto error)
 pause

echo There were no errors during the process
 pause
 exit /b

:error

echo There was an error during the process for %error%
 pause
 exit /b

Hi

If you want a print out when copy has failed I would use xcopy, this is because it sets an error level = see below
 
xcopy autocp.exe c:\cuptools 
if if errorlevel 4 goto lowmemory 
if errorlevel 2 goto abort 
if errorlevel 0 goto error

copy nvspbind.exe /y /v c:\cuptools


:lowmemory 
ECHO COPY FAILED due to low memory
goto :enderror
:abort
ECHO User aborted this!
goto :enderror

:error
ECHO Error
goto :enderror

:enderror
Echo quitting
pause
exit

Open in new window



As for the 2nd you need to use choice see below as an example

 
Rem You need CHOICE to run this
CHOICE /N /C:YN Is this a Tablet? (Y, N)%1
REM - THE NEXT LINES ARE DIRECTING USER DEPENDING UPON INPUT
IF ERRORLEVEL ==Y GOTO Y
IF ERRORLEVEL ==N GOTO ONE
GOTO END
:Y
ECHO YOU HAVE PRESSED Y NO SYSPREP...BLAH
GOTO END
:N
ECHO YOU HAVE PRESSED N SYSPREP RUNNING...BLAH
C:\sysprep\sysprep -mini -reseal
GOTO END

:END

Open in new window



As you see we use labels to control this, I have not tested this but should work :)

Hope this helps
Sorry missed bit about sysprep:

@Echo off

cls
echo Doing the whatever this process
echo.

:answer
 set answer=N
 set /p answer=Is this a tablet [Y] or [N]
if /I "%answer%"=="Y" (
 echo ... Is a tablet
 echo *** Copying sysprep.inf ***
 copy sysprep.inf c:\sysprep
 if errorlevel 1 (set error=SysprepCopy & goto error)
) ELSE (
 echo ... Not a tablet
)

echo *** Running Sysprep ***
C:\sysprep\sysprep -mini -reseal
 if errorlevel 1 (set error=RunSysprep & goto error)
 pause

echo *** Copying AutoCP.EXE ***
copy autocp.exe /y /v c:\cuptools
 if errorlevel 1 (set error=autocp.exe & goto error)
 pause

echo *** Copying NVSPBIND.EXE ***
copy nvspbind.exe /y /v c:\cuptools
 if errorlevel 1 (set error=nvspbind.exe & goto error)
 pause

echo *** Running Install.bat ***
Call install.bat
 if errorlevel 1 (set error=install.bat & goto error)
 pause

echo *** Copying Sep ***
Xcopy /s sep c:\cuptools\sep /y
 if errorlevel 1 (set error=sep & goto error)
 pause

echo *** Copying vdefhub.zip ***
copy vdefhub.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=vdefhub.zip & goto error)
 pause

echo *** Copying IPSDef.zip ***
copy IPSDef.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=IPSDef.zip & goto error)
 pause

echo There were no errors during the process
 pause
 exit /b

:error

echo There was an error during the process for %error%
 pause
 exit /b



Only 250 points?
Avatar of wajhiuddin
wajhiuddin

ASKER

would you please combine all this togather on one file/script and post it ?

I raised the points to 500
Thanks
You can leave out any of the "echo" and "pause" statements if you don't want them cluttering up the screen and asking for key presses as you wish.
commands runn OK from top till
Xcopy /s sep c:\cuptools\sep /y
 error appears saying "file sep can not be found"
not sure why I tried this command manually in dos which works fine
OK, where is the file or dir "sep" currently.... it will be trying to copy a directory called sep from the current directory when the script is run.  TRy either specifying the full dir or adding it as a change dir, e.g.

cd /d C:\some\dir\wheresepsubdir is
Xcopy /s sep c:\cuptools\sep /y

or

Xcopy /s c:\somedir\whatever\sep c:\cuptools\sep /y

Steve
Oh yeah make sure the dir. exists in the destination otherwise it will ask you if it is a "file or directory".

e.g.

md c:\cuptools\sep 2>NUL
Xcopy /s c:\somedir\whatever\sep c:\cuptools\sep /y
yes the dir is there and it works fine if I manuallly type same command at the dos prompt
I can't  put the source path because it will be different as per drive maping but destination will be the same
following process should  be happened at the end
answer
 set answer=N
 set /p answer=Is this a tablet [Y] or [N]
if /I "%answer%"=="Y" (
 echo ... Is a tablet
 echo *** Copying sysprep.inf ***
 copy sysprep.inf c:\sysprep
 if errorlevel 1 (set error=SysprepCopy & goto error)
) ELSE (
 echo ... Not a tablet
)

So I moved the above cammands to the bottom
but after when it fails to xcopy sep folder it escapes following process
echo *** Copying vdefhub.zip ***
copy vdefhub.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=vdefhub.zip & goto error)
 pause

echo *** Copying IPSDef.zip ***
copy IPSDef.zip /y /v c:\cuptools\SEP
 if errorlevel 1 (set error=IPSDef.zip & goto error)


and runs sysprep ???
ok, you need to put it between the bit

pause

echo there were no errors...

Then it should do the other last  bit, then your sysprep bit and the last bit only gets run if there is an error at all.

On mobile at moment but if unsure can post reordered script later.

Steve
ok how does it know where the sep dir should come from?  is it a subdir of where  the script is for instance?  if it is we can put the path in as "%~dp0sep" to get the path of the batch file and use that as the source path.  especially if the batch is started from a unc path - i.e. \\server\share rather than a drive letter the current drive / path isn't set to the batch file dir.

steve
ok how does it know where the sep dir should come from?  is it a subdir of where  the script is for instance?  if it is we can put the path in as "%~dp0sep" to get the path of the batch file and use that as the source path.  especially if the batch is started from a unc path - i.e. \\server\share rather than a drive letter the current drive / path isn't set to the batch file dir.

steve
OK great now it finds SEP folder and Sysprep folder after adding "%~dp0sep" but now can't find
vdefhub.zip & IPSDef.zip adding "%~dp0sep" doesn't help here.
how come all these  folder and files  don't have any problem on my bat file ?(just for curioustiy)
OK almost there,
attached is the script  looks like it works oK except after copying all files and folders it exit the program  instaed of asking Tablet or not and running sysprep. I had to add REM before exit /b
and then it did asks about the Tablet or not but no mateer what I put either Y or N it goes to run sysprep without copying sysprep.inf ignoring Y

Nothing attached that I can see sorry.  

Don't remove the exit /b, it sounds like you have some commands in the wrong order now.  Please try again posting it here -- just paste it into the comment or click on the "code" link and will check it.

Steve
sorry it;s attached now  AuditTest.TXT
will post new ver in minute but you posted ythe answer code between the wrong section, should be between pause and echo of NOT being an error.  will clarify shortly.
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if yoy say no for TABLET it won't run sysprep it exits intead.
if you say yes then it's all good.
works great thanks
no problem, glad we got there - hope it is a bit clearer how you can do things now.   As you know lots of good people hereif you need any more.

Steve
yep you are correct Thansk again