0

I have a code in batch script which will check if the name of the file is same or content of the file is same or not. Also it will updat the ini file to 0 or 1 0 - if the file names are different 1 - if the file names are different

Below is my code

@ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
        CALL :error
        echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
        set /a rungmis=0
        echo Rungmis %rungmis%
        timeout 5
    ) ELSE (
        CALL :next
        echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
        set /a rungmis=1
        echo Rungmis %rungmis%
        timeout 5
    )
)

for %%I in (rungmis) do (
 setlocal enabledelayedexpansion
 type app.ini | find /v "%%I=">settings.tmp
 move /y settings.tmp gmisapp.ini
 echo %%I=!%%I!>>app.ini

)
type app.ini
timeout 5

It is updating the .ini flag (rungmis flag) to 0 or 1. But the problem which i am facing is whenever the ini is getting updated the flag (rungmis) is getting updated at the last line of the ini

Actual app.ini file

[TO_RUN_GMIS]
rungmis=1
;0 means GMIS will run
;1 means GMIS will not run


[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
DataSource=GMIS_DEV_NEW

app.ini after getting updated

[TO_RUN_GMIS]
;0 means GMIS will run
;1 means GMIS will not run


[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
rungmis=1

can anyone please help me out with that?

  • because batch files do not update inline, echo will always append to the last line. You would need to write all of the content line by line and replace the 0 and 1 when needed each time. – Gerhard Jan 11 '19 at 11:23
  • too be honest, what would be easier, create 2 ini files. `app.ini` which will contain setting 0 and `app_backup.ini` which contains value seting 1. then instead of echo'ing the line each time, simply run a 3 way file rename so effectively it is like changing the value inside the file. – Gerhard Jan 11 '19 at 11:29
  • can you help me out with the code? – Riju Mukherjee Jan 11 '19 at 11:34
  • You will need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) with `echo Rungmis %rungmis%` (or if it's for debugging only, replace with `set Rungmis`) – Stephan Jan 11 '19 at 11:35
  • do you require the 2 comments in the ini file, started of with `;` or are they simply there for us? – Gerhard Jan 11 '19 at 12:00
  • they are simply there – Riju Mukherjee Jan 11 '19 at 12:07
  • i am getting it. Can anyone help me out with the code? – Riju Mukherjee Jan 11 '19 at 12:23

1 Answers1

0

Please give this a try.

@ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
        CALL :error
        echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
        set /a rungmis=0
        echo Rungmis %rungmis%
        timeout 5
    ) ELSE (
        CALL :next
        echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
        set /a rungmis=1
        echo Rungmis %rungmis%
        timeout 5
    )
)


for /f "tokens=*" %%a in ('type "app.ini" ^| find /v /n "" ^& break ^> "app.ini"') do (
    set "str=%%a"
    setlocal enabledelayedexpansion
    set "str=!str:*]=!"
    if "!str:~0,7!"=="rungmis" set "str=!str:~0,-1!%rungmis%"
    >>app.ini echo(!str!
    endlocal
)

type app.ini
timeout 5

So we simply do a type on the file and add some additional characters to ensure we replicate newlines as well. We then get rid of those characters before printing to file. But first we search for the string rungmis and replaces its value with the value you determined earlier in the script. Then each line gets printed back into the app.ini file with the replacement value.

Gerhard
  • 22,678
  • 7
  • 27
  • 43