我正在编写一个Windows批处理脚本,它试图确定路径值(从应用程序的配置文件中提取)是指向用户特定的文件夹(例如%APPDATA%)还是一般的(例如C: \ ProgramData \ AppName的)。
这种冗长的方式是尝试检查每一个可能设置的wya,例如这就是我开始编写的代码:
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%"
)
但是,在我继续这种方法之前,我想问一下是否有简单的单行可以告诉我一个给定的pat是否会映射到用户特定的文件夹。还要记住,在某些系统上,用户文件夹可以是网络UNC路径。
答案 0 :(得分:0)
仅根据您的问题确定:
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