Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

Does file exist using replace to change file extension

I'm trying to determine whether a file exists in a folder and if so exit my batch file.

What I would like to do is pass the batch file a folder and a filename as separate arguments.

The filename will be something like: MyAccessDatabase.accdb

Then, in the batch file, I need to check to see whether there is a file in the folder passed as the 1st argument with a file extension of .laccdb, so I would be looking for the existence of: MyAccessDatabase.laccdb

If that file exists, I would like to display a message on screen for the user: "The application is already open or closed improperly.  If the application is not running, contact Steve"

Then I want it to exit the batch file.  If that file does not already exist, I want it to continue process the rest of the batch file
Avatar of Bill Prew
Bill Prew

Here's the basic idea, although a popup from BAT is a little tricky, how pretty do you want that to be, and are you opposed to using a very small freeware utility EXE (standalone EVE, no install) that I often use?


First, here's the called BAT file (EE29170747.BAT in my test):


@echo off
setlocal

if exist "%~1\%~n2.laccdb" (
    echo The application is already open or closed improperly.  If the application is not running, contact Steve
    exit /b
)

Open in new window


And here's an example of calling it:


@echo off
setlocal

call EE29170747b.bat "B:\EE\EE29170747\Files" "MyAccessDatabase.accdb"

Open in new window

The small utility I mentioned is WBOX at:

Horst Schaeffer's Software Pages


»bp
Try this:
@echo off
setlocal

set Directory=%~1
set FileName=%~2
set FileBaseName=%~n2
if exist "%Directory%\%FileBaseName%.laccdb" (
	msg.exe %username% "%~nx0: The application is already open or closed improperly. If the application is not running, contact Steve"
	exit /b 1
)

:Continue
echo Starting '%Directory%\%FileName%' ...
REM ...

Open in new window

Bill,
if not exist "%~1\%~n2.laccdb" (
your logic is reversed; if the lock file exists, the script needs to leave.
Yes, corrected that after initial post, thanks.


»bp
And msg.exe is an option for a simple popup message box.  Over the years I have tended to stay away from it, but that's a personal "somewhat unfounded" bias.  I know for computer to computer messaging it can be hit or miss, but on the local machine it should work.

Just be aware it's not available on Windows Home, but I suspect that's not an issue in your environment.


»bp
Avatar of Dale Fye

ASKER

@oBdA


is msg.exe a Windows file?


Bill, is that the file you were talking about in your original post?

Yes, that's part of the OS in the server versions of Windows and all desktop versions that can be joined to a domain since Server 2003/XP.
Works remotely as well, but only if the user sending the message is local Admin on the receiving machine.
Just try it:
msg.exe %username% "Hello World"

Open in new window

MSG.EXE is part of windows typically, yes?

Bill, is that the file you were talking about in your original post?

No, I use a slightly more robust pair of utilities for my BAT messaging needs, some good freeware from a fellow developer.  The link I shared was to one of those tools, WBAT, on a page with several other useful BAT supplement utilities for user input and alerts...


»bp
And of course a small VBScript approach can be used as well, if you want to try a different approach...

@echo off
setlocal

if exist "%~1\%~n2.laccdb" (
    echo MsgBox "The application is already open or closed improperly." ^& vbCrLf ^& vbCrLf ^& "If the application is not running, contact Steve", vbOkOnly, "Warning - Already Running" >"%TEMP%\%~n0.vbs"
    wscript.exe "%TEMP%\%~n0.vbs"
    del "%TEMP%\%~n0.vbs"

    exit /b
)

Open in new window

User generated image
»bp

Bill,


I like the VB script version, here is what I'm using now for my test, and it's not working properly.   When I currently run the first batch file I get a screen flicker when a command box opens and then closes, but no other messages:



Routine which calls the 2nd bat:

@echo off
:: Launches a Bat file which checks to see if a file (2nd argument with .laccdb) exists in the folder (1st argument)
CALL E:\Developing Solutions\TestFolder\FileExists.bat "C:\Users\Admin\AppData\Roaming\Microsoft\Addins\" "Test.accdb"

Open in new window

And the FileExist.bat:

@echo off
setlocal

:: Accepts two parameters, a folder name and a filename
:: Checks to see whether the filename with a ".laccdb" extension exists and displays message
::if exist "%~1\%~n2.laccdb" (
    echo MsgBox "The application is already open or closed improperly." ^& vbCrLf ^& vbCrLf ^& "If the application is not running, contact your system administator", vbOkOnly, "Warning - Already Running" >"%TEMP%\%~n0.vbs"
    wscript.exe "%TEMP%\%~n0.vbs"
    del "%TEMP%\%~n0.vbs"

    exit /b
)
 echo MsgBox "The application was not running", vbOkOnly, "Continue" >"%TEMP%\%~n0.vbs"
 wscript.exe "%TEMP%\%~n0.vbs"
 del "%TEMP%\%~n0.vbs"

Open in new window


In the first bat, if this .bat is in the same folder as the 2nd, do I need the full path name or can I simply use:

Call FileExists.Bat "arg1" "arg2"

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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

Thanks, to both of you, that really helped, it has been so long since I've used batch files!