if exist command in a batch flle

Hello Experts,

i have a simple batch file to check if a file exist in a particular directory, if so then copy it to some other directory and then display a message saying file copied. if the file does not exist in that directory look for the file in another directory and do same process again.

my batch file works properly, the only problem is it displays file copied message whether the file exist or not.

basically if the file in the first IF does not exist then i dont want the file copied message from the first if condition be displayed.

hope i am clear in explaining.

Thanks appreicate it

@echo off
IF EXIST c:\Test1\batchMsgBoxFile.bat copy c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt
echo x=msgbox("file copied") > message1.vbs
start message1.vbs
IF NOT EXIST GOTO TEST2FILE

:TEST2FILE

copy c:\Test2\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.bat
call c:\DisplayBatch\batchMsgBoxFile.bat
echo x=msgbox("file copied to Test2 folder") > message2.vbs
start message2.vbs

Open in new window

LVL 2
niceoneishereAsked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:

@echo off
IF EXIST c:\Test1\batchMsgBoxFile.bat (
  copy c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt
  echo x=msgbox("file copied") > message1.vbs
  start message1.vbs
) else (
  copy c:\Test2\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.bat
  call c:\DisplayBatch\batchMsgBoxFile.bat
  echo x=msgbox("file copied to Test2 folder") > message2.vbs
  start message2.vbs
)

Open in new window

0
 
Bill PrewConnect With a Mentor Commented:
I'm not sure exactly what you are trying to do, but you can use parens to group statements after an IF.  So you can do something like this:

@echo off
IF EXIST c:\Test1\batchMsgBoxFile.bat (
  copy c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt
  echo x=msgbox("file copied") > message1.vbs
  start message1.vbs
)

~bp
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
However, there are more simple means to display a message box, like the msg command:

msg * "File 1 copied" /time:5
will display a message box for 5 seconds, while continueing to proceed with the batch file.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
vikas_madhusudanaConnect With a Mentor Commented:
can you change this to this

IF EXIST c:\Test1\batchMsgBoxFile.bat copy c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt
echo x=msgbox("file copied") > message1.vbs

IF EXIST c:\Test1\batchMsgBoxFile.bat (
copy c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt
echo x=msgbox("file copied") > message1.vbs
)
0
 
Bill PrewCommented:
A couple of other thoughts on message boxes from BAT files.

http://claudiosoft.online.fr/msgbox.html

http://www.robvanderwoude.com/usermessages.php

~bp
0
 
t0t0Connect With a Mentor Commented:
Firstly, let me make it quite clear your batch file DOES NOT work properly as you seem to think it does. Under 'normal' circumsatnces, it might appear to work 'properly' however, it is logically flawed.

'batchMsgBoxFile.bat' could just as well be a foldername. That being the case, the following condition will fail:

   IF EXIST c:\Test1\batchMsgBoxFile.bat

Unlikely as it might be, nevertheless hopefully, I make my point.


I have devised a little trick to distinguish between testing for the existence of files and folders. This uses the DIR /A-D and DIR /AD commands as in the following code:


   @ECHO OFF
   DIR /A-D c:\Test1\batchMsgBoxFile.bat >NUL 2>&1
   IF %ERRORLEVEL%==0 (
      COPY /Y c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt >NUL
      ECHO x=msgbox("file copied")>message.vbs
   ) ELSE (
      COPY /Y c:\Test2\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.bat >NUL
      CALL c:\DisplayBatch\batchMsgBoxFile.bat
      ECHO x=msgbox("file copied to Test2 folder")>message.vbs
   )
   START message.vbs


Even though this is an improvement on your code, it still makes a lot of assumptions. For example, it assumes the COPY command will succeed. It also assumes message.vbs will be created and will exist when attempting to start it. Other assumptions include the existence of c:\Test2 and the .bat file etc..

As I said, under 'ordinary' circumstances, the code above will work without much concern but I would refrain from using the term 'proper'.

Notice I have replaced message1.vbs and message2.vbs with just a single file: message.vbs. This is because your code is going to display a message regardless of the IF condition therefore, it might as well be the same .vbs file thereby saving code and making things a little clearer.

That said, the following code will also suffice:


   @ECHO OFF
   IF EXIST c:\Test1\batchMsgBoxFile.bat (
      COPY /Y c:\Test1\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.txt >NUL
      ECHO x=msgbox("file copied")>message.vbs
   ) ELSE (
      COPY /Y c:\Test2\batchMsgBoxFile.bat c:\DisplayBatch\batchMsgBoxFile.bat >NUL
      CALL c:\DisplayBatch\batchMsgBoxFile.bat
      ECHO x=msgbox("file copied to Test2 folder")>message.vbs
   )
   START message.vbs
0
 
niceoneishereAuthor Commented:
Thanks to everyone. all the suggested methods helped and hence i gave point equally to everyone. appreciate it

0
 
niceoneishereAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.