We help IT Professionals succeed at work.

Does file exist using replace to change file extension

Dale Fye
Dale Fye asked
on
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
Comment
Watch Question

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:

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
)


And here's an example of calling it:


@echo off
setlocal

call EE29170747b.bat "B:\EE\EE29170747\Files" "MyAccessDatabase.accdb"
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
The small utility I mentioned is WBOX at:

Horst Schaeffer's Software Pages


»bp
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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

Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Bill,
if not exist "%~1\%~n2.laccdb" (
your logic is reversed; if the lock file exists, the script needs to leave.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Yes, corrected that after initial post, thanks.


»bp
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Author

Commented:

@oBdA


is msg.exe a Windows file?


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

Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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 PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
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 FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Author

Commented:

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"

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"
 


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"


Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
I suspect the problem is this line:

CALL E:\Developing Solutions\TestFolder\FileExists.bat "C:\Users\Admin\AppData\Roaming\Microsoft\Addins\" "Test.accdb"

since the bat you are calling has a path with spaces in it, you need to quote it, as in:

CALL "E:\Developing Solutions\TestFolder\FileExists.bat" "C:\Users\Admin\AppData\Roaming\Microsoft\Addins\" "Test.accdb"

Yes, if you are already in the folder as the default folder when running the first bat, you can just use the filename format (no path), as in:

Call FileExists.Bat "arg1" "arg2"

A suggestion, when testing BAT scripts always run them from a command prompt you open first, not just by double clicking them in Explorer, etc.  That way you should see any errors displayed.


»bp
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Author

Commented:

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