Skip navigation

JSI Tip 8055. How can I cause all or a portion of a logon script to only run if the script has changed?


If you have commands in your logon script that only need to run when the script changes, use the following technique to cause it to only run when the script has changed.

Example:

@echo off
setlocal
net use x: \\ServerName\ShareName1
net use y: \\ServerName\ShareName2
rundll32 printui.dll,PrintUIEntry /ia /c\\PRTSVR /m "AGFA-AccuSet v52.3" /h "Intel"
rundll32 printui.dll,PrintUIEntry /if /b "Test Printer" /c\\PRTSVR /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "AGFA-AccuSet v52.3"
endlocal

If you only wanted the rundll32 commands to run when the script has changed:

@echo off
setlocal
net use x: \\ServerName\ShareName1
net use y: \\ServerName\ShareName2
:: Insert
:: set batchdp to the full path to the script file.
set batchdp="%~f0"
:: set batchfn to the file name of the script file.
set batchfn=%~n0
:: set batchfnx to the file name and extension of the script file.
set batchfnx=%~nx0
:: set oldfile to the file name that records the script date / time in the user's profile
set oldfile="%userprofile%\%batchfn%.old"
If not exist %oldfile% set old=none&goto postit
:: Retrieve the previous script date / time
for /f "Tokens=*" %%a in ('type %oldfile%') do (
 set old=%%a
)
:postit
:: Set the current script date / time
for /f "Tokens=*" %%a in ('dir %batchdp% /N^|findstr /L /I /c:"%batchfnx%"') do (
 set new=%%a
)
:: If the script has not changed, exit or goto .....
if "%old%"=="%new%" endlocal&goto :EOF
:: Write the current script's date / time to the user profile
echo %new%>%oldfile%
:: Previous code here
rundll32 printui.dll,PrintUIEntry /ia /c\\PRTSVR /m "AGFA-AccuSet v52.3" /h "Intel"
rundll32 printui.dll,PrintUIEntry /if /b "Test Printer" /c\\PRTSVR /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "AGFA-AccuSet v52.3"
endlocal



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish