I have a config file on many remote machines that I need to modify through the use of a batch script. The config file has two lines like this:
1_IP = "10.101.34.216"
2_IP = "10.101.34.214"
I simply need to swap the two IP's, but I'm having a hard time.
Ideally, I would write a simple script to search for 10.101.34.216 and replace it with 10.101.34.214 and vice versa - however, if I accidentally run the script on the remote machines in the future it would just revert to the original.
Therefore, I need to set the search parameter to look for exactly 1_IP = "10.101.34.216" and replace it with exactly 1_IP = "10.101.34.214" and then subsequently an exact search for 2_IP = "10.101.34.214" to be replaced with exactly 2_IP = "10.101.34.216"
I'm currently using the following script, which I found on a separate stackoverflow post.
#echo off
setlocal enableextensions disabledelayedexpansion
set "search=%1"
set "replace=%2"
set "textFile=system.cfg"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
However, if I set search parameter to
set search= "1_IP = "10.101.34.216" "
It does not work because of the double quotes. I've tried several approaches to escape the double quotes, but I can't seem to get it to work. Any suggestions?
Thank you in advance for your time, patience, and expertise.
Since the current strings behind 1_IP = and 2_IP = do not seem to matter, I would not try to replace them, but I would simply write the desired strings, like this (see all the explanatory rem comments):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~dp0system.cfg" & rem // (path/name of file to process)
set "_KEY[1]=1_IP" & rem // (1st key to search for)
set "_KEY[2]=2_IP" & rem // (2nd key to search for)
set "_VAL[1]=10.101.34.214" & rem // (1st value to assign to 1st key)
set "_VAL[2]=10.101.34.216" & rem // (2nd value to assign to 2nd key)
rem // Read specified file and iterate through all (non-empty) lines:
for /F delims^=^ eol^= %%L in ('type "%_FILE%" ^& ^> "%_FILE%" rem/') do (
rem // Write (append) to the specified file:
>> "%_FILE%" (
rem // Split key from value (key must not contain spaces):
for /F "eol== delims== " %%K in ("%%L") do (
rem /* If key is a predefined one return respective value;
rem otherwise return the current line unedited: */
if /I "%%K"=="%_KEY[1]%" (
echo(%%K = "%_VAL[1]%"
) else if /I "%%K"=="%_KEY[2]%" (
echo(%%K = "%_VAL[2]%"
) else (
echo(%%L
)
)
)
)
endlocal
exit /B
If you want to be able to predefine an arbitrary number of keys and values, and to retain empty lines, you may want to use this script:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~dp0system.cfg" & rem // (path/name of file to process)
set "_KEY[1]=1_IP" & rem // (1st key to search for)
set "_KEY[2]=2_IP" & rem // (2nd key to search for)
set "_VAL[1]=10.101.34.214" & rem // (1st value to assign to 1st key)
set "_VAL[2]=10.101.34.216" & rem // (2nd value to assign to 2nd key)
rem // Read specified file and iterate through all lines, preceded by line number:
for /F "delims=" %%L in ('findstr /N "^" "%_FILE%" ^& ^> "%_FILE%" rem/') do (
rem // Write (append) to the specified file:
>> "%_FILE%" (
rem // Split line number plus key from value (key must not contain spaces):
for /F "delims== " %%K in ("%%L") do (
rem // Store extracted key and full line, both including line numbers:
set "KEY=%%K" & set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Clear flag, remove line number from key:
set "FLAG=" & set "KEY=!KEY:*:=!"
rem // Loop through all available keys:
for /F "tokens=2 delims=[]=" %%M in ('2^> nul set _KEY[') do (
rem /* If key is a predefined one return respective value;
rem otherwise set flag to indicate key has been found: */
if /I "!KEY!"=="!_KEY[%%M]!%" (
echo(!KEY! = "!_VAL[%%M]!"
set "FLAG=#"
)
)
rem // Return current line unedited in case flag is not set:
if not defined FLAG echo(!LINE:*:=!
endlocal
)
)
)
endlocal
exit /B
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q41578841.txt"
SET "outfile=%destdir%\outfile.txt"
SET "search1=1_IP = \"10.101.34.216\""
SET "search2=2_IP = \"10.101.34.214\""
SET "replace1=1_IP = "10.101.34.214""
SET "replace2=2_IP = "10.101.34.216""
SET "replaced=N"
(
FOR /f "usebackqtokens=1*delims=" %%a IN ("%filename1%") DO (
ECHO(%%a|FINDSTR /x /L /C:"%search1%" >NUL
IF ERRORLEVEL 1 (
ECHO(%%a|FINDSTR /x /L /C:"%search2%" >NUL
IF ERRORLEVEL 1 (ECHO(%%a
) ELSE (SET "replaced=Y"&ECHO(%replace2%
)
) ELSE (SET "replaced=Y"&ECHO(%replace1%
)
)
)>"%outfile%"
IF %replaced%==Y (MOVE "%outfile%" "%filename1%">nul&ECHO made changes
) ELSE (DEL "%outfile%"&echo no changes made
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q41578841.txt containing your data plus some dummy data for my testing.
%outfile% may be used as a temporary file. its name is not relevant, it just needs to not-exist when the job is run.
read each file line. if the line exactly matches (/x) the search-string (/c: since it may contain spaces, /L literally - /i for case-insensitive omitted) then set errorlevel 0. if neither matches, regurgitate the line, else output the replacement line and flag that the replacement took place.
Finally, either replace the file or delete the dummy output file and report.
It's not pretty, but when is Windows Shell script?...:
#echo off
setlocal
set TEXT_FILE=.\system.cfg
set IP_1=
set IP_2=
for /f "tokens=*" %%L in (%TEXT_FILE%) do call :PROCESS_LINE %%L
echo 1_IP = "%IP_2%"
echo 2_IP = "%IP_1%"
endlocal
goto END
:PROCESS_LINE
set PL_LINE=%*
set PL_LINE=%PL_LINE:"=%
if "%PL_LINE:~0,4%" == "1_IP" set IP_1=%PL_LINE:~7%
if "%PL_LINE:~0,4%" == "2_IP" set IP_2=%PL_LINE:~7%
goto END
:END
Related
I am writing a batch script that I need to parse text out of a .csv and have ran into a roadblock:
I have a for-loop set up to grab data from each line (this works fine) but I end up needing a value that is separated by multiple lines. For example (I placed what I want to be considered a single entry in parenthesis for context):
(data I need,flag_for_which_process_to_run,dontcare,"data I need
data continued
data continued
this could continue for any number of lines",dontcare,dontcare,dontcare,dontcare)
(repeat)
Is there any way to get a batch script to parse this out without breaking the for loop? If it's helpful, the data in %%d is encased in double quotes. Code is below, the section I am referring to is the second if inside the for loop.
SETLOCAL EnableDelayedExpansion
for /f "tokens=1,2,3,4 delims=," %%a in (sample.csv) do (
REM Skip if %%b is not flag1
if "%%b"=="flag1" (
.
.
.
)
REM Skip if %%b is not otherflag
if "%%b"=="otherflag" (
REM Set the %%a variable
set device=%%a
echo "%%d"> output\tmp\temp.txt
)
)
Given that the first three tokens/values are unquoted (so they cannot contain quotation marks or commas on their own) and the whole CSV file does not contain escape or back-space characters, the following script, when the CSV file is provided as a command line argument, should extract the values you are interested in (it just echoes them out):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (CSV file; `%~1` is first command line argument)
rem // Get carriage-return character:
for /F %%C in ('copy /Z "%~0" nul') do set "_CR=%%C"
rem // Get line-feed character:
(set ^"_LF=^
%= blank line =%
^")
rem // Get escape and back-space characters:
for /F "tokens=1,2" %%E in ('prompt $E$S$H ^& for %%Z in ^(.^) do rem/') do set "_ESC=%%E" & set "_BS=%%F"
set "CONT="
rem // Read CSV file line by line:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Branch for normal lines:
if not defined CONT (
rem // Get relevant tokens/values:
for /F "tokens=1-3* delims=, eol=," %%A in ("%%L") do (
set "DEVICE=%%A" & set "FLAG=%%B" & set "LINE=%%D"
if not "%%D"=="%%~D" (
rem // Fourth token begins with a `"`, hence remove it and enter branch for continued lines then:
for /F delims^=^ eol^= %%E in ("%%D"^") do set "LINE=%%~E"
set "DATA=" & set "CONT=#"
) else (
rem // Fourth token does not begin with a '"', hence it cannot be continued:
for /F "delims=, eol=," %%E in ("%%D") do (
rem // Do something with the data, like echoing:
echo/
echo FLAG=%%B
echo DEVICE=%%A
echo DATA=%%E
)
)
)
) else set "LINE=%%L"
rem // Branch for continued lines:
if defined CONT (
setlocal EnableDelayedExpansion
rem // Temporarily replace escaped (doubled) `"` with back-space character:
set "LINE=!LINE:""=%_BS%!"
rem // Collect continued data with line-breaks replaced by escape characters:
for /F delims^=^"^ eol^=^" %%D in ("!DATA!%_ESC%!LINE!") do endlocal & set "DATA=%%D"
setlocal EnableDelayedExpansion
if not "!LINE!"=="!LINE:"=!^" (
rem /* There is a single `"` (plus a `,`), which is taken as the end of the continued fourth token;
rem hence replacing back line-breaks and (unescaped) `"`: */
set "DATA=!DATA:*%_ESC%=!" & set "DATA=!DATA:%_BS%="!^"
for %%E in ("!_CR!!_LF!") do set "DATA=!DATA:%_ESC%=%%~E!"
rem // Do something with the data, like echoing:
echo/
echo FLAG=!FLAG!
echo DEVICE=!DEVICE!
echo DATA=!DATA!
endlocal
set "CONT="
) else endlocal
)
)
endlocal
exit /B
i have to search a string from a txt like Pippo.K=5 and replace it with Pippo.K=1. I need to search the entire string. What i did is:
set "search=Pippo.K=5"
set "replace=Pippo.K=1"
set "textFile=%SettingFile%.txt"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
but what i returned is
5=Pippo.K=1=5
How can i fix this error?
The following script constitutes a pure batch-file solution. Supposing it is stored as repl-str.bat, you need to call it like this for your application:
repl-str.bat "%SettingFile%.txt" "Pippo.K=5" "Pippo.K=1" "%SettingFile%.txt"
This specifies the input file %SettingFile%.txt, the literal and case-sensitive search string Pippo.K=5, the replacement string Pippo.K=1 and the output file %SettingFile%.txt that is the same as the input file (the related technique has been taken from this answer: Batch script to find and replace a string in text file without creating an extra output file for storing the modified file). If no output file is given, the result is output to the console (useful for testing). If a fifth command line argument is given (arbitrary value), the search is done in a case-sensitive manner.
Here is the code of the script repl-str.bat:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FILE_I=%~1"
set "SEARCH=%~2"
set "REPLAC=%~3"
set "FILE_O=%~4"
set "CASE=%~5"
set "FLAG=%~6"
if not defined FILE_I exit /B 1
if not defined SEARCH exit /B 1
if not defined FILE_O set "FILE_O=con"
if defined CASE set "CASE=#"
if defined FLAG set "FLAG=#"
for /F "delims=" %%L in ('
findstr /N /R "^" "%FILE_I%" ^& break ^> "%FILE_O%"
') do (
set "STRING=%%L"
setlocal EnableDelayedExpansion
set "STRING=!STRING:*:=!"
call :REPL RETURN STRING SEARCH REPLAC "%CASE%" "%FLAG%"
>> "%FILE_O%" echo(!RETURN!
endlocal
)
endlocal
exit /B
:REPL rtn_string ref_string ref_search ref_replac case flag
setlocal EnableDelayedExpansion
set "STR=!%~2!"
set "SCH=!%~3!"
set "RPL=!%~4!"
if "%~5"=="" (set "OPT=/I") else (set "OPT=")
if not defined SCH endlocal & set "%~1=" & exit /B 1
set "SCH_CHR=!SCH:~,1!"
if not "%~6"=="" set "SCH_CHR="
if "!SCH_CHR!"=="=" set "SCH_CHR=" & rem = terminates search string
if "!SCH_CHR!"==""^" set "SCH_CHR=" & rem " could derange syntax
if "!SCH_CHR!"=="%%" set "SCH_CHR=" & rem % ends variable expansion
if "!SCH_CHR!"=="^!" set "SCH_CHR=" & rem ! ends variable expansion
call :LEN SCH_LEN SCH
call :LEN RPL_LEN RPL
set /A RED_LEN=SCH_LEN-1
set "RES="
:LOOP
call :LEN STR_LEN STR
if not defined STR goto :END
if defined SCH_CHR (
set "WRK=!STR:*%SCH_CHR%=!"
if %OPT% "!WRK!"=="!STR!" (
set "RES=!RES!!STR!"
set "STR="
) else (
call :LEN WRK_LEN WRK
set /A DFF_LEN=STR_LEN-WRK_LEN-1,INC_LEN=DFF_LEN+1,MOR_LEN=DFF_LEN+SCH_LEN
for /F "tokens=1,2,3 delims=," %%M in ("!DFF_LEN!,!INC_LEN!,!MOR_LEN!") do (
rem set "RES=!RES!!STR:~,%%M!"
if defined WRK set "WRK=!WRK:~,%RED_LEN%!"
if %OPT% "!STR:~%%M,1!!WRK!"=="!SCH!" (
set "RES=!RES!!STR:~,%%M!!RPL!"
set "STR=!STR:~%%O!"
) else (
set "RES=!RES!!STR:~,%%N!"
set "STR=!STR:~%%N!"
)
)
)
) else (
if %OPT% "!STR:~,%SCH_LEN%!"=="!SCH!" (
set "RES=!RES!!RPL!"
set "STR=!STR:~%SCH_LEN%!"
) else (
set "RES=!RES!!STR:~,1!"
set "STR=!STR:~1!"
)
)
goto :LOOP
:END
if defined RES (
for /F delims^=^ eol^= %%S in ("!RES!") do (
endlocal
set "%~1=%%S"
)
) else endlocal & set "%~1="
exit /B
:LEN rtn_length ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if defined STR (
set "INT=!STR:~%%L!"
if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
)
)
endlocal & set "%~1=%LEN%"
exit /B
Basically, this approach takes the first character of the search string and looks it up in the input text. At each match, it is checked whether the whole search string occurs. If so, it is replaced by the replacement string by removing as many characters as the search string consists of, hence avoiding sub-string replacement syntax which fails in case the search string contains =, or the search or the replacement string contains % or !.
However, if the first character of the search string is =, ", % or !, the approach is different, the script checks every single character position for occurrence of the search string then, with the disadvantage of reduced overall performance. If a sixth command line argument is given (arbitrary value), this (slow) mode is forced.
Batch variable substring substitution does have limitations. Dealing with literal equal signs is one of them.
powershell "(gc \"%textFile%\") -replace '%search%','%replace%'"
would work. That PowerShell one-liner is a simple alternative to your for /f loop without that limitation.
If you prefer a for /F loop, if your text file is an ini-style file, try this:
#echo off & setlocal
set "searchItem=Pippo.K"
set "searchVal=5"
set "newVal=1"
set "textFile=test.txt"
>"outfile.txt" (
for /f "eol=; usebackq tokens=1* delims==" %%I in ("%textFile%") do (
if /I "%%~I"=="%searchItem%" (
if "%%~J"=="%searchVal%" (
echo %%I=%newVal%
) else echo %%I=%%J
) else (
if not "%%~J"=="" (echo %%I=%%J) else echo %%I
)
)
)
move /y "outfile.txt" "%textFile%"
Be advised that if any of the items in your file has a blank value (e.g. valuename=), the equal sign will be stripped unless you add some additional logic.
You might also consider using ini.bat from this answer.
Sorry I can't word this correctly but I need a command to create a text file that dumps the file names in numeric order. Here is the command that does what I want but everything that has 1 at the start is bunched, 2, 3 etc.....
(for %i in (*.flv) do #echo file '%i') > file.txt
Most cmd/batch file commands, like dir, sort and set, for instance, do pure alphabetic sorting, so any numeric parts are not treated particularly. For example, string12 appears before string3, because the character 1 appears before 2 with the used way of sorting. To change the behaviour so that alpha-numeric sorting is applied, meaning that string3 appears before string12, you have to write your own code for accomplishing that. The following script does exactly that, by padding every numeric part appearing in strings/file names to a fixed amount of digits, in which case alpha-numeric and alphabetic sort orders match. Below is the code, including explanatory remarks. As you can see, complex code is required to accomplish the task and to make the script secure against all characters that have special meanings to cmd (like SPACE, ,, ;. = as well as ^, &, (, ), %, !):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_TEMPFILE=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (temporary file used for sorting)
set /A "_DIGITS=12" & rem // (number of digits every numeric part is padded for sorting)
rem // Build string containing enough zeros for padding:
set "$PAD=" & setlocal EnableDelayedExpansion
for /L %%D in (1,1,%_DIGITS%) do set "$PAD=!$PAD!0"
endlocal & set "$PAD=%$PAD%"
rem // Prepare temporary file containing data to sort:
> "%_TEMPFILE%" (
rem // Gather and resolve all command line arguments:
for %%I in (%*) do (
rem // Get pure file name:
set "ITEM=%%~nxI"
rem // Extend all numeric parts in file name to certain number of digits:
call :PROCESS AUGM ITEM || >&2 echo ERROR: potential problem sorting "%%~nxI"!
rem // Write extended and original file name into temporary file:
setlocal EnableDelayedExpansion
echo(!AUGM!^|!ITEM!
endlocal
)
)
rem // Return content of temporary file in ascendingly sorted order:
for /F "tokens=2 delims=| eol=|" %%J in ('sort "%_TEMPFILE%"') do (
rem // Simply return each item:
echo(%%J
)
rem // Delete temporary file:
del "%_TEMPFILE%"
endlocal
exit /B
:PROCESS rtn_augmented_string ref_string
rem /* Routine to augment a string so that every numeric part is padded with leading
rem zeros to the left to hold a predefined number of digits: */
setlocal DisableDelayedExpansion
set "#RTN=%~1"
set "#ARG=%~2"
rem // Initialise required variables:
set "COLL="
set "ERRL=0"
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%B in (^""!%#ARG%!"^") do (
endlocal
set "PSTR=%%~B"
setlocal EnableDelayedExpansion
)
rem // Entry point for loop to handle one numeric string part:
:REPEAT
rem // Extract the string portions before and after the first numeric part:
for /F "tokens=1,* delims=0123456789 eol=0" %%A in ("+!PSTR!") do (
endlocal
set "PART=%%A"
set "NEXT=%%B"
rem // Determine length of string portion before first numeric part:
call :LENGTH PLEN PART
set /A "PLEN-=1"
setlocal EnableDelayedExpansion
set "PART=!PART:~1!"
rem // Split off string portion before first numeric part from total string:
for %%C in (!PLEN!) do (
if defined PSTR set "PSTR=!PSTR:~%%C!"
)
rem /* Splitt off string portion after first numeric part from remaining string;
rem this is nothing but extracting the first numeric part itself: */
call :SPLIT PNUM NEXT PSTR
rem // Determine the actual length of the numeric part:
call :LENGTH NLEN PNUM
rem // Do the actual padding with leading zeros of the numeric part:
if defined PNUM (
set "PNUM=%$PAD%!PNUM!"
set "PNUM=!PNUM:~-%_DIGITS%!"
)
rem // Store the part after the first numeric part:
for /F delims^=^ eol^= %%C in (^""!NEXT!"^") do (
rem /* Build string with the string portion before the current numeric part
rem and the padded current numeric part itself: */
for /F delims^=^ eol^= %%D in (^""!COLL!!PART!!PNUM!"^") do (
rem // Check whether the predefined number of padding digits is sufficient:
for /F %%E in ("!NLEN!") do (
endlocal
set "PSTR=%%~C"
set "COLL=%%~D"
if %%E GTR %_DIGITS% set "ERRL=1"
setlocal EnableDelayedExpansion
)
)
)
)
rem // Repeat the whole approach while there is still a remaining string portion:
if defined PSTR goto :REPEAT
rem // Return the string with padded numeric parts:
for /F delims^=^ eol^= %%R in (^""!COLL!"^") do (
endlocal
endlocal
set "%#RTN%=%%~R"
exit /B %ERRL%
)
exit /B
:SPLIT rtn_left_string ref_split_char val_string
rem /* Routine to split a string at the first occurrence of a certain character and to
rem return the portion before it: */
setlocal DisableDelayedExpansion
set "#RTN=%~1"
set "#CHR=%~2"
set "#ARG=%~3"
rem // Initialise required variables:
setlocal EnableDelayedExpansion
set "CHAR= " & if defined %#CHR% set "CHAR=!%#CHR%:~,1!"
if "!CHAR!"=="<" (set "PREF=>") else (set "PREF=<")
rem // Check whether a split character is defined:
if defined %#CHR% (
rem /* Split character available, so split off first occurrence and everything after
rem from the original string: */
for /F eol^=^%CHAR%^ delims^=^%CHAR% %%C in ("%PREF%!%#ARG%!") do (
endlocal
set "%#RTN%=%%C"
setlocal EnableDelayedExpansion
set "%#RTN%=!%#RTN%:~1!"
)
) else (
rem // No split character defined, so do not split off anything:
set "%#RTN%=!%#ARG%!"
)
rem // Return the resulting string:
for /F delims^=^ eol^= %%R in (^""!%#RTN%!"^") do (
endlocal
endlocal
set "%#RTN%=%%~R"
)
exit /B
:LENGTH rtn_length ref_string
rem /* Routine to determine the length of a given string: */
setlocal DisableDelayedExpansion
set "#RTN=%~1"
set "#ARG=%~2"
setlocal EnableDelayedExpansion
rem // Check whether a string is provided:
if defined %#ARG% (
rem // String is available, so calculate its length:
set /A "%#RTN%=1"
for %%A in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if not "!%#ARG%:~%%A!"=="" (
set /A "%#RTN%+=%%A"
set "%#ARG%=!%#ARG%:~%%A!"
)
)
) else (
rem // String is empty, so length is zero:
set /A "%#RTN%=0"
)
rem // Return the computed length:
for /F %%R in ("!%#RTN%!") do (
endlocal
endlocal
set "%#RTN%=%%R"
)
exit /B
Provide (a) file pattern(s) as (a) command line argument(s), like this, for example (supposing the batch file is saved as sort-alpha-num.bat):
sort-alpha-num.bat "*.flv"
To store the resulting sorted list in a text file called file.txt, use this:
sort-alpha-num.bat "*.flv" > "file.txt"
In case an error message like ERROR: potential problem sorting "1000000000000.flv"! appears, increase the number of digits on top of the script (see line set /A "_DIGITS=12").
Since you didn't give us more information about how your output file should be ?
#echo off
set "folder=%userprofile%\Desktop\*.flv"
set "listfiles=listfiles.txt"
If Exist %listfiles% Del %listfiles%
setLocal EnableDelayedExpansion
Rem Populate the array with existent files in this folder
for /f "tokens=* delims= " %%a in ('Dir /s /b /a:-d "%folder%"') do (
set /a Count+=1
set "File[!Count!]=%%~na"
)
::******************************************************************
:Display_Files
cls & color 0B
echo(
For /L %%i in (1,1,%Count%) do (
echo %%i - !File[%%i]!
)
echo(
(
rem to save result into logfile
For /L %%i in (1,1,%Count%) do (
echo %%i - !File[%%i]!
)
)>> %listfiles%
echo Hit any key to open %listfiles% :
Pause>nul & Start "" %listfiles%
I'm trying to write a batch file to increment version number every time i run it, but I'm confuse about "for /f" and the behaviour of the batch file when I test it by using command prompt. Please help me with this.
here's my batch file
for /f "tokens=2,3 " %%i in (version.h) do (
set /a number=%%j+1
echo %%i
echo %%j
echo %number%
if %%i==REVISION (
echo line1
echo #define %%i "%number%" >> temp.file
) else (
echo line2
echo #define %%i %%j >> temp.file
)
)
del /q version.h
ren temp.file version.h
and here's my version.h
#define MAJOR "1"
#define MINOR "0"
#define REVISION "242"
The batch file can only produce correct result at the first run(#define REVISION "243"), and has a weird result at the second run(#define REVISION "0"). The third run's result is correct("#define REVISION "244"), but the forth run it goes weird again(#define REVISION "1"), and so on.
It seems that I didn't parse the correct string so I cannot have correct result every time.
I typed "for /?" in the command prompt and read the help message, but still cannot understand it, please help me with this. Any reply would be appreciate!
The following script does what you want and preserves empty lines and special characters present in the target file. There is no temporary file involved, the file modification is accomplished in-place.
So here is the code -- reference the explanatory remarks for how it works:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILE=%~1" & rem // (provide the target file as command line argument)
set "DIRECTIVE=#define" & rem // (name of the directive to search)
set "DEFINITION=REVISION" & rem // (name of the definition to search)
set "CASESENS=" & rem // (set to non-empty for case-sensitive searches)
set "QUOTED="^" & rem // (set to non-empty for quoting returned number)
rem // Resolve arguments and options:
if not defined FILE ((>&2 echo ERROR: no file specified!) & exit /B 1)
if defined CASESENS (set "CASESENS=") else (set "CASESENS=/I")
if defined QUOTED (set "QUOTED="^") else (set "QUOTED=")
rem // Loop through all lines in the target file:
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%L in ('
rem/ /* Prefix lines with line numbers to not lose empty ones; ^& ^
rem/ after having read file, deplete its entire content: */ ^& ^
findstr /N /R "^^" "!FILE!" ^& ^> "!FILE!" break
') do (
endlocal
set "FLAG="
set "LINE=%%L"
rem // Split line into three tokens:
for /F "tokens=1-3 eol= " %%I in ("%%L") do (
set "FIELD1=%%I"
set "FIELD2=%%J"
set "NUMBER=%%~K"
setlocal EnableDelayedExpansion
rem // Check first token for matching directive name:
if %CASESENS% "!FIELD1:*:=!"=="!DIRECTIVE!" (
rem // Check second token for matching definition name:
if %CASESENS% "!FIELD2!"=="!DEFINITION!" (
endlocal
rem // Increment number of third token:
set /A "NUMBER+=1"
set "FLAG=#"
setlocal EnableDelayedExpansion
)
)
endlocal
)
setlocal EnableDelayedExpansion
rem // Write output line into target file:
>> "!FILE!" (
rem // Check whether dirctive and definition matched:
if defined FLAG (
rem // Match found, so write new line with incremented number:
echo(!DIRECTIVE! !DEFINITION! %QUOTED%!NUMBER!%QUOTED%
) else (
rem // No match found, so write original line:
echo(!LINE:*:=!
)
)
)
endlocal
endlocal
exit /B
i have to search a string from a txt like Pippo.K=5 and replace it with Pippo.K=1. I need to search the entire string. What i did is:
set "search=Pippo.K=5"
set "replace=Pippo.K=1"
set "textFile=%SettingFile%.txt"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
but what i returned is
5=Pippo.K=1=5
How can i fix this error?
The following script constitutes a pure batch-file solution. Supposing it is stored as repl-str.bat, you need to call it like this for your application:
repl-str.bat "%SettingFile%.txt" "Pippo.K=5" "Pippo.K=1" "%SettingFile%.txt"
This specifies the input file %SettingFile%.txt, the literal and case-sensitive search string Pippo.K=5, the replacement string Pippo.K=1 and the output file %SettingFile%.txt that is the same as the input file (the related technique has been taken from this answer: Batch script to find and replace a string in text file without creating an extra output file for storing the modified file). If no output file is given, the result is output to the console (useful for testing). If a fifth command line argument is given (arbitrary value), the search is done in a case-sensitive manner.
Here is the code of the script repl-str.bat:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FILE_I=%~1"
set "SEARCH=%~2"
set "REPLAC=%~3"
set "FILE_O=%~4"
set "CASE=%~5"
set "FLAG=%~6"
if not defined FILE_I exit /B 1
if not defined SEARCH exit /B 1
if not defined FILE_O set "FILE_O=con"
if defined CASE set "CASE=#"
if defined FLAG set "FLAG=#"
for /F "delims=" %%L in ('
findstr /N /R "^" "%FILE_I%" ^& break ^> "%FILE_O%"
') do (
set "STRING=%%L"
setlocal EnableDelayedExpansion
set "STRING=!STRING:*:=!"
call :REPL RETURN STRING SEARCH REPLAC "%CASE%" "%FLAG%"
>> "%FILE_O%" echo(!RETURN!
endlocal
)
endlocal
exit /B
:REPL rtn_string ref_string ref_search ref_replac case flag
setlocal EnableDelayedExpansion
set "STR=!%~2!"
set "SCH=!%~3!"
set "RPL=!%~4!"
if "%~5"=="" (set "OPT=/I") else (set "OPT=")
if not defined SCH endlocal & set "%~1=" & exit /B 1
set "SCH_CHR=!SCH:~,1!"
if not "%~6"=="" set "SCH_CHR="
if "!SCH_CHR!"=="=" set "SCH_CHR=" & rem = terminates search string
if "!SCH_CHR!"==""^" set "SCH_CHR=" & rem " could derange syntax
if "!SCH_CHR!"=="%%" set "SCH_CHR=" & rem % ends variable expansion
if "!SCH_CHR!"=="^!" set "SCH_CHR=" & rem ! ends variable expansion
call :LEN SCH_LEN SCH
call :LEN RPL_LEN RPL
set /A RED_LEN=SCH_LEN-1
set "RES="
:LOOP
call :LEN STR_LEN STR
if not defined STR goto :END
if defined SCH_CHR (
set "WRK=!STR:*%SCH_CHR%=!"
if %OPT% "!WRK!"=="!STR!" (
set "RES=!RES!!STR!"
set "STR="
) else (
call :LEN WRK_LEN WRK
set /A DFF_LEN=STR_LEN-WRK_LEN-1,INC_LEN=DFF_LEN+1,MOR_LEN=DFF_LEN+SCH_LEN
for /F "tokens=1,2,3 delims=," %%M in ("!DFF_LEN!,!INC_LEN!,!MOR_LEN!") do (
rem set "RES=!RES!!STR:~,%%M!"
if defined WRK set "WRK=!WRK:~,%RED_LEN%!"
if %OPT% "!STR:~%%M,1!!WRK!"=="!SCH!" (
set "RES=!RES!!STR:~,%%M!!RPL!"
set "STR=!STR:~%%O!"
) else (
set "RES=!RES!!STR:~,%%N!"
set "STR=!STR:~%%N!"
)
)
)
) else (
if %OPT% "!STR:~,%SCH_LEN%!"=="!SCH!" (
set "RES=!RES!!RPL!"
set "STR=!STR:~%SCH_LEN%!"
) else (
set "RES=!RES!!STR:~,1!"
set "STR=!STR:~1!"
)
)
goto :LOOP
:END
if defined RES (
for /F delims^=^ eol^= %%S in ("!RES!") do (
endlocal
set "%~1=%%S"
)
) else endlocal & set "%~1="
exit /B
:LEN rtn_length ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if defined STR (
set "INT=!STR:~%%L!"
if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
)
)
endlocal & set "%~1=%LEN%"
exit /B
Basically, this approach takes the first character of the search string and looks it up in the input text. At each match, it is checked whether the whole search string occurs. If so, it is replaced by the replacement string by removing as many characters as the search string consists of, hence avoiding sub-string replacement syntax which fails in case the search string contains =, or the search or the replacement string contains % or !.
However, if the first character of the search string is =, ", % or !, the approach is different, the script checks every single character position for occurrence of the search string then, with the disadvantage of reduced overall performance. If a sixth command line argument is given (arbitrary value), this (slow) mode is forced.
Batch variable substring substitution does have limitations. Dealing with literal equal signs is one of them.
powershell "(gc \"%textFile%\") -replace '%search%','%replace%'"
would work. That PowerShell one-liner is a simple alternative to your for /f loop without that limitation.
If you prefer a for /F loop, if your text file is an ini-style file, try this:
#echo off & setlocal
set "searchItem=Pippo.K"
set "searchVal=5"
set "newVal=1"
set "textFile=test.txt"
>"outfile.txt" (
for /f "eol=; usebackq tokens=1* delims==" %%I in ("%textFile%") do (
if /I "%%~I"=="%searchItem%" (
if "%%~J"=="%searchVal%" (
echo %%I=%newVal%
) else echo %%I=%%J
) else (
if not "%%~J"=="" (echo %%I=%%J) else echo %%I
)
)
)
move /y "outfile.txt" "%textFile%"
Be advised that if any of the items in your file has a blank value (e.g. valuename=), the equal sign will be stripped unless you add some additional logic.
You might also consider using ini.bat from this answer.