CMD: extract variable string from .txt file - batch-file

I have myfile.txt containing this:
line 1: keyword=string
line 2
line 3
I need to echo that variable string via constant keyword.
And this string I need is always in the end of the 1st line.
So far I have:
for /f "usebackq tokens=*" %%a in ("myfile.txt") do (
for /f "usebackq tokens=*" %%b in ('findstr /i "keyword=" %%a') do (
echo %%b & goto 108
)
)
:108
But I guess that findstr syntax is completely wrong.
Please, help.

You can do rhis very simply as:
Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
For /F "tokens=*" %%_ IN ('Type "c:\my\path\myfile.txt" ^| FIND /I "keyword="
) DO (
SET "Tmp_Line=%%_"
For /F "Tokens=* delims==" %%A in ('ECHO=!Tmp_Line:*keyword=!') DO (
echo %%a
GOTO :108
Pause
)
GOTO :EOF
)
Goto :EOF

Related

Adding a increasing value

I know this one is an easy one, but it's driving me nuts
Need help with setting up a counter to increase by 1
I have a txt file with a 100x timestamp and when I enter 1000 all I get is 1001
and I'm trying to get 1001,1002,1003
Here is the script I am using
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
SET /a TimeCount+=1
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %TimeCount%,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
Thanks guys
Thank you Magoo for pointing out !Timecount!
Here is the correct way to set up the script to increase by 1
This script will add to your number if you enter 100 and you have 10 repeated codes within the same file you will get 101,102,103,...110, etc...
Now the adding will carry over to all other files so..
If you have 2 files in the same folder and each have 10 repeated codes each the script will count from 101 to 120
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)
Now by moving this SET /a TimeCount=%Stamp% under the For Loop
you will 2 results each file will get 101 to 110
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
SET /a TimeCount=%Stamp%
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)

batch script adaption with following code

I have a Batch file with the following code:
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO for /L %%z in (1 1 %%i) do echo %%a;%%b;%%c;%%d;%%e;%%f;%%g;%%h;%%i;)
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Output:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Now I would like to have the following Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
because in %%i has the value 3.
If in %%i the value is 4 the output should be:
TEST0;TEST1;WERT1;;;;;;4;
TEST0;TEST1;WERT2;;;;;;4;
TEST0;TEST1;WERT3;;;;;;4;
TEST0;TEST1;WERT4;;;;;;4;
etc.
#echo off
SETLOCAL EnableDelayedExpansion
set "quelle=t.csv"
set "ziel=t1.csv"
>"%ziel%" (
for /f "usebackq tokens=1,2,* delims=;" %%a in ("%quelle%") DO (
set "i=0"
for %%k in (%%c) do set "nr=%%k"
for %%m in (%%c) do (
set /a i+=1
if !i! leq !nr! echo %%a;%%b;%%m;;;;;;!nr!;
)
)
)
type "%ziel%"
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERTa;WERTb;WERTc;WERTd;WERTe;WERTf;4;
Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
TEST0;TEST1;WERTa;;;;;;4;
TEST0;TEST1;WERTb;;;;;;4;
TEST0;TEST1;WERTc;;;;;;4;
TEST0;TEST1;WERTd;;;;;;4;
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO (
echo %%a;%%b;%%c;;;;;;%%i;
echo %%a;%%b;%%d;;;;;;%%i;
echo %%a;%%b;%%e;;;;;;%%i;
echo %%a;%%b;%%f;;;;;;%%i;
echo %%a;%%b;%%g;;;;;;%%i;
echo %%a;%%b;%%h;;;;;;%%i;
)
)
I've removed the For /L loop as it's purpose isn't clear, and the %%z value does not get used. The only thing it appears to do is cause the same line to be echoed multiple times depending on the value of the %%i token retrieved in the first loop.
This seems as if it will do what your samples show, is it what you mean?
#Set "quelle=C:\Users\User-01\quelle_001.txt"
#Set "ziel=C:\Users\User-01\ziel_001.csv"
#(For /F "UseBackQ Tokens=1-3,9 Delims=;" %%G In ("%quelle%") Do #For /F "Delims=0123456789" %%K In ("%%I") Do #For /L %%L In (1 1 %%J) Do #Echo %%G;%%H;%%K%%L;;;;;;%%J;)>"%ziel%"

batch file: Parse string after substring occurence, not delimeter

I have and batch file script shows me output of adb utility.
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x
2 output lines
0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096 device product:WW_P023 model:P023 device:P023_1 transport_id:11
I want parse non-space substring after "transport_id:".
For the first line i want get 12, for second 11. How can I do that?
If I understand correctly what you've asked, the following batch-file should output the information you require.
#Echo Off
For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
SetLocal EnableDelayedExpansion
For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)
Pause
And, if you wanted to do that from cmd:
For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do #Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do #Echo %~I
You may easily extract all variables no matter their positions and show what you want:
#echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 delims=" %%a IN ('adb devices -l') DO (
for %%b in (%%a) do for /F "tokens=1,2 delims=:" %%x in ("%%b") do set "%%x=%%y"
echo Product=!product!
echo Transport=!transport_id!
)
For example:
Product=java_joyplus_qb7
Transport=12
Product=WW_P023
Transport=11
SETLOCAL EnableDelayedExpansion
REM …
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO (
set "_ID="&set "_transport_id="
call :parse "" "%%~x"
echo transport_id !_transport_id! (!_ID!^)
)
REM … remaining part of your script here …
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Tested using the following script with hard-coded hypothetical adb output (not having adb installed):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
(
REM last token
set "_ID="&set "_transport_id="
call :parse "" "0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12"
echo transport_id !_transport_id! (!_ID!^)
REM penult token
set "_ID="&set "_transport_id="
call :parse "" "F9NPFP084096 device product:WW_P023 model:P023 transport_id:11 device:P023_1"
echo transport_id !_transport_id! (!_ID!^)
REM elsewhere token
set "_ID="&set "_transport_id="
call :parse "" "abc123def567 transport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
REM nowhere token
set "_ID="&set "_transport_id="
call :parse "" "noTransportId sport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
)
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Output: D:\bat\SO\61588773.bat
transport_id 12 (0123456789ABCDEF)
transport_id 11 (F9NPFP084096)
transport_id 10 (abc123def567)
transport_id (noTransportId)

Windows Batch SET Syntax

I have adopted some code from here:
Extract substring between 2 strings
Rem Looking for a test.txt substring preceded by page\[%%x\]= and succeeded by ;
#Echo Off
for /l %%x in (0, 1, 4) do (
echo %%x
For /F "Delims=" %%A In ('FindStr /I "page\[%%x\]=.*;" "test.txt"') Do Call :Sub "%%A" "%%x"
)
Pause
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Up2Sub=%Line:*page[ %~2 ]=%"
Set "SubStr=%Up2Sub:;="&:"%"
Echo %SubStr% > test%~2.json
Exit /B
The issue I am having is regarding this line:
Set "Up2Sub=%Line:*page[ %~2 ]=%"
The variable %~2 does not get concatenated correctly.
What would be the syntax to get this value of the %~2 variable added to this SET statement in between the []?
Thanks
Sample text.txt
....
page[0]={*****};
page[1]={*****};
page[2]={*****};
page[3]={*****};
page[4]={*****};
....
To split a string, you can also use a for /f loop:
#Echo Off
for /l %%x in (0, 1, 4) do (
For /F "Delims=" %%A In ('FindStr /I "page\[%%x\]=.*;" "test.txt"') Do (
for /f "tokens=2 delims=;=" %%s in ("%%A") do echo %%x: %%s
)
)
Pause
This splits the string (%%A) into tokens, separated by ; and =. With page[0]={*****};, the first token is page[0],then =is a delimiter. The second token is {*****} and the third token (after the;) is empty. we just include the ';' to the delimters to get rid of it.
With a little analyzing your file and choosing tokens and delims right, you can even reduce the code to a single for:
For /F "tokens=2,4 delims=[]{}" %%A In ('FindStr /I "page\[[0-4]\]=.*;" "test.txt"') Do echo {%%B}>test%%A.json

Append in the line if a particular character is present

03a01Fa.wav
03a01Nc.wav
03a01Wa.wav
I have this text file
by using batch file i want to appent "anger" if the letter in caps is W, if the letter is F then "happy", N for "neutral". is it possible to do ?
i want it like this
03a01Fa.wav,happy
03a01Nc.wav,neutral
03a01Wa.wav,anger
Thanks in advance, looking forward to a solution.
#echo off
setlocal EnableDelayedExpansion
rem Define the equivalences array
for %%a in ("W=anger" "F=happy" "N=neutral") do (
for /F "tokens=1,2 delims==" %%b in (%%a) do (
set "append[%%b]=%%c"
)
)
for /F %%a in (theFile.txt) do (
set name=%%a
for /F %%b in ("!name:~5,1!") do echo %%a,!append[%%b]!
)
You may review the array management in Batch files at this post.
Another way using FIND :
#echo off
for /f "delims=" %%a in (file.txt) do (
echo %%a | find "F">nul && echo %%a,happy
echo %%a | find "N">nul && echo %%a,neutral
echo %%a | find "W">nul && echo %%a,anger)
If you need the output in a file (output.txt) :
#echo off
for /f "delims=" %%a in (file.txt) do (
echo %%a | find "F">nul && echo %%a,happy
echo %%a | find "N">nul && echo %%a,neutral
echo %%a | find "W">nul && echo %%a,anger)>output.txt
#echo off
setlocal enabledelayedexpansion
(for /f %%i in (t.t) do (
set a=%%i
set a=!a:~5,1!
if !a!==F echo %%i,happy
if !a!==N echo %%i,neutral
if !a!==W echo %%i,angry
))>o.t
#echo off
setlocal enableextensions disabledelayedexpansion
(for /f "usebackq" %%a in ( "wavfile.txt" ) do (
for /f "delims=0123456789abcdefghijklmnopqrstuvwxyz" %%b in ("%%a") do (
if %%b==W (
echo %%a,anger
) else if %%b==F (
echo %%a,happy
) else if %%b==N (
echo %%a,neutral
)
)
)) > wavFileOut.txt

Resources