I am trying to insert an "if not exist" in a windows batch file where another similar IF with the same formatting IS working - can't tell why this one fails after researching and testing.
The second If not exist working as expected. (and third with ELSE)
Formatting is the same, %INIFile% is defined
@echo off
setlocal EnableExtensions Enabledelayedexpansion
set "TODAY=%Date:~4,2%-%Date:~7,2%-%Date:~12,2%"
set "NOW=%time:~0,2%.%time:~3,2%.%time:~6,2%"
set "TempFile=%TEMP%\%~n0.tmp"
set "INIFile=Parameters.ini"
if not exist ".\%INIFile%" (
echo ERROR: List file "%INIFile%" not found.
echo ERROR: List file "%INIFile%" not found.>>%LogFile%
goto :EndBatch
)
:: Get Parameters
call :get-ini %INIFile% Parameters ListFile result
Set "ListFile=%result%"
call :get-ini %INIFile% Parameters Target result
Set "Target=%result%"
call :get-ini %INIFile% Parameters TarDIR result
Set "TarDIR=%result%"
call :get-ini %INIFile% Parameters SectionName result
Set "SectionName=%result%"
call :get-ini %INIFile% Parameters EntryName result
Set "EntryName=%result%"
call :get-ini %INIFile% Parameters NewValue result
Set "NewValue=%result%"
call :get-ini %INIFile% Output LogName result
Set "LogFile=%result%_%EntryName%_%TODAY%_T%NOW%_Log.txt"
XCOPY /Y "%~dp0*_log.txt" "%~dp0%LOGS" 2>nul >nul
ERASE "%~dp0*_log.txt" /Q 2>nul >nul
Echo INI File Updater
Echo INI File Updater>>%LogFile%
Echo ==============================
Echo ==============================>>%LogFile%
Echo PC List: %ListFile%
Echo PC List: %ListFile%>>%LogFile%
Echo INI File: %INIFile%
Echo INI File: %INIFile%>>%LogFile%
Echo Run: %DATE%:%TIME%
Echo Run: %DATE%:%TIME%>>%LogFile%
Echo Target File: %Target%
Echo Target File: %Target%>>%LogFile%
Echo Target DIR: \%TarDIR%
Echo Target DIR: \%TarDIR%>>%LogFile%
Echo INI Section : %SectionName%
Echo INI Section : %SectionName%>>%LogFile%
Echo Search Key: %EntryName%= Update: "%EntryName%=%NewValue%"
Echo Search Key: %EntryName%= Update: "%EntryName%=%NewValue%">>%LogFile%
Echo ==============================
Echo.
Echo ==============================>>%LogFile%
Echo.>>%LogFile%
if not exist ".\%ListFile%" (
echo ERROR: List file "%ListFile%" not found.
echo ERROR: List file "%ListFile%" not found.>>%LogFile%
goto :EndBatch
)
for /F "usebackq delims=" %%I in ("%ListFile%") do call :UpateIniFile "\\%%I\%TarDIR%\%Target%"
goto EndBatch
:UpateIniFile
set "EmptyLines="
set "EntryUpdate="
set "CopyLines="
if not exist %1 (
echo --ERROR-- %1 Unreachable !time:~0,8!
echo --ERROR-- %1 Unreachable !time:~0,8!>>"%LogFile%"
goto :EOF
) ELSE (
for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" %1 2^>nul') do (
set "Line=%%I"
if defined CopyLines (
echo(!Line:*:=!
endlocal
) else if not defined EntryUpdate (
echo(!Line:*:=!
if /I "!Line:*:=!" == "!SectionName!" (
endlocal
set "EntryUpdate=1"
)
) else (
if /I "!Line:*:=!" == "!EntryName!=!NewValue!" (
endlocal
goto ValueExists
)
if "!Line:*:=!" == "" (
endlocal
set /A EmptyLines+=1
) else (
set "Line=!Line:*:=!"
if "!Line:~0,1!!Line:~-1!" == "[]" (
echo !EntryName!=!NewValue!
if defined EmptyLines for /L %%J in (1,1,!EmptyLines!) do echo(
echo !Line!
endlocal
set "EntryUpdate=3"
set "CopyLines=1"
) else (
if defined EmptyLines for /L %%L in (1,1,!EmptyLines!) do echo(
for /F delims^=^=^ eol^= %%J in ("!Line!") do (
if /I not "%%~J" == "!EntryName!" (
echo !Line!
endlocal
) else (
echo !EntryName!=!NewValue!
endlocal
set "EntryUpdate=2"
set "CopyLines=1"
)
)
set "EmptyLines="
)
)
)
)
)>"%TempFile%"
if not defined EntryUpdate (
>>"%TempFile%" echo %SectionName%
>>"%TempFile%" echo %EntryName%=%NewValue%
set EntryUpdate=4
)
if %EntryUpdate% == 1 (
>>"%TempFile%" echo %EntryName%=%NewValue%
set "EntryUpdate=3"
)
move /Y "%TempFile%" %1 2>nul >nul
if errorlevel 1 (
setlocal EnableDelayedExpansion
echo Failed to update: %1 : !time:~0,8!
echo Failed to update: %1 : !time:~0,8!>>"%LogFile%"
del "%TempFile%"
endlocal
goto :EOF
)
if %EntryUpdate% == 2 (
echo !EntryName!=!NewValue! updated in: %1 : !time:~0,8!
echo !EntryName!=!NewValue! updated in: %1 : !time:~0,8!>>"%LogFile%"
goto :EOF
)
if %EntryUpdate% == 3 (
echo !EntryName!=!NewValue! ADDED to: %1 : !time:~0,8!
echo !EntryName!=!NewValue! ADDED to: %1 : !time:~0,8!>>"%LogFile%"
goto :EOF
)
if %EntryUpdate% == 4 (
echo Section+ !EntryName!=!NewValue! to: %1 : !time:~0,8!
echo Section+ !EntryName!=!NewValue! to: %1 : !time:~0,8!>>"%LogFile%"
goto :EOF
)
:ValueExists
echo !EntryName!=!NewValue! existed in: %1 : !time:~0,8!
echo !EntryName!=!NewValue! existed in: %1 : !time:~0,8!>>"%LogFile%"
del "%TempFile%"
goto :EOF
:get-ini <filename> <section> <key> <result>
set %~4=
set insection=
for /f "usebackq eol=; tokens=*" %%a in ("%~1") do (
set line=%%a
if defined insection (
for /f "tokens=1,* delims==" %%b in ("!line!") do (
if /i "%%b"=="%3" (
endlocal
set %~4=%%c
goto :eof
)
)
)
if "!line:~0,1!"=="[" (
for /f "delims=[]" %%b in ("!line!") do (
if /i "%%b"=="%2" (
set insection=1
) else (
endlocal
if defined insection goto :eof
)
)
)
)
:EndBatch
Echo.
Echo.>>%LogFile%
Echo --END--
Echo --END-->>"%LogFile%"
Echo.
Echo --LOGFILE-- %LogFile%
Echo.
endlocal
pause
The first one fails while the second (and third) nearly identical one works
if not exist ".\%INIFile%" (
echo ERROR: List file "%INIFile%" not found.
echo ERROR: List file "%INIFile%" not found.>>%LogFile%
goto :EndBatch
)
Parameters.ini
[Parameters]
ListFile=PCList.txt
Target=SMSStart.ini
TarDIR=Storeman
SectionName=[Maintenance]
EntryName=Reboot
NewValue=1
[Output]
LogName=INI_Update
PCList.txt
LAB-LANE005
LAB-LANE006
LAB-LANE001
LAB-LANE007
LAB-LANE008
Aucun commentaire:
Enregistrer un commentaire