How to detect if a file exists in login script

  • 7015261
  • 20-Jun-2014
  • 23-Jul-2014

Environment

Novell Client 2 SP3 for Windows

Situation

It may be helpful to find if a file exists on a workstation, such as when testing if an application has been installed, etc.

Resolution

Checking for the existence of a file can be accomplished by using IF EXIST in a batch file called from the login script, or by using the login script ERRORLEVEL variable with a MAP statement.
 
Use "if exist" in a batch file:
 
Create a batch file using the "if exist" batch file command, and place it in a location accessible to the user when executing the login script.  For example:
 
TEST.BAT:

echo off
if exist c:\test\file.txt goto yesfile
if not exist c:\test\file.txt goto nofile
goto end
:yesfile
echo FILE EXISTS PROCESSING
goto end
:nofile
echo FILE DOES NOT EXIST PROCESSING
copy f:\PUBLIC\FILES\file.txt c:\test\file.txt
goto end
:end
 
From the login script, call the batch file as follows:
 
#COMMAND.COM /C TEST.BAT
 
The COMMAND.COM /C will close the CMD box window automatically after it terminates.
 
 
Use the "ERRORLEVEL" login script variable:
 
ERROR_LEVEL is a special variable that can be used to tell if there have been errors encountered in the login script processing. In the following example, some users have a Home Directory configured, and others do not. The administrator wants to check for the existence of the Home Directory and if it does not exist, map another location.
 
MAP ERRORS OFF
MAP G:=%HOME_DIRECTORY
IF "%ERROR_LEVEL"<>"0" THEN
MAP G:=.DATA.COMPANY:\%CN
END
Examples using FIND.EXE:
 
The following example checks for the existence of a file (c:\test\file.txt) on the local hard drive by attempting to find a the text "mytext" in the file, and writes "Success" or "Failure" to the login results windows depending on the result.
 
#FIND /I "mytext" c:\test\file.txt
IF "%ERROR_LEVEL"="0" THEN BEGIN
WRITE "Success"
ELSE
WRITE "Failure"
END
 
The following example checks for the existence of a file (c:\test\file.txt) on the local hard drive by attempting to find a certain text (mytext) in the file, and if it does not exist, copies a file from the server to the root of the C: drive on the local workstation.

#FIND /I "mytext" C:\test\file.txt
IF "%ERROR_LEVEL" != "0" THEN
#CMD /C COPY i:file.txt C:\test\file.txt
END