SourceFilePath="D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
How to strip folder names which is just before the file name
From the above example string, i want to get dll_feature,dllfunction,GV into different variables.
#ECHO Off
SETLOCAL
SET "SourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
SET /a count=0
:loop
FOR %%a IN ("%sourcefilepath%") DO SET "part%count%=%%~nxa"&SET "sourcefilepath=%%~dpa"
IF DEFINED part%count% SET /a count +=1&SET "sourcefilepath=%sourcefilepath:~,-1%"&GOTO loop
SET part
SET count
GOTO :EOF
Provided you don'y have weirdo characters in the starting name...
#echo off
setlocal enableextensions disabledelayedexpansion
set "sourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
set "count=10000"
for %%a in ("%sourceFilePath:\=" "%") do (
setlocal enabledelayedexpansion
for %%b in (!count:~-4!) do (
endlocal
set "e_%%b=%%a"
)
set /a "count+=1"
)
rem Show the variables
set e_
Separates each element in the variable replacing the backslash with a space (everything quoted to avoid problems) and for each element, a variable is defined
Numbers in variable names are padded. If not needed just change the initial count value to 0
The one limitation that the MC ND's solution is that you must know the directory depth to get the parts at the end.
Here's an alternate strategy that employs recursion and subroutines. The "subroutine" :SPLIT gets the nth item from the specified path (0 is the right-most) and saves it into the variable specified. The way it's written, if you specify a level that is greater than the number of directory parts, it saves an empty string to the variable. This could be changed easily if that was desirable.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "SourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
FOR /L %%i IN (0,1,10) DO (
CALL :SPLIT "%SourceFilePath%" %%i TGT
#ECHO [%%i] !TGT!
)
EXIT /B
REM Syntax: CALL :SPLIT _path_ _level_
REM Get the _nth_ element of the path (0 is the right-most)
:SPLIT
SETLOCAL ENABLEDELAYEDEXPANSION
SET "PATH=%~1"
SET "LEVEL=%~2"
SET "TARGET_VAR=%~3"
REM Strip off a trailing dir sep (for the %%~ substitution).
IF "%PATH:~-1,1%"=="\" SET "PATH=!PATH:~0,-1!"
FOR %%p IN (%PATH%) DO (
IF /I "%PATH%"=="%%~dp" (
REM BASE CASE
IF "%LEVEL%"=="0" (
SET "_TARGET=%PATH%"
) ELSE (
SET "_TARGET="
)
) ELSE IF "%LEVEL%"=="0" (
REM Return the leaf
SET "_TARGET=%%~nxp"
) ELSE (
REM Recurse on the drive+directory
SET /A SUBLEVEL=LEVEL-1
CALL :SPLIT "%%~dpp" !SUBLEVEL! _TARGET
)
)
ENDLOCAL && SET "%TARGET_VAR%=%_TARGET%"
EXIT /B
The output of this script is:
[0] filename.cpp
[1] dll_feature
[2] dllfunction
[3] GV
[4] Beanch
[5] Site
[6] Code
[7] Projects
[8] D:
[9]
[10]
For the description you give, this invocation would give you something like you want:
CALL :SPLIT "%SourceFilePath%" 0 PART0
#ECHO %PART0%
CALL :SPLIT "%SourceFilePath%" 1 PART1
#ECHO %PART1%
CALL :SPLIT "%SourceFilePath%" 2 PART2
#ECHO %PART2%
Related
I have the following Problem:
I have 2 functions/labels called STARTERand Get_age. The Get_age function stores ages and names in some related variables and shall return the age of the person i passed to it (passed the name).
But the variable which shall store the return value -> means !arr[%%i].age!seems to be empty all the time. I think this is may of the (ENDLOCAL ...) block.
What confuses me too: if i only want to return the %%i inside the (ENDLOCAL ...) block it works fine, but when there are some !exclamation marks! the variable seems to get empty.
How can i now return the age easily without bloating the code? PS: This piece of code is only a small example to give a better view on it.
ThankĀ“s a lot
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM FUNCTION WHICH ASKS STORES A NAME AND ASKS FOR ITS AGE
:STARTER
setlocal
set "person=Niels"
call :Get_age "%person%" "readback"
echo %person% is -%readback%- years old.
pause
exit /b 0
REM FUNCTION WHICH RETURN THE AGE TO THE GIVEN NAME
:Get_age
setlocal
set "searchName=%~1"
set "retVal=%~2"
set "arr[0].name=Niels"
set "arr[0].age=5"
set "arr[1].name=Julia"
set "arr[1].age=2"
set "arr[2].name=Claus"
set "arr[2].age=9"
set "arr_size=2"
REM Go through the arr and return the age to the given name
for /l %%i in (0 1 %arr_size%) do (
if "!arr[%%i].name!" equ "%searchName%" (
(ENDLOCAL
set %retVal%=!arr[%%i].age!
)
)
)
exit /b 0
Perhaps this structure, which enables delayed expansion only when needed, and ends it again as soon as it has no further purpose, will work better for you:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Stores a name, determines their age and returns it.
Set "person=Niels"
Call :Get_Age "%person%" "readback"
Echo %person% is -%readback%- years of age.
Pause
Exit /B 0
:Get_Age
Set "arr[0].name=Niels"
Set "arr[0].age=5"
Set "arr[1].name=Julia"
Set "arr[1].age=2"
Set "arr[2].name=Claus"
Set "arr[2].age=9"
Set "arr_size=2"
For /L %%G In (0,1,%arr_size%) Do (
SetLocal EnableDelayedExpansion
If /I "!arr[%%G].name!" == %1 (
For %%H In ("!arr[%%G].age!") Do (
EndLocal
Set "%~2=%%~H"
)
) Else EndLocal
)
GoTo :EOF
You could store the value and return it after the loop.
set "found="
for /l %%i in (0 1 %arr_size%) do (
if "!arr[%%i].name!" equ "%searchName%" (
set "found=!arr[%%i].age!"
)
)
(
ENDLOCAL
set "%retVal%=%found%"
)
goto :eof
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.
In the following script I want to pass a string via variable and the variable name for an array which should contain substrings to a subroutine.
The subroutine puts substrings of the passed string into an array/list which then should get "returned" by setting it as the value of the 2. passed parameter.
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET testString=Hello World
REM Pass testString and substrings to subroutine
CALL :get_substrings testString substrings
REM For testing. Echo substrings. DOESN'T WORK. substrings is empty!
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
ENDLOCAL
EXIT /B 0
:get_substrings
SETLOCAL ENABLEDELAYEDEXPANSION
SET "string=!%~1!"
REM Alternative approach: Make a connection to %2 rightaway
REM SET "substrings=!%~2!"
REM Process string: Put substrings into indexed array. This works as expected!
FOR /L %%s IN (0,1,2) DO (
SET substrings[%%s]=!string:~0,5!
SET string=!string:~5!
)
REM For testing. Echo the substrings. Works as expected!
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
REM For alternative approach
REM ENDLOCAL
REM End the local the set 2.param = substringsArray
ENDLOCAL & SET %2=%substrings%
EXIT /B 0
Processing the string by creating a array with substrings in the subroutine works as expected. But setting 2. parameters value and keeping the value after subroutine doesn't work...
Notes: The processing of the string is just a dummy. The real process is slightly different but the core with the substrings array is the same. The script is executable right away.
So, how can I get the value substrings back?
This does what you want:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET testString=Hello World
REM Pass testString and substrings to subroutine
CALL :get_substrings testString substrings
REM For testing. Echo substrings.
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
ENDLOCAL
EXIT /B 0
:get_substrings
SETLOCAL ENABLEDELAYEDEXPANSION
SET "string=!%~1!"
REM Process string: Put substrings into indexed array. This works as expected!
FOR /L %%s IN (0,1,2) DO (
SET substrings[%%s]=!string:~0,5!
SET string=!string:~5!
)
REM For testing. Echo the substrings. Works as expected!
FOR /L %%s IN (0,1,2) DO (
ECHO !substrings[%%s]!
)
REM End the local the set 2.param = substringsArray
set SubEnviron=1
for /F "tokens=2* delims=[]=" %%a in ('set substrings[') do (
if defined SubEnviron ENDLOCAL
set "%2[%%a]=%%b"
)
EXIT /B 0
I wasn't able to understand your counting of characters so here's how I'd probably do it:
#Echo Off
SetLocal EnableDelayedExpansion
Set "TestString=Hello World"
For /F "Delims==" %%A In ('Set SubString[ 2^>Nul') Do Set "%%A="
Set "i=1"
Set "SubString[%i%]=%TestString: ="&Set/A i+=1&Set "SubString[!i!]=%"
Set SubString[
Pause
Example Output:
SubString[1]=Hello
SubString[2]=World
Press any key to continue . . .
For the purposes of testing you probably don't need the For loop, its purpose is to ensure there are no existing variables whose name begins with SubString[
Edit
This uses three parameters:
The string to cut%string%
A number of how long each substring should be%chrnum%
The substring parameter%strvar%
#Echo Off
SetLocal EnableDelayedExpansion
Set "string=montuewedthufrisatsun"
Set "chrnum=3"
Set "strvar=substring"
Set "i=1"
Set "_=%string%"
:Loop
Set "!strvar![%i%]=!_:~,%chrnum%!"
If "!_:~%chrnum%!"=="" GoTo Write
Set "_=!_:~%chrnum%!"
Set /A i+=1
GoTo Loop
:Write
Set !strvar![ 2>Nul
Pause
Yes, I fully understand that you are not going to change this code to PowerShell. But, it might be worth considering for the next time given how easy it is. get_substrings is a lambda.
PS C:\src\t\selarr> type .\lamb002.ps1
$teststring = 'hello cruel world'
$get_substrings = { param($t) foreach ($s in $t.split()) { $s.Substring(0,4) } }
$a = & $get_substrings $teststring
$a.length
$a[0]
$a[1]
$a[2]
PS C:\src\t\selarr> .\lamb002.ps1
3
hell
crue
worl
I have 2 CSV files that has
File1:
Column1,column2
data1, data2
File2:
Column3,column4, column5,column6
data3,data4,data5,data6
I have to create a new CSV file that combines both columns from file 1 with the 1st and 3rd columns from file 2 (4 total columns).
Column1,column2,column3,column5
data1,data2,data3,data5
I am looking to do this using batch file commands. Any suggestions?
Code i am using helps me copy one file.
#ECHO OFF
SETLOCAL
(
FOR /f "tokens=1-3delims=," %%a IN (file1.csv) DO (
ECHO(%%a,%%c
)
)>new.csv
GOTO :EOF
How about following script?
File1.csv :
column1,column2
data1,data2
data3,data4
data5,data6
File2.csv :
column3,column4,column5,column6
data3,data4,data5,data6
data7,data8,data9,data10
Script :
#echo off
setlocal enabledelayedexpansion
set ct1=0
for /f "tokens=*" %%i in (File1.csv) do (
set /a ct1+=1
set ar1[!ct1!]=%%i
)
set ct2=0
for /f "tokens=*" %%i in (File2.csv) do (
set /a ct2+=1
set ar2[!ct2!]=%%i
)
if !ct1! lss !ct2! (
set ct=!ct2!
) else (
set ct=!ct1!
)
for /l %%i in (1,1,!ct!) do (
echo !ar1[%%i]!,!ar2[%%i]!>> new.csv
)
new.csv :
column1,column2,column3,column4,column5,column6
data1,data2,data3,data4,data5,data6
data3,data4,data7,data8,data9,data10
data5,data6,
Here is a pure batch solution that works, but with the following limitations and or assumptions:
File 1 lines are terminated by carriage return and linefeed (Windows style)
File 1 lines are no longer than 1021 bytes
File 2 must have a value for each column (no consecutive commas)
File 2 line lengths never exceed ~8191 bytes
File 2 does not have any quoted column values that include commas.
Files 1 and 2 have the same number of lines
Neither file has quoted data values that include new lines (rare, but possible within a CSV).
#echo off
setlocal disableDelayedExpansion
<"file1.csv" >"merged.csv" (
for /f "usebackq eol=, delims=, tokens=1,3" %%A in ("file2.csv") do (
set /p "part1="
set "part2=%%A,%%B"
setlocal enableDelayedExpansion
echo !part1!,!part2!
endlocal
)
)
A much more robust and faster solution is possible if you use PowerShell, JScript, or VBS.
You can also implement an efficient and robust solution using JREPL.BAT - a regular expression text processing utility. JREPL.BAT is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged help.
This JREPL solution has only the following reasonable limitations:
Files 1 and 2 must have the same number of lines
Neither file has quoted data values that include new lines
#echo off
setlocal
set "col=\q(?:\q\q|[^,\q])*\q|[^,]*"
call jrepl "^(%col%),(?:%col%),(%col%)(?:,.*|$)" ^
"$txt=stdin.ReadLine()+','+$1+','+$2" ^
/jq /x /f "file2.csv" /o "merged.csv" <"file1.csv"
This flexible script does what you want, given that the following restrictions are not violated:
both files must contain the same number of lines;
the number of columns per line/row must be equal per each file;
lines are no longer than 1023 bytes, including the terminating line-break;
field/cell values must not contain line-breaks;
each line/row must be terminated by Windows-style line-breaks (CR+LF);
the given column numbers to copy must be sorted in ascending order;
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE1=%~dp0File1.csv" & rem // (1st input CSV file; state `%~1` to use 1st arg.)
set "_FILE2=%~dp0File2.csv" & rem // (2nd input CSV file; state `%~2` to use 2nd arg.)
set "_COLS1=1,2" & rem // (ascending list of columns to copy from 1st file)
set "_COLS2=1,3" & rem // (ascending list of columns to copy from 2nd file)
set "_SEPAR=," & rem // (separator character, usually `,`)
rem // Main routine:
4< "%_FILE1%" 3< "%_FILE2%" (
call :READLINE
)
endlocal
exit /B
:READLINE
rem // Read a line of both files:
set "LINE1=" & set "LINE2="
<&4 set /P LINE1=""
<&3 set /P LINE2=""
rem // Terminate sub-routine in case both lines are empty:
if not defined LINE1 if not defined LINE2 exit /B
rem // Process lines:
call :PROCESS LINE1 LINE2
rem // Repeat reading:
goto :READLINE
exit /B
:PROCESS ref_string1 ref_string2
setlocal DisableDelayedExpansion
set "BUF=%_SEPAR%"
setlocal EnableDelayedExpansion
rem // Test both strings against global wild-card characters:
set "STR1=!%~1!" & set "STR2=!%~2!"
if "!STR1:**=!!STR2:**=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*?=!!STR2:*?=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*<=!!STR2:*<=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*>=!!STR2:*>=!"=="!STR1!!STR2!" goto :PROCESS_CONT
>&2 echo(ERROR: Illegal character encountered^^!
exit /B 1
:PROCESS_CONT
rem // Prepare line strings for being processed by a standard `for` loop:
set "STR1=!STR1:"=""!^"
set "STR2=!STR2:"=""!^"
set "STR1="!STR1:%_SEPAR%=","!""
set "STR2="!STR2:%_SEPAR%=","!""
rem // `for /F` loops to transport prepared line strings beyond `endlocal`:
for /F "delims=" %%E in (^""!STR1!"^") do (
for /F "delims=" %%F in (^""!STR2!"^") do (
endlocal
rem // Process 1st line string:
set /A "IDX=0"
for %%I in (%%~E) do (
rem // Compare column index of current item with given column list:
set /A "IDX+=1" & set "FND="
for %%J in (%_COLS1%) do (
setlocal EnableDelayedExpansion
if !IDX! EQU %%J (
endlocal & set "FND=#"
) else endlocal
)
rem // Matching column index encountered, so assemble output line:
if defined FND (
set "NEW=%%~I%_SEPAR%"
setlocal EnableDelayedExpansion
for /F "delims=" %%K in (^""!BUF!!NEW!"^") do (
endlocal
set "BUF=%%~K"
)
)
)
rem // Process 1st line string:
set /A "IDX=0"
for %%I in (%%~F) do (
rem // Compare column index of current item with given column list:
set /A "IDX+=1" & set "FND="
for %%J in (%_COLS2%) do (
setlocal EnableDelayedExpansion
if !IDX! EQU %%J (
endlocal & set "FND=#"
) else endlocal
)
rem // Matching column index encountered, so assemble output line:
if defined FND (
set "NEW=%%~I%_SEPAR%"
setlocal EnableDelayedExpansion
for /F "delims=" %%K in (^""!BUF!!NEW!"^") do (
endlocal
set "BUF=%%~K"
)
)
)
setlocal EnableDelayedExpansion
)
)
rem // Return output line buffer:
echo(!BUF:~1,-1!
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.