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
Microsoft DOSMicrosoft DevelopmentMicrosoft Applications

Avatar of undefined
Last Comment
wajhiuddin

8/22/2022 - Mon
Steve Knight

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

jarjarbinx1979

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
Steve Knight

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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
wajhiuddin

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

I raised the points to 500
Thanks
Steve Knight

I have :-)   http:#36309695
Steve Knight

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
wajhiuddin

ASKER
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
Steve Knight

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
Steve Knight

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
wajhiuddin

ASKER
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
wajhiuddin

ASKER
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 ???
Steve Knight

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Steve Knight

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
Steve Knight

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
wajhiuddin

ASKER
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)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
wajhiuddin

ASKER
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

Steve Knight

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
wajhiuddin

ASKER
sorry it;s attached now  AuditTest.TXT
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Steve Knight

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
Steve Knight

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
wajhiuddin

ASKER
if yoy say no for TABLET it won't run sysprep it exits intead.
if you say yes then it's all good.
wajhiuddin

ASKER
works great thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Steve Knight

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
wajhiuddin

ASKER
yep you are correct Thansk again