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

Jerold Schulman

May 19, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today


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 offsetlocalnet use x: \ServerNameShareName1net use y: \ServerNameShareName2rundll32 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%inftprint.inf" /r "lpt1:" /m "AGFA-AccuSet v52.3"endlocalIf you only wanted the rundll32 commands to run when the script has changed:@echo offsetlocalnet use x: \ServerNameShareName1net use y: \ServerNameShareName2:: 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 profileset oldfile="%userprofile%%batchfn%.old"If not exist %oldfile% set old=none&goto postit:: Retrieve the previous script date / timefor /f "Tokens=*" %%a in ('type %oldfile%') do ( set old=%%a):postit:: Set the current script date / timefor /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 profileecho %new%>%oldfile%:: Previous code hererundll32 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%inftprint.inf" /r "lpt1:" /m "AGFA-AccuSet v52.3"endlocal



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like