Avatar of Dale Fye
Dale Fye
Flag 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
Windows Batch

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
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

Bill Prew

The small utility I mentioned is WBOX at:

Horst Schaeffer's Software Pages


»bp
oBdA

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

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
oBdA

Bill,
if not exist "%~1\%~n2.laccdb" (
your logic is reversed; if the lock file exists, the script needs to leave.
Bill Prew

Yes, corrected that after initial post, thanks.


»bp
Bill Prew

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dale Fye

ASKER

@oBdA


is msg.exe a Windows file?


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

oBdA

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

Bill Prew

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
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
Bill Prew

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

sshot-111.png
»bp
Dale Fye

ASKER

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
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dale Fye

ASKER

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.