Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

if exist command in a batch flle

Posted on 2010-01-02
8
Medium Priority
?
1,071 Views
Last Modified: 2013-11-09
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

0
Comment
Question by:niceoneishere
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 500 total points
ID: 26162085

@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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 26162090
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 26162099
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 7

Assisted Solution

by:vikas_madhusudana
vikas_madhusudana earned 500 total points
ID: 26162102
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26162127
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
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 500 total points
ID: 26165064
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26173902
Thanks to everyone. all the suggested methods helped and hence i gave point equally to everyone. appreciate it

0
 
LVL 2

Author Closing Comment

by:niceoneishere
ID: 31671970
Thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question