1.

Solve : Creating simple batch file, how do i invoke a response if sucessful?

Answer»

Hi ,

I am looking to create a VERY simple batch file that creates a directory structure

below is the code i have created

@echo off

@pause
echo on

@mkdir C:\Temp\test11

@mkdir C:\Temp\test\sample11

@pause


This works very well in the creation of the FOLDERS. It also works very well if the batch file is run again , because it comes up with a warning to say that the folders already exists.

I would like to add a message to say that the files were created successfully IF they did not exist.
If they already exist then the COMPUTER response is more than sufficient.

Also i would like to add a command to say "press Enter to exit app" , rather than the pause command

Any pointers graciously accepted.
After a bit of researching I managed to COME up with a solution to your request:

@echo off

:CREATE1
IF NOT EXIST C:\Temp\test11 md C:\Temp\test11
GOTO :MSG1

:CREATE2
IF NOT EXIST C:\Temp\test\sample11 md C:\Temp\test\sample11
GOTO :MSG2

:MSG1
ECHO Creation of C:\Temp\test11 successful.
GOTO CREATE2

:MSG2
ECHO Creation of C:\Temp\test\sample11 successful.

REM choice /c: /n "Press enter to exit the app"

pause


However, I still don't know how to CATCH the Enter key with the CHOICE command.

Hi , Thanks for your THOUGHTS!

Because the program will only ever make the same folders , I didnt need to go down the road of making a variable

In the end I settled on ...

@Echo off
pause

if exist d:\test echo Folder test Already Exists
if not exist d:\test md d:\test & echo Folder test Created

if exist d:\test\sample echo Folder sample Already Exists
if not exist d:\test\sample md d:\test\sample & echo Folder sample created

if exist d:\test\sample1 echo Folder sample1 Already Exists
if not exist d:\test\sample1 md d:\test\sample1 & echo Folder sample1 Created



But still no further forward on the 'press enter to exit' .. although , for purposes i'll just stick with the pause command

Thanks again
TimJust saying:

Use the %errorlevel% variable. If %errorlevel% is not 0 then there was error. So, use this code after your desired code. Errorlevel gets reset after each command run.

If not %errorlevel%==0 Echo COMMAND FAILED. Quote from: o0timbo0o on January 20, 2010, 08:51:54 AM

But still no further forward on the 'press enter to exit' .. although , for purposes i'll just stick with the pause command

You can use set /p to wait for a string terminated by ENTER. The string can be zero length (i.e. just ENTER by itself)

Code: [Select]set /p dummy="Press the Enter key "
The variable "dummy" is so called because it is just a throwaway & is not used for anything.



Discussion

No Comment Found