i´ve Problems replacing a string in a loop
For example: I have a File called someBlabla.txt which has multiple lines with random sentences. Now i want to read the file line by line in a for loop and replace a specific char of a word by another. I want to use the shown string replace method, means %myVar:to_replace=replace%
Has someone any hints?
Here is my Script:
REM REPLACE EXAMPLE
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
set file=someBlaBla.txt
set "charToReplace=a"
set "replacer=Z"
for /f "tokens=*" %%a in (%file%) do (
set "replaced_line=%a:%charToReplace%=%replacer%%"
echo replacedLine=!replaced_line!
)
exit /b 0
Here is my textfile:
This is a sentence with an a
this is something
there is a car
and so on and so on
some a a a
Related
I have a text file named myfile.txt and it has the following text-
Hi my cry die fly
I want to delete all the words after cry.
Please help
Something like this do:
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%i in ("myfile.txt") do (
set "line=%%i"
set "line_excl=!line:*cry=!"
call echo %%line:!line_excl!=%%
)
It sets each line in the file as a value to the a variable called %line%, delayedexpansion is needed however, so we enable it and then use the variable as !line!.
We set a search replace variable to be everything up to cry and then simply set echo the !line! by excluding the remaining part, which is everything after cry.
I have a .txt file like this:
Customer Number||Customer Name||Partner Number||Partner Name||Customer Country
1ABC||Jame||1234||Anny||USA
2DEF||Susan||5678||Prissy||UK
My output should be a .csv file with no empty columns.
This is what I tried:
#echo off
setlocal disableDelayedExpansion
set input="C:\a.txt"
set output="C:\Customer_Pipe.csv"
>%output% (
for /f "tokens=*" %%a in ('input"') do (
set line=%%a
setlocal enableDelayedExpansion
echo "!line:|=","!">>"%~2"
)
)
If you want to read the file stored in variable INPUT, you need to read it as %INPUT% to do that (you stated INPUT literally, with odd quotation). When you specify '' within the set of for /F, the part within () is interpreted as a command rather than a file, so for /F tries to execute command input which cannot be found and so the script fails. I stringly recommend to put "" around the specified file, together with the usebackq option, in order to avoid trouble with white-spaces in paths/file names.
If you want to write into the file stored in variable OUTPUT, you must not redirect the echo to somewhere else (you placed >>"%~2" in the code).
Here is the code as I would write it:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "INPUT=%~1"
set "OUTPUT=%~2"
if not defined INPUT exit /B 1
if not defined OUTPUT set "OUTPUT=con"
> "%OUTPUT%" (
for /F usebackq^ delims^=^ eol^= %%L in ("%INPUT%") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
echo(!LINE:^|^|=,!
endlocal
)
)
endlocal
exit /B
The input file for the batch program must be provided as the first command line argument. The second argument (optional) is the output file.
Note that this script does not check whether the input data contains , characters, which impact the way the converted file is treated. If you want to put quotation marks around all fields of the returned CSV data to avoid data interpretation troubles, replace the above echo command line by:
echo("!LINE:||=","!"
The output data (with the original echo command line) looks like this:
Customer Number,Customer Name,Partner Number,Partner Name,Customer Country
1ABC,Jame,1234,Anny,USA
2DEF,Susan,5678,Prissy,UK
The output data with the modified echo command line looks like this:
"Customer Number","Customer Name","Partner Number","Partner Name","Customer Country"
"1ABC","Jame","1234","Anny","USA"
"2DEF","Susan","5678","Prissy","UK"
There are two problems in your code.
1)
You use setlocal EnableDelayedExpansion, that's a good idea, but you have to close it with endlocal else you get an error after ~32 setlocals.
2)
Your replace functions looks a bit odd.
You add some quotes, they will be part of the output later.
You could try this.
echo(!line:^|^|=,!
At all
>%output% (
for /f "tokens=*" %%a in (%input%) do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:||=,!"
(echo(!line!)
endlocal
)
)
I am working on one of the batch file and I have a specific requirement where I have a string "text1,text2,text3,text4...etc" & I would like to have my output as a pyramid like
text1
text1,text2
text1,text2,text3
text1,text2,text3,text4
..etc
My brain says that it is easily achievable, but not getting the logic! How could I do it through batch scripting (for loop)?
The method below allows you to have a list of comma-separated values that may contain spaces or Batch special characters. Of course, it also works with your simple values.
#echo off
setlocal EnableDelayedExpansion
set "string=text one,text <two>,text |three|,text &four&"
set "line="
for %%a in ("%string:,=","%") do (
set "line=!line!%%~a,"
echo !line:~0,-1!
)
Output:
text one
text one,text <two>
text one,text <two>,text |three|
text one,text <two>,text |three|,text &four&
#echo off
setlocal enableextensions enabledelayedexpansion
set "text=text1,text2,text3,text4,text5,text6,text7,text8"
set "output="
for %%a in (%text%) do (
if defined output ( set "output=!output!,%%a" ) else set "output=%%a"
echo(!output!
)
As the values are separated with commas, and commas are a default delimiter in for command, just iterate over the list concatenating the values as they are processed
You can make use of a variable in which on every loop, you'd append a text on it. You'll also need activate delayedexpansion so your variables get updated quick. for /l %%a in (;;) creates an infinite loop.
#echo off
setlocal enabledelayedexpansion
set var=text1
for /l %%a in (;;) do (
echo !var!
set var=!var!,text1
)
Edit: Noticed that your numbers actually increment. You can do:
#echo off
setlocal enabledelayedexpansion
set var=text1
for /l %%a in (2,1,100) do (
echo !var!
set var=!var!,text%%a
)
2 = initial value of %%a
1 = increment per loop
100 = maximum number but only with respect to condition. max number printed is 100 - 1 or 99.
I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)
Please help me to create a script to insert specific text line with multiple numbers of 60 at the end of each line, in a output text file
i have specific text like: specific_text60
when i run the script, the generated output text file content is like:
specific_text60
specific_text120
specific_text180
specific_text240
.....
specific_text64800
and the lines end with the multiple of 60 end at the specified number, in my case 64800
Try this:
(for /l %%i in (60,60,64800) do echo specific_text%%i)>out.txt
#echo off
setlocal enabledelayedexpansion
set /a "incr=60, end=64800"
set text=specific_text
set outfile=output.txt
for /L %%I in (%incr%, %incr%, %end%) do >>"%outfile%" echo(%text%%%I
If you want the number at the beginning of the line instead of the end, move the %%I to the other side of %text% like this:
#echo off
setlocal enabledelayedexpansion
set /a "incr=60, end=64800"
set text=specific_text
set outfile=output.txt
for /L %%I in (%incr%, %incr%, %end%) do >>"%outfile%" echo(%%I%text%