1

I have a script which will check if the file names and the content of the files are same or not, below is the code and it is working fine

ECHO OFF
CLS
for %%i in (C:\Users\f1ym41a\Documents\deep\*.DAT) do (
fc C:\Users\f1ym41a\Documents\deep\MOVE.DAT %%i > NUL
if errorlevel 1 (
        CALL :error
        echo C:\Users\f1ym41a\Documents\deep\MOVE.DAT and %%i are different >>output.log
    ) ELSE (
        CALL :next
        echo C:\Users\f1ym41a\Documents\deep\MOVE.DAT and %%i are same >>output.log
    )
timeout 5
)
PAUSE

What i need to do is if the file names are same then it will change the flag in the ini file to 1. Below is the ini file (deep.ini)

[INI]
flag = 0

Since i am new to batch scripting. Can somebody help me out with this?

  • If the ini file has only two lines, why don't you simply rewrite it? Also if there are several *.DAT files, only the last value will persist. –  Dec 14 '18 at 09:33
  • What have you tried to accomplish the task you describe? Please provide a [mcve]! – aschipfl Dec 14 '18 at 10:18

3 Answers3

0

You can try with replacer.bat:

call replacer.bat move.dat "flag = 0" "flag = 1"
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.

@echo off
if not exist "%USERPROFILE%\Documents\deep\MOVE.DAT" goto :EOF
if not exist "%~dp0jrepl.bat" goto :EOF

call "%~dp0jrepl.bat" "^(flag *= *)0" "$11" /F "%USERPROFILE%\Documents\deep\MOVE.DAT" /O -

The batch file first checks if the file to modify exists at all and immediately exits if this condition is not true, see Where does GOTO :EOF return to?

The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks next if JREPL.BAT really exists in directory of the batch file and exits if this condition is not true.

The meaning of the regular expression search string is:

  • ^ ... find at beginning of a line
  • (...) ... a string referenced in replace string with $1 to keep this part of found string unmodified
  • flag ... case-sensitive the string flag
  •  * ... with 0 or more spaces
  • = ... and an equal sign
  •  * ... and once more 0 or more spaces
  • 0 ... and the character 0.

The replace string back-references the found string between beginning of line and character 0 with $1 and replaces 0 by 1.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains also %~dp0 ... drive and path of argument 0 which is the batch file path always ending with a backslash.
  • echo /?
  • goto /?
  • if /?
  • jrepl.bat /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    I don't want an existing script. Is there any way to include some codes in my script so that it will automatically set the flag to 1 if the file names are same using set command or something like that? – Riju Mukherjee Dec 14 '18 at 09:21
  • @RijuMukherjee - it's really painful to replace text in a file with pure batch. Using external script is really easier. Mind that JREPL is a script ,not a binary – npocmaka Dec 14 '18 at 09:29
  • @RijuMukherjee Other solutions using no other script interpreter than Windows command processor designed for executing commands and not for modifying file contents can be found on Stack Overflow. For example see my answers on [How to update line in existing text file containing a specific string?](https://stackoverflow.com/a/53692295/3074564) and [Modify a string in a .properties file with batch](https://stackoverflow.com/a/53322206/3074564) explaining nearly each line of posted code most likely required by you to write the batch code for this task for which `cmd.exe` is not designed for. – Mofi Dec 14 '18 at 13:32
  • @RijuMukherjee Modifying a text file with pure `cmd.exe` commands is like using [Lineman's pliers](https://en.wikipedia.org/wiki/Lineman's_pliers) to drive nails into a hard board. A nail can be driven into a hard board also with Lineman's pliers more or less hardly depending on size and form of the nail (= string to modify) and the board (= file), but using a hammer (= other scripting language) makes this task much easier, more efficient and most likely also less painful. – Mofi Dec 14 '18 at 13:45
0

![enter image description here :: 1st need remove some possible space in the string to got more precision when compare them: "flag = 0" will became "flag=0", no space and no tab.

:: 2nd for to do this, use this "' (2 characters) to set a variable that use "=" to string instead a special character, by set "'=flag=0" (very old technical!)

:: 3rd treat equal, treat tab character, and to remove it, because some time this is a invisible and possible character that can eventually occur in file dat, see in this question 10878138

:: 4th Compare the strings by string from file by file, line by line...

:: finely You need replace line in the file (.dat or .ini) this part I´m really confuse, but the code are above, sorry if my error!

Obs: use the conversion of this "flag = 0" this this one "flag=0", only for processing comparatives operation, wend the %%i match flag = 0 then only changed to replace to files by flag = 1, bat, a specific thing here is the command fc are comparing %%i, by the same file in looping for with no other file.

@echo off && setlocal EnableExtensions EnableDelayedExpansion

set "'=flag=0"
set _file_new_flag1="%temp%\Flag1.dat"
set _path_to_dats=C:\Users\f1ym41a\Documents\deep\

for /f "delims= " %%T in ('forFiles /p "." /m "%~nx0" /c "cmd /c echo(0x09"') do set "_tab=%%T"

type nul >output.log && set "_tab=%_tab:~0,1%"

cd /d "!_path_to_dats!"

for /f "tokens=* delims= " %%x in ('dir /o-d /on /b "*.dat"') do (

     if defined _file_new_flag ( 

         move /y "!_file_new_flag1!" "!_file_now!"

         set _file_now=<nul
         set "_file_now=%%~x"

        ) else (

         set "_file_now=%%~x"

        )

     call :_file_compare_:

    )

 endlocal & goto :_end_of_file_:

 :_file_compare_:

 for /f "tokens=* delims= " %%X in ('type "!_file_now!"') do (

     for /f "tokens=* delims= " %%i in ('echo/"%%~X"') do (

         set "_to_compare=%%~i"

         call set "_to_compare=!_to_compare: =!"

         for /f "tokens=* delims=" %%I in ('echo/%_tab%') do call set "_to_compare=!_to_compare:%%I=!"

         if ["!_to_compare!"] equ ["%'%"] ( 

             echo/C:\Users\f1ym41a\Documents\deep\MOVE.DAT and %%i are same >>output.log
             echo/%%~i>>!_file_new_flag1!

            ) else (

             echo/C:\Users\f1ym41a\Documents\deep\MOVE.DAT and %%i are different >>output.log
             echo/flag = 1>>!_file_new_flag1!

            )

         timeout /t 5

         set _to_compare=<nul

        ) 

    ) 

exit /b 

:_end_of_file_:

So sorry about my English.

Io-oI
  • 2,514
  • 3
  • 22
  • 29