Could batch file recognize the fields in the cfg file? - batch-file

I have cfg file which has two fields : test.cfg
[Test_1] = 12345
TC_1=testCase/String_print.tc -arg "Indonesia" -logToConsole
TC_2=testCase/test2.tc -c "ramen" -logToConsole
[Test_2] = 12346
TC_1= testCase/test.tc -c "olleH" -logToConsole
And bat file test.bat
#ECHO off
set var=C:\Users\syuniyar\.EasyTest\4\ccl\config\test2.cfg
SETLOCAL enabledelayedexpansion
FOR /F "tokens=*" %%a IN (%var%) DO (
SET line=%%a
IF "!line:~0,7!"=="Test_1=" SET Test_1=!line:~7! (
FOR /F "tokens=*" %%a IN (%var%) DO (
IF "!line:~0,5!"=="TC_1=" SET TC_1=!line:~5!
IF "!line:~0,5!"=="TC_2=" SET TC_2=!line:~5!
)
)
)
SET TC_1=%TC_1:;=%
SET TC_2=%TC_2:;=%
ECHO %TC_1%
ECHO %TC_2%
I want to retrieve the TC_1 in Test_1 = 12345 which has the value 'testCase/String_print.tc -arg "Indonesia" -logToConsole'. But from the current code what I got is 'testCase/test.tc -c "olleH" -logToConsole' which is from 12346. It returns the last item matched. My questions are : Is it possible that batch file recognizes something like Test_1\TC_1? Before I tried using array but it stored all the element from cfg file into one single array.
Could anyone help?

This Batch file do what you requested:
#echo off
setlocal EnableDelayedExpansion
rem Get the line number of "[Test_1]"
for /F "delims=:" %%a in ('findstr /N /L /B "[Test_1]" test2.cfg') do set lines=%%a
rem Get the TC_1 line after previous one
for /F "skip=%lines% tokens=1,2 delims==" %%a in (test2.cfg) do (
if "%%a" equ "TC_1" set "%%a=%%b" & goto continue
)
:continue
echo TC_1=%TC_1%

Related

Batch remove part of string after “-1” is found or any other number “-[0-9]”

I got a file containing a string on each line like this:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5-16515
extra-huh-9.5-511-515
extra-3.2
I am iterating over it and want it to remove the part of the string on the right after in find "-" + any number "-0","-1","-2","-9",...
so output should be
fruit-apple
vegtable-sla-mc5
extra-huh
extra
this is code i have but it only works with a "-" i cant combine it so it takes "-" + any number like "-1","-5","-2",...
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
echo stripped string = %%A
)
complete code not necessary i think but in case u need it below
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "RawPath=%~dp0"
FOR /F "USEBACKQ TOKENS=*" %%M IN ("%RawPath%/mods") DO (
REM for %%f in (*.jar) do (
Set "fileNameCheck=%%M"
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
Echo [46m%%A[0m
if exist "%~dp0%%A*.jar" (
REM echo [32mFound %%A "%~dp0%%A*.jar"[0m
if exist "%~dp0%%M" (
REM echo [42mUp to Date[0m [32m%%A "%~dp0%%M"[0m
) else (
for %%j in (*.jar) do (
echo %%j |find "%%A" >nul
if not errorlevel 1 (
echo [41mDifferent Version[0m [31m%%j [0m[90mNewer version[0m [32m%%M[0m
)
)
)
) else (
REM echo [31mMissing %%A[0m
)
)
)
pause
Given that the strings do not contain any of the caracters ?, *, < and >, the following script should do the trick:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0."
set "_FILE=mods.txt"
rem // Read file line by line:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Store current line, initialise variables:
set "LINE=%%L" & set "PREV=-" & set "COLL=-"
setlocal EnableDelayedExpansion
rem /* Split line string at every `-` and iterate through items;
rem (`?`, `*`, `<`, `>` must not occur in the string!): */
for %%I in ("!LINE:-=" "!") do (
endlocal & set "ITEM=%%~I"
rem // Check whether item only consists of numerals and `.`:
(
rem // This loop executes when there are other characters:
for /F "delims=0123456789. eol=." %%J in ("%%~I") do (
setlocal EnableDelayedExpansion
rem /* Rebuilt string with currently found items; if there
rem are other items following numeric ones, keep them: */
for /F "delims=" %%K in ("!COLL!!PREV:~1!-!ITEM!") do (
endlocal & set "COLL=%%K" & set "PREV=-"
)
)
) || (
rem // This section runs when there are numerals and `.`:
setlocal EnableDelayedExpansion
for /F "delims=" %%K in ("!PREV!-!ITEM!") do (
rem /* Store numeric items in a separate buffer; if there
rem are no other items following, this is dismissed: */
endlocal & set "PREV=%%K"
)
)
setlocal EnableDelayedExpansion
)
rem // Return rebuilt line string:
echo(!COLL:~2!
endlocal
)
endlocal
exit /B
The example input file mods.txt:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5
extra-huh-9.5
extra-3.2
test-9.15.5
keep-forge
name-subname-10.5-55.5
globalxp-1.16.5
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more-3.4
mix-1.2-string-10.5-55.5-more-3.4-5.6-7
8.9
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
leads to the following output:
fruit-apple
vegtable-sla-mc5
extra-huh
extra
test
keep-forge
name-subname
globalxp
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
One way is to replace the - with \ and then use variable substitution to strip the %%~ni part from the string. We just need to remove the first and last occurrence then from the string. findstr will determine that the end of string needs to be -num.*
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s *.jar ^| findstr /RC:"-[0-9].*$"') do (
set "line=%%~nxA"
for %%i in ("!line:-=\!") do set "final=%%~pi"
set "final=!final:~0,-1!-"
echo !final! | findstr /RC:"\\[0-9].*$" >nul 2>&1
if !errorlevel! equ 0 (
for %%s in ("!final!") do set "final=%%~ps"
set final=!final!
)
set "final=!final:~0,-1!"
set "final=!final:~1!"
echo !final:\=-!
)
the edited code should take care of the additional - as per your comment example name-subname-10.5-55.5
Here's an untested idea, which may not be particularly quick, especially if you have a large number of strings in your mods file:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('Echo Prompt $E ^| %SystemRoot%\System32\cmd.exe /D'
) Do Set "$E=%%G"
For /F Delims^=^ EOL^=^ UseBackQ %%G In ("%~dp0mods") Do (Set "Line=%%G"
SetLocal EnableDelayedExpansion
For /L %%H In (0 1 9) Do For /F Delims^=^ EOL^= %%I In (
'(Echo(^) ^| Set /P "=!Line:-%%H="^&:"!" 0^<NUL') Do Set "Line=%%I"
Echo(%$E%[46m!Line!%$E%[0m
EndLocal
)
Pause

Removing carriage returns from a text file using a batch file based on a value

I have a text file that I would like to edit and therefore would like to remove the last line. I have the following code for this:
for /f "delims=" %%a in (input.txt) do (
echo/|set /p ="%%a%"
)>>output.txt
input:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Now I would like to edit the data in groups for example by the first value, so that I have the following output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
If I replace the FOR /F "delims=" %%a in (input.txt) do … loop with an equivalent FOR %%a in … loop:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_gruppeName="
(
for %%a in (
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
) do (
for /f "tokens=1 delims=;" %%A in ("%%~a") do (
if /I NOT "%%~A"=="!_gruppeName!" (
if defined _gruppeName echo(
set "_gruppeName=%%~A"
)
)
echo/|set /p ="%%~a"
)
echo(
)>>output.txt
REM debugging output follows
type output.txt
Output:
1st run: 2>NUL del output.txt & D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Next run: D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Your question is not clear
based on ... the first value (GRUPPEA)
is it SORTed? or just write duplicates on the same line?
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
::VARIABLES
set "in=input.txt" IN file
set "out=output.txt" OUT file
set/a"#lines=0"
set "gruppe="
set "prevContent="
::Count lines
FOR /F %%L in ('
"findstr /N "^^" "%in%" %= do not skip empty lines =%"
') do set/a"#lines+=1" %= Get the # of lines =%
::Read IN via SET /P #LINES number of times
::Bangs (!) will be lost
<"%in%" >"%out%" (FOR /L %%# in (1 1 %#lines%) do (
set "data=" ::clear DATA
set/p"data=" ::read from IN
FOR /F tokens^=1^ delims^=^;^ eol^= %%T in ("!data!") do set "gruppe=%%T"
if NOT "!prevContent!" == "!gruppe!" (
set "prevContent=!gruppe!"
echo(
)
<nul set/p"=!data!" ::does not work with leading space, tabs, or equal signs
)) %= read file by lines via SET /P =%
exit /b
The script counts the number of lines using FINDSTR /N and a FOR /F loop to count the # of lines, which is required to execute SET /P that many times.
Tips:
Use ECHO( instead of the problematic ECHO.
Using a pipe | is very slow, as described by #jeb here. Use <nul instead

Batch Split each line into multiples lines

My txt file looks like:
A1;A2;A3
B1;B2
I want to split it like:
A1;;;A2
A2;;;A3
A3;;;A3
B1;;;B2
B2;;;B2
The rule is: For each line take two adjacent elements and create a new output line with them, for the last element of a line: create also a new output line, but use the element twice.
Here is my try:
(
for /f "tokens=1-4 delims=;" %%a in (%FilePath%) do (
for /f "tokens=1-4 delims=;" %%x in (%FilePath%) do (
echo %%a;;;%%y
)
)
)>%FilePath_Final%
But it gives the wrong format:
A1;;;A2
A1;;;B2
B1;;;A2
B1;;;B2
How can I use batch commands to split the lines so that the expected result is obtained?
PS: A1, A2, B1 etc. are just some string examples, I can have various strings
Here is an example of a file content:
XB8998901;XB8998900;8051191;24048271;24048270
XB0134812;XB0134810;XB0134801;XB0134800
XB6312701;XB6312700
XB6314201;XB6314200
The ouput should look like:
XB8998901;;;XB8998900
XB8998900;;;8051191
8051191;;;24048271
24048271;;;24048270
24048270;;;24048270
XB0134812;;;XB0134810
XB0134810;;;XB0134801
XB0134801;;;XB0134800
XB0134800;;;XB0134800
XB6312701;;;XB6312700
XB6312700;;;XB6312700
XB6314201;;;XB6314200
XB6314200;;;XB6314200
#echo off
setlocal enabledelayedexpansion
set "last="
(for /f "delims=" %%a in (old.txt) do (
for %%b in (%%a) do (
if defined last echo !last!;;;%%b
set "last=%%b"
)
echo !last!;;;!last!
set "last="
))>new.txt
fc new.txt compare.txt
old.txt (your example file):
XB8998901;XB8998900;8051191;24048271;24048270
XB0134812;XB0134810;XB0134801;XB0134800
XB6312701;XB6312700
XB6314201;XB6314200
compare.txt (desired output from your example):
XB8998901;;;XB8998900
XB8998900;;;8051191
8051191;;;24048271
24048271;;;24048270
24048270;;;24048270
XB0134812;;;XB0134810
XB0134810;;;XB0134801
XB0134801;;;XB0134800
XB0134800;;;XB0134800
XB6312701;;;XB6312700
XB6312700;;;XB6312700
XB6314201;;;XB6314200
XB6314200;;;XB6314200
Output:
Vergleichen der Dateien new.txt und compare.txt
FC: Keine Unterschiede gefunden
(Translation: Comparing files new.txt and compare.txt; FC: no differences encountered)
Use this:
#echo off
Setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=;" %%a in ('type pathoffile ^| find "A"') do (set 1=%%a & set 2=%%b & set 3=%%c)
for /f "tokens=1,2 delims=;" %%a in ('type pathoffile ^| find "B"') do (set 4=%%a & set 5=%%b)
set n=1
:loop
set /a m=n+1
if "!%n%:~0,1!"=="!%m%:~0,1!" (
echo !%n%!;;;!%m%! >>filepath_final
) else (
echo!%n%!;;; >>filepath_final
)
set /a n=n+1
if %m% equ 5 goto :eof
goto loop

findstr multiple search in one single output line

I have multiple text files that contains 3 lines of information that I want to output as one single line for each file
Example
File1.txt contains
User: "John"
Date: "13-March-2017"
Time: "10.30am"
Remarks: "xcvsfas"
File2.txt contains
User: "Mary"
Date: "13-March-2017"
Time: "11.30am"
Remarks: "xerteyas"
My expected output is as follows
c:\temp\file1.txt:User: "John"; Date: "13-March-2017"; Time: "10.30am"
c:\temp\file2.txt:User: "Mary"; Date: "13-March-2017"; Time: "11.30am"
I tried
findstr /s /i "user date time:" %inputfolder%\*.* > %outputfolder%\final.txt
EDIT: Code modified per new specifications posted in a comment in other answer... :(
#echo off
setlocal EnableDelayedExpansion
set "file="
(
for /F "tokens=1* delims=:" %%a in ('findstr /S /I "user date time" %inputfolder%\*.*') do (
if "!file!" neq "%%a" (
if defined file echo !file!:!out!
set "file=%%a"
set "out=%%b"
) else (
set "out=!out!; %%b"
)
)
echo !file!:!out!
) > %outputfolder%\final.txt
I'm assuming all your .txt are in the same folder and only them. Then I get the first three lines of each file and print them in one line by using the set command like:
#echo off
setlocal EnableDelayedExpansion
pushd <files dir>
for /f %%i in ('dir /b') do (
set "c=0"
for /f "tokens=*" %%j in ('type "%%i"') do (
set /a "c=c+1"
if "!c!" equ "3" (
set /p "=%%j" <nul
) else if "!c!" lss "3" (
set /p "=%%j; "<nul
)
)
echo(
)
popd
I tested with the input you gave and the result is:
User: "John"; Date: "13-March-2017"; Time: "10.30am";
User: "Mary"; Date: "13-March-2017"; Time: "11.30am";
Hope it helps.
You could loop through the files by a for loop and do the search individually -- like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_LOCATION=."
set "_INPUTFILES=File*.txt"
set "_OUTPUTFILE=%~dpn0.log"
rem // Write everything into output file:
> "%_OUTPUTFILE%" (
rem // Iterate over matching files recursively:
for /R "%_LOCATION%" %%F in ("%_INPUTFILES%") do (
rem // Initialise line string variable:
set "LINE=%%~F:"
rem // Search currently iterated input file for the keywords:
for /F "delims=" %%L in ('findstr /L /I /B "User: Date: Time:" "%%~F"') do (
rem // Store found item:
set "ITEM=%%L"
rem // Toggle delayed expansion in order not to lose exclamation marks:
setlocal EnableDelayedExpansion
rem // Build line string and transfer result over `endlocal` barrier:
for /F "delims=" %%E in ("!LINE!!ITEM!; ") do (
endlocal
set "LINE=%%E"
)
)
rem // Return built line string:
setlocal EnableDelayedExpansion
echo !LINE:~,-2!
endlocal
)
)
endlocal
exit /B
The fields User:, Date: and Time: are returned in the original order they appear in every file.

Batch script Moving %%var to string variable

I am trying to read the file print.txt which is having file paths
example: C:\Documents and Settings\Administrator\My Documents\My Pictures\a1.jpg
It getw read successfully, but when I am trying to move it to filename var its not getting any value. The echo prints as 'filename is: filename
FOR /f "tokens=*" %%s in (print.txt) do (
Set filename=" " %%s " "
echo.filename is: %filename
)
Try this:-
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=* delims=" %%s in (print.lst) do (
echo %%s
Set filename=%%s
echo filename is: "!filename!"
)
Update 2
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=* delims=" %%s in (print.lst) do (
echo %%s
Set filename=%%s
echo filename is: "!filename!"
For %%A in (!filename!) do (
Set Folder=%%~dpA
Set Name=%%~nxA
echo.Folder is: !Folder!
)
)

Resources