Using FINDSTR to set variables in FOR Loop - windows

(EDITED) How can I read data from a text file and assign certain parts of it to variables? In the lines below, for example, I want to assign the data to variables where indicated but ignore the header line, the preface word "DATA:" and any quotation marks.
HEADER (ignore)
DATA: "set text inside quotes to VAR1"
DATA: "set text inside quotes to VAR2"
DATA (etc.)
The code I'm using is:
setlocal enabledelayedexpansion
set COUNT=0
for /f "usebackq delims=" %%a in ("data.txt") do (
set "INPUT=%%a"
set /a COUNT=!COUNT!+1
echo !INPUT! | findstr /i /c:"HEADER" > nul
if errorlevel 1 (
set "INPUT=!INPUT:DATA:=!"
set "INPUT=!INPUT:"=!"
set VAR!COUNT!=!INPUT!
echo Variable !COUNT! = !VAR!!COUNT!
)
)
My expected output is
Variable 1 = set text inside quotes to VAR1
Variable 2 = set text inside quotes to VAR2
but what I get is
Variable 2 = 2
Variable 3 = 3
Variable 4 = 4
What am I doing wrong?

I took your code and fixed it up.
In particular:
I switched the findstr to find all lines that do not match HEADER
I used arrays to store the variables.
It may need some minor tweaks to get it exactly as you want it, but I think I've given you a working outline.
#echo off
setlocal enabledelayedexpansion
set COUNT=0
for /f "tokens=*" %%a in ('findstr /i /v HEADER data.txt') do (
set "INPUT=%%a"
set /A COUNT=!COUNT!+1
set "INPUT=!INPUT:DATA:=!"
set VAR[!COUNT!]=!INPUT!
call echo Variable !COUNT! = %%VAR[!COUNT!]%%
)
Output:
Variable 1 = "set text inside quotes to VAR1"
Variable 2 = "set text inside quotes to VAR2"
Variable 3 = DATA (etc.)
To match lines that have leading spaces, use switches:
findstr /V /R /C:"^ *HEADER"
The regular expression means:
^ : Beginning of line
[space] : a literal space
* : the previous character (space) repeated as many times as needed
HEADER : the text to find after leading spaces
The switches mean:
/R Use Regular Expressions
/C: Use this search-string
/V Show all lines that do NOT match this expression.

Related

How to set one variable equal to the sum of 2 other variables in cmd

My code is
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"== "" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set /a _nextmm=_mm+1
)
:s_done
What I want is this variable "_nextmm" = this variable "_mm" + 1
But when I run this code. It's result is
IF "2021" == "" goto s_done
Set _yyyy = 2021
Set _mm = 008
Set /a _nextmm = _mm+1
And I call echo %_nextmm%. The result is 1 instead of 009.
What did I do wrong in here?
I hope there are no SPACEs around the equal-to signs in your actual set command lines as they would harm. The best way is this syntax:
rem /* No spaces around `=`-sign, and quotes around the whole assignment expression,
rem which avoids unwanted training whitepsaces and protects special characters;
rem without assigning the quotes themselves to the variable value: */
set "VAR=Value"
Anyway, use:
set /A "_nextmm=%%J+1"
instead of set /A _nextmm=_mm+1, because (as I assume) %%J is 8, which is a correct decimal number, but _mm is 008, which is treated as an octal number due to the leading zeros (yes, I know, this is not intuitive, but take a look at this), which is invalid as there are only digits 0 to 7, hence 0 is taken for further arithmetic operations.
If you have your echo command line within the body of the for /F loop (so before the closing )), use:
for /F … (
…
call echo %%_nextmm%%
)
or use delayed variable expansion:
setlocal EnableDelayedExpansion
for /F … (
…
echo !_nextmm!
)
endlocal
If you have your echo command line outside of the body of the for /F loop (so after the closing )), normal or immediate expansion will work as expected:
for /F … (
…
)
echo %_nextmm%
TBH, I'm not sure of your logic, because if this was done in December 2021, then adding 1 would make the month incorrectly 13, and the year would remain as 2021.
For that reason, it would be much better if you just used PowerShell to assist you instead of WMIC. PowerShell sees dates as objects not strings, so you can perform the math directly in order to define the required values for your variables.
For /F "Tokens=1-2" %%G In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile
-Command "(Get-Date).AddMonths(1).ToString('yyyy MM')"') Do (Set "_yyyy=%%G"
Set "_MM=%%H")
If you wanted to do it without splitting the line up for better reading then:
For /F "Tokens=1-2" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "(Get-Date).AddMonths(1).ToString('yyyy MM')"') Do Set "_yyyy=%%G" & Set "_MM=%%H"

batch replace spaces between quotes in file and then remove all quotes

I want create a batch to replace spaces with a + sign if the space is in between quotes. Then I want to remove the quotes from a text file. How can I accomplish this?
So I want to change a line like this:
2016-01-11 14:45:09 Server 127.0.0.1 GET /global/images/logo_small.jpg - 80 - 173.252.120.117 "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-" www.vietnam.ttu.edu 200 200 200 1868 0
To this line.
2016-01-11 14:45:09 Server 127.0.0.1 GET /global/images/logo_small.jpg - 80 - 173.252.120.117 facebookexternalhit/1.1+(+http://www.facebook.com/externalhit_uatext.php) - www.vietnam.ttu.edu 200 200 200 1868 0
Thanks
You could use JREPL.BAT to arrive at a very concise and efficient solution. JREPL is a pure script based (JScript/batch) regular expression text processing utility that runs on any version of Windows from XP onward.
jrepl "\q| " "q=!q;''|q?'+':' '" /j /x /t "|" /jbegln "q=false" /f test.txt /o -
For this solution I use the /T option, which is very similar to the unix tr utility, or the sed y command.
I define two search terms, the first for a quote (The \X option enables the \q escape sequence), and the second for a space.
The /J option treats replacement strings as JScript. The first replacement string for the quote toggles a "q" variable TRUE or FALSE, and replaces the quote with an empty string. The second replacement string conditionally replaces the space with a plus or space, depending on the state of the "q" variable.
The /JBEGLN option initializes the "q" variable to FALSE at the beginning of each line.
The /F option specifies the input file, and the /O - option specifies that the output overwrites the original file.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q34732271.txt"
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO ECHO %%a&SET "line="&CALL :process %%a
GOTO :EOF
:process
SET "addition=%~1"
IF not DEFINED addition ECHO %line:~1%&GOTO :eof
IF "%~1"==%1 (
REM quoted
SET "line=%line% %addition: =+%"
) ELSE (
SET "line=%line% %addition%"
)
shift
GOTO process
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q34732271.txt containing your data for my testing.
The echo %%a shows your one line of data on the screen and the echo within the :process routine shows that line processed.
Batch is not an ideal language to process strings as it exhibits sensitivity to many symbols. This process should work provided you are happy to have space-strings compressed and the source string does not contain , ;,tab % or any other symbol that cmd treats specially.
Here is a pure batch-file solution that walks through the characters in each line in file line.txt, replaces all SPACEs in between a pair of quotation marks "" by + signs and stores the result in text_new.txt. The input string may contain any characters, even special ones:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define global constants here:
set "INFILE=line.txt"
set "OUTFILE=line_new.txt"
set "SEARCH= "
set "REPLACE=+"
set "KEEPQUOTES="
set "QUOTE="""
set "QUOTE=%QUOTE:~,1%"
set "QFLAG="
> "%OUTFILE%" (
for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
set "LINE=%%L"
call :SUB LINE
)
)
endlocal
exit /B
:SUB
setlocal EnableDelayedExpansion
set "LINE=!%1!"
set "LINENEW="
set /A "POS=0"
:LOOP
set "CHAR=!LINE:~%POS%,1!"
set /A "POS+=1"
if not defined CHAR (
echo(!LINENEW!
endlocal
exit /B
)
if "!CHAR!"=="!QUOTE!" (
if defined QFLAG (
set "QFLAG="
) else (
set "QFLAG=Quoted"
)
if defined KEEPQUOTES (
set "LINENEW=!LINENEW!!CHAR!"
)
) else if defined QFLAG (
if "!CHAR!"=="!SEARCH!" (
set "LINENEW=!LINENEW!!REPLACE!"
) else (
set "LINENEW=!LINENEW!!CHAR!"
)
) else (
set "LINENEW=!LINENEW!!CHAR!"
)
goto :LOOP

Cannot use !random! in batch file

#echo off
setlocal enableDelayedExpansion
set /p startIndex="Start index: "
set /p endIndex="End index: "
for /l %%a in (%startIndex% 1 %endIndex%) do (
set /a seed = !random!
echo %seed%
)
set /p endfile="Wait for it...."
I expect that this script will print out some random numbers. But it does not work. It just printed out some lines with same content: "Echo is off ."
How can I fix this code ?
You need to say
echo !seed!
because the whole loop is parsed at the beginning, and seed is expanded (to nothing, as it doesn't exist yet) before the loop starts running. You therefore need to use delayed expansion.
Delayed expansion is also required on referencing the value of variable seed defined and assigned with a random value within a block defined with ( ... ).
#echo off
setlocal EnableDelayedExpansion
set /p "startIndex=Start index: "
set /p "endIndex=End index: "
for /l %%a in (%startIndex% 1 %endIndex%) do (
set "seed=!random!"
echo !seed!
)
set /p "endfile=Wait for it ..."
Further option /a is not necessary to assign a random number to variable seed as no arithmetic expression to evaluate. But be careful with spaces around equal sign. All spaces are ignored by set on using option /a, but are not anymore ignored by command set on a simple assignment without option /a.
And also take care about where first double quote is written on line with command set as this makes a big difference.
For details about spaces around equal sign and first double quote position see answer on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?

string comparison in batch file not working

I have this code:
SETLOCAL enabledelayedexpansion enableextensions
SET OutofService=Weblog_Doc(file:///C:/Bla/BlaKon.htm?send=2000)
REM - Declare and set the Array
FOR /f "delims=" %%a IN (C:\Folder1\log.dat) DO (
SET /a c+=1
SET x[!c!]=%%a
)
REM - Read Array from end to start
FOR /l %%I IN (!c! -1 1) DO (
SET _result=!x[%%I]:~-47%!
ECHO !_result!
ECHO !OutofService!
IF "!_result!"=="!OutofService!" (
ECHO yahooo!
)
)
The string in file log.dat is as follows:
09/17/15 15:18:52:577 Container:
Weblog_Doc(file:///C:/Bla/BlaKon.htm?send=2000)
My ECHO statement is outputting the result:
Weblog_Doc(file:///C:/Bla/BlaKon.htm?send=2000)
Weblog_Doc(file:///C:/Bla/BlaKon.htm?send=2000)
The if statement is returning false and not outputting yahooo! as it should, given the fact that these two strings should be equal. What am I doing wrong?
SET x[!c!]=%%a
^^^^^^^^^^ Spaces included in value
....
SET _result=!x[%%I]:~-47%!
^^^^^^ Spaces included in value
Try with (percent sign in the third line is not needed)
SET "OutofService=Weblog_Doc(file:///C:/Bla/BlaKon.htm?send=2000)"
....
SET "x[!c!]=%%a"
....
SET "_result=!x[%%I]:~-47!"
This way, while the assignment operation is quoted, the quotes are not included in the value, nor the ending spaces if present.

How to extract the numbers after a character in windows batch files

Hi I do need to extract the last part of a string after the last dot
Example:
1.2.37 ==> I need the 37
1.2.567 ==> I need the 567
as you can see the number of characters after the dot is not fixed so expressions like
base=%fullver:~0,-2%
Can't be used. How can I achieve this?
#echo off
setlocal enableextensions disabledelayedexpansion
set "fullver=1.2.456"
for %%a in ("%fullver:.=\%") do set "base=%%~na"
echo %base%
The trick is to replace the dots with backslashes, process the string as a path and retrieve the name of the last element in it.
Alternatively, if all the elements need to be retrieved, instead of a for, a for /f is used to tokenize the variable using the dots as separators
#echo off
setlocal enableextensions disabledelayedexpansion
set "fullver=1.2.456"
for /f "tokens=1-3 delims=." %%a in ("%fullver%") do (
set "major=%%a"
set "minor=%%b"
set "build=%%c"
)
echo [%major%] [%minor%] [%build%]
I found the following question which actually tokenizes the string.
How to split a string in a Windows batch file?
May be you can try using this to delimit it with "." and take the last value stored in the string variable. Not sure if there is a simple way, but this works.
Here is an edited Version to fit your Needs:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=1.2.5.234
for /f "tokens=1 delims=." %%a IN ("!teststring!") DO set firststring=%%a
echo !firststring!
REM Do something with each substring
:stringLOOP
REM Stop when the string is empty
if "!teststring!" EQU "" goto END
for /f "delims=." %%a in ("!teststring!") do set substring=%%a
REM Now strip off the leading substring
:striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1!
if "!teststring!" EQU "" goto stringloop
if "!stripchar!" NEQ "." goto striploop
goto stringloop
:END
echo !substring!
endlocal
I prefer MC ND's answer if you are looking for only the last node, or if you know how many nodes there are.
Here is a method to capture all nodes if the total number of nodes is unknown:
#echo off
setlocal enableDelayedExpansion
set "fullver=1.2.456"
:: Parse each node and store in an "array"
set cnt=0
for %%A in (%fullver:.= %) do (
set /a cnt+=1
set "node.!cnt!=%%A"
)
:: Show the results
for /l %%N in (1 1 %cnt%) do echo node.%%N = !node.%%N!
Another solution! This one gets the first and last parts of the string:
#echo off
setlocal
set "testString=1.2.5.234"
set "first="
for %%a in ("%testString:.=" "%") do (
if not defined first set "first=%%~a"
set "last=%%~a"
)
echo First: %first%
echo Last: %last%
As a bonus, this method correctly process special Batch characters that may appear in the string, excepting wild-cards.
You can use the below command to achieve what you want.
base=%fullver:~~4,3%
4 implies 4th digit i.e., 5 and 3 implies 3 digits from 4.
The output will be
567

Resources