|
Answer» 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.
Code: [Select]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:
Code: [Select]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]Your code has 'fall into' errors. Unless you want to fall into something. You need to deal with the case where the error level is not a 0 or 1. Also, your use of ERRORLEVEL is odd. Below is a typical use of it in a batch FILE.
Code: [Select]IF ERRORLEVEL ==3 GOTO THREE IF ERRORLEVEL ==2 GOTO TWO IF ERRORLEVEL ==1 GOTO ONE[ DOS batch files rules are very odd. Look at this: http://support.microsoft.com/kb/69576
If batch is hard for you, consider using a more friendly script language. Myself, I would VBA inside of Access, but I am not sure if your application could do what your want using only VBA. (Visual Basic for Applications.) https://www.youtube.com/watch?v=G9pqmd2PSYk
Many IT professionals use Powershell with batch for common problems. This guide is for using Powershell to test network issues. http://www.microsoft.com/en-us/download/details.aspx?id=41948
Sorry I can't help more.
QUOTE from: Geek-9pm on February 10, 2015, 10:06:13 PM Also, your use of ERRORLEVEL is odd.
The OP is using the NT family alternative SYNTAX for errorlevel, which is an improvement on the old MS-DOS syntax
Old MS_DOS syntax:
If errorlevel N command
(There are no == signs like in your example.)
That means if the errorlevel is N or more, execute the command (often a GOTO a label)
HOWEVER in NT family command language (I believe since Windows 2000 at least) you have an additional alternative syntax where errorlevel is treated like any percent-sign variable like this:
You don't really need any quotes
if %errorlevel%==N command if %errorlevel% equ N command (equivalent)
Conveniently, you can use the standard IF comparison operators available in NT family command language:
EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal
I suggest to the OP: put some lines like this...
echo errorlevel is %errorlevel%
...in your script to try to see what is happening.
|