I'm fairly new to writing scripts and having trouble (yes, yet another newbie with a question..
) But I have been working pretty hard on this and I'm not able to get this to work.
I've also just recently learned how to make an access database. Not very relevant, except for that is what the script will be working with. Basically, my company's network is terrible and I'm trying to write a script to work around this.
The script started as a simple "Is access running? If no, start the database." This was put in the task scheduler to run every 3 minutes. That worked, until the computer lost connection to the server for a while. Then since access was still open, it wouldn't restart the database. So I'm attempting to write a script that can read if a specific title of a dialog box if it appears, force close access and open it again. This will also open a network drive in an explorer window and close it to refresh the connection since Microsoft has yet to figure out how to make network drives reconnect AFTER the PC/Laptop connects to the WiFi.
Here is my script. I've added a lot of extra echos so I can see what steps the program is going through when running.
@echo off
echo 10
TASKLIST /FI "imagename eq explorer.exe" /v | Find "Restoring Network Connections" 2>nul
timeout 10
echo 20
if "%errorlevel%"=="0" GOTO check2
timeout 10
echo 30
if "%errorlevel%"=="1" GOTO prog1
timeout 10
echo 40
:check2
echo 50
TASKLIST /FI "imagename eq MSACCESS.EXE" /v | Find "Microsoft Office Access" 2>nul
echo 60
if "%errorlevel%"=="0" GOTO check3
echo 70
if "%errorlevel%"=="1" GOTO prog2
echo 80
:check3
echo 90
TASKLIST /FI "imagename eq MSACCESS.EXE" /v | FIND /I "ACTION FAILED" 2>nul
echo 100
if "%errorlevel%"=="0" GOTO progcheck
echo 110
if "%errorlevel%"=="1" GOTO prog3
echo 120
:progcheck
echo 130
tasklist | findstr /i MSACCESS.EXE
echo 140
if %errorlevel% neq 0 goto restart else
echo 150
timeout 20
echo 160
goto end
echo 170
:restart
echo 180
REM start "" "C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "J:\Manufacturing\TECH\Tool crib\TCI Database rev1.accdb"
echo 190
goto end
echo 200
:prog1
echo 210
timeout 10
echo 220
TASKKILL /F /FI "WINDOWTITLE eq Restoring Network Connections" /IM explorer.exe
echo 230
echo Restoring Network Connections Dialog box closed
echo 240
start "" "C:\refreshnetdrive.bat"
timeout 20
echo 250
goto check2
echo 260
:prog2
echo 270
timeout 10
TASKKILL /F /IM MSACCESS.EXE
echo 280
timeout 10
goto check3
echo 290
:prog3
echo 300
timeout 10
TASKKILL /F /IM "MSACCESS.EXE"
echo 310
timeout 10
goto progcheck
:end
echo 320 end of script
timeout 600
I'll clean it up a bit to make it easier to read:
@echo off
TASKLIST /FI "imagename eq explorer.exe" /v | Find "Restoring Network Connections" 2>nul
if "%errorlevel%"=="0" GOTO check2
if "%errorlevel%"=="1" GOTO prog1
:check2
TASKLIST /FI "imagename eq MSACCESS.EXE" /v | Find "Microsoft Office Access" 2>nul
if "%errorlevel%"=="0" GOTO check3
if "%errorlevel%"=="1" GOTO prog2
:check3
TASKLIST /FI "imagename eq MSACCESS.EXE" /v | FIND /I "ACTION FAILED" 2>nul
if "%errorlevel%"=="0" GOTO progcheck
if "%errorlevel%"=="1" GOTO prog3
:progcheck
tasklist | findstr /i MSACCESS.EXE
if %errorlevel% neq 0 goto restart else
goto end
:restart
REM start "" "C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "J:\Manufacturing\TECH\Tool crib\TCI Database rev1.accdb"
goto end
:prog1
TASKKILL /F /FI "WINDOWTITLE eq Restoring Network Connections" /IM explorer.exe
start "" "C:\refreshnetdrive.bat"
goto check2
:prog2
TASKKILL /F /IM MSACCESS.EXE
goto check3
:prog3
TASKKILL /F /IM "MSACCESS.EXE"
goto progcheck
:end
It looks like I have the first check working (Line 10-30.) I haven't fully tested to be sure. But I know the 2nd and 3rd are not (Line 40-110.) Even when access isn't running, the script finds a positive on the errorlevel and attempts to close and reopen access. I'm really hoping I didn't just miss a quotes somewhere, because I have poured over this for hours wondering where I went wrong. Attached is the output I'm getting from the script with the line echos still entered. Thanks for any help you can give me!
[attachment deleted by admin to conserve space]