How to check if a folder path matches USERs folder in BAT script

140 views Asked by At

I am writing a Windows batch script which tries to determine if a path value (extracted from an application's config file) points to a user specific folder (e.g. %APPDATA% ) or a general one (e.g. C:\ProgramData\AppName).

The long winded way to do this would be to try and check for every possible wya this can be set, e.g this is what I started coding up:

REM Get the value from the config file, entry is "set="

set locSettingsFile="%installFolder%\setting.data"
echo "Loc file %locSettingsFile%"

IF EXIST "%locSettingsFile%" (
   echo "Location settings file found at: %locSettingsFile%"

   REM Got the config file,now  get the set=value entry

   for /F "delims=" %%a in ('FINDSTR /R /C:"^set=" %locSettingsFile%') do set "setKeyVal=%%a"
   echo %setKeyVal%
   IF "%setKeyVal%"=="" (
      echo "Set location is not set in %locSettingsFile%"
   ) ELSE (

      REM Got the entry now extract the value from set=value

      for /f "tokens=2 delims==" %%a in ("%setKeyVal%") do set "setVal=%%a"
      echo "Set value is %setVal%"

      REM Now check if it points to a user specific folder

      IF /I %setVal:~0,9% == "%APPDATA%" IF /I %setVal:~1,8% == ":\USERS\"(

         REM Its user specific

      )
   )
) ELSE (
   echo echo "Location settings file does not exist: %locSettingsFile%"
)

But, before I carry on with this approach, I would like to ask if there is simple one-liner that can tell me if a given pat will map to a user specific folder. Bear in mind also that on some systems the user folder could be a network UNC path.

1

There are 1 answers

0
Compo On

Based only on what is certain from your question:

Set "locSettingsFile=%installFolder%\setting.data"

Echo Loc file %locSettingsFile%

Set "setVal="
For /F "Tokens=1* Delims==" %%A In (
    'FindStr /IRC:"^set=" "%locSettingsFile%" 2^>Nul') Do Set "setVal=%%B"
If Not Defined setVal GoTo :EOF
Echo Set value is %setVal%

SetLocal EnableDelayedExpansion
If Not "!setVal:%AppData%=!"=="%setVal%" (
    Echo It is somewhere within the %%AppData%% location)
If Not "!setVal:%ProgramData%=!"=="%setVal%" (
    Echo It is somewhere within the %%ProgramData%% location)
EndLocal

Pause