Batch file Cout math examples from txt file - batch-file

Hello I have txt file with math examples:
12;+;56
893;+;354
756;-;231
987;-;884
14;*;15
45;*;33
1024;/;10
120;/;30
12345;+;5667
15747;-2344
And I have batch file for counting. I want to display examples + resul in batch file
I have this code, but it return result 0.
#echo off
cls
echo Examples:
echo.
set result=0
for /f "eol=# delims=; tokens=1,2,3" %%A in (examples.txt) do (
set result=%%A%%B%%C
echo %%A%%B%%C = %result%
)
pause

You need to use set /a to do arithmetic calculations. You'll also need delayedepxansion and let's use usebackq in order to use double quotes if the input file is in a path containing spaces.
#echo off & set result=0
setlocal enabledelayedexpansion & cls
echo Examples:
echo(
for /f "usebackq tokens=1-3*delims=;" %%A in ("examples.txt") do (
set /a result=%%A%%B%%C
echo %%A%%B%%C = !result!
)
pause
Check you examples.txt file as well as you forgot a semicolon on the last example.

Related

Batch file to rewrite txt file starting from last line

lets say that i have test.txt with this lines
1
2
3
i need batch file to rewrite this file as
3
2
1
in new txt file
i tried this to copy last line
for /f "delims=" %%A in (ttt.txt) do set last=%%A
echo %last%>>ttt_lastline.txt
and this to delete last line
#echo off & setlocal EnableDelayedExpansion
set row=
for /F "delims=" %%j in (File.txt) do (
if defined row echo.!row!>> File.new
set row=%%j
)
but wasn't helpful
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "destdir=U:\destdir"
SET "filename1=100lines.txt"
SET "outfile=%destdir%\outfile.txt"
SET /a count=0
FOR /f "delims=" %%a IN (%filename1%) DO (
SET /a count+=1
SET "line[!count!]=%%a"
)
(
FOR /L %%a IN (%count%,-1,1) DO ECHO(!line[%%a]!
)>"%outfile%"
GOTO :EOF
You would need to change the setting of destdir to suit your circumstances.
I used a file named 100lines.txt containing some dummy data for my testing.
Produces the file defined as %outfile%
Using delayedexpansion, read each line into line[?] using !count!, then simply echo each line loaded in reverse order.
This is a Vbscript solution.
Create a file named reverse.vbs and use this as the code.
Dim Stack: Set Stack = CreateObject("System.Collections.Stack")
Do While Not WScript.StdIn.AtEndofStream
Stack.Push WScript.StdIn.ReadLine
Loop
WScript.StdOut.WriteLine Join(Stack.ToArray, vbCrLf)
Now to reverse your file execute this from a cmd prompt or a batch file.
type input.txt |cscript //nologo Reverse.vbs>>output.txt
#echo off
setlocal enabledelayedexpansion
for /f %%a in (forward.txt) do
(
set reversed=%%a !reversed!
)
for %%i in (!reversed!) do echo.%%i >> reversed.txt
goto :eof

read a file line by line and save it in variable batch file

I have the following .txt file:
Marco
Paolo
Antonio
I want to read it line-by-line, and for each line I want to assign a .txt line value to a variable. Supposing my variable is name, the flow is:
Read first line from file
Assign name = "Marco"
Do some tasks with name, let sat set first= %name%
Read second line from file
Assign name = "Paolo"
How do I do it?
This will read a file into an array and assign each line into a variable and display them
#echo off
set "File2Read=file.txt"
If Not Exist "%File2Read%" (Goto :Error)
rem This will read a file into an array of variables and populate it
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set /a count+=1
set "Line[!count!]=%%a"
)
rem Display array elements
For /L %%i in (1,1,%Count%) do (
echo "Var%%i" is assigned to ==^> "!Line[%%i]!"
)
pause>nul
Exit
::***************************************************
:Error
cls & Color 4C
echo(
echo The file "%File2Read%" dos not exist !
Pause>nul
exit /b
::***************************************************
I think this answers your question.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "somefile=somefile.txt"
if not exist %somefile% (
echo File '%somefile%' does not exist^^!
goto :eof
)
for /f "delims=" %%i in (%somefile%) do (
set "name=%%i"
set "first=!name!"
echo !first!
)

How to replace Strings in Windows Batch file

I would like to replace the following String in a file:
android:versionName="anyStringHere" >
*anyStringHere represents any possible string
With:
android:versionName="1.04.008" >
How would I do this in a clean, reusable way, and preserve the new lines, tabs, and indentation in the file?
Not even close to the fastest option, and not 100% bulletproof, but this is pure batch and will handle spacing and indentation while do the replacement.
#echo off
setlocal enableextensions disabledelayedexpansion
rem File to process
set "file=data.txt"
rem How to find lines
set "match=public static String CONST = \"abc\";"
rem String to replace and replacement
set "findStr=abc"
set "replaceStr=def"
rem temporary file to work with lines
set "tempFile=%temp%\repl.tmp"
rem All the output goes into the temporary file
(
rem Process input file extracting non matching lines
for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('findstr /n /v /c:"%match%" ^< "%file%"') do (
set /a "n=1000000+%%a"
setlocal enabledelayedexpansion
< nul set /p "n=!n!"
endlocal
echo :%%b
)
rem Process input file extrancting matching lines and changing strings
for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('findstr /n /c:"%match%" ^< "%file%"') do (
set /a "n=1000000+%%a"
set "data=%%b"
setlocal enabledelayedexpansion
set "data=!data:%findStr%=%replaceStr%!"
echo !n!:!data!
endlocal
)
)> "%tempFile%"
rem Sort the output file to get the final file
(for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('sort "%tempFile%"') do (
if "%%b"=="" (
echo.
) else (
echo %%b
)
)) > "%file%.repl"
This is the simplest way to do this that I could come up with. It takes a String and searches for it in a file, then replaces the entire line that contains the string. It won't only replace parts of a line, which can be done with a bit more effort.
#echo off
:: file containing string to replace
set file=test.txt
:: string to replace in file
set searchString=line 4
:: string to write to file
set repString=line 4 edited
setLocal enableDelayedExpansion
set count=0
if not exist %file% echo cannot find file - %file% & goto :EOF
:: Search for string - and get it's line number
for /F "delims=:" %%a in ('findstr /N /I /C:"%searchString%" "%file%"') do set searchLine=%%a
if not defined searchLine echo cannot find string - %searchString% - in file - %file% & goto :EOF
:: Read file into variables - by line number
for /F "delims=~!" %%b in ('type %file%') do (
set /a count=!count!+1
set line!count!=%%b
)
:: Edit the one line
set line%searchLine%=%repString%
:: Empty file and write new contents
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%
pause
You can change the echo on the last for loop to output to a different file, maybe %file%.new or something, and then remove the del command.
This is a robust solution that retains all formatting. It uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
type "file.txt" | repl "(public static String CONST = \q).*(\q.*)" "$1def$2" x >"newfile.txt"
I found that using sed was the cleanest solution
http://gnuwin32.sourceforge.net/packages/sed.htm
sed "s/android:versionName=\".*\" >/android:versionName=\"%NEW_VERSION%\" >/g" %ORIG_FILE_NAME% > %TEMP_FILE_NAME%
#move /Y %TEMP_FILE_NAME% %ORIG_FILE_NAME% >nul

Batch Scripting iterating over files in a folder and extract for each one just the first line

I need to iterate over files contained in a folder and extract the first line for each file. I've tried to get this writing two batch file - the first one overate over files:
FOR %%a in (D:\TEST_BAT\*.TXT) do (
call Estrai_Header.bat %%a %header%
#echo on
echo %header%
)
The second one (named Estrai_header.bat) extract the first line for the file (just passing it as parameter):
set header = ""
SET /A maxlines=1
SET /A linecount=0
FOR /F %%b IN (%1) DO (
IF !linecount! GEQ %maxlines% GOTO ExitLoop
set $2 = %2%%b
echo %2%
SET /A linecount+=1
echo %linecount%
)
:ExitLoop
exit /b
Estrai_Header.bat works correctly and prints for every file just the first row. But I cannot see the value of the first line extracted in the first batch (it prints a void string). What's wrong in these batch files?
Thanks in advance.
try this:
#echo off&setlocal
FOR %%a in (D:\TEST_BAT\*.TXT) do (
set "line="
for /f "usebackqdelims=" %%i in ("%%a") do if not defined line set "line=%%i"
setlocal enabledelayedexpansion
echo(!line!
endlocal
)

Batch command to take only first line from input

I'm looking for a DOS batch program that takes a file:
First input line
Second input line
Third input line...
And outputs "First input line"
you can just get the first line like this
set /p firstline=<file
echo %firstline%
Assuming you mean the Windows cmd interpreter (I'd be surprised if you really were still using DOS), the following script will do what you want:
#echo off
setlocal enableextensions enabledelayedexpansion
set first=1
for /f "delims=" %%i in (infile.txt) do (
if !first!==1 echo %%i
set first=0
)
endlocal
With an input file of infile.txt as:
line 1
line 2
line 3
this will output:
line 1
This will still process all the lines, it just won't print those beyond line 1. If you want to actually stop processing, use something like:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in (infile.txt) do (
echo %%i
goto :endfor
)
:endfor
endlocal
Or you could just go get your hands on Cygwin or GnuWin32 and use the head program. That's what I'd do. But, if that's not an option (some workplaces don't allow it), you can create a similar cmd file in Windows itself as follows (winhead.cmd):
#echo off
setlocal enableextensions enabledelayedexpansion
if x%1x==xx goto :usage
if x%2x==xx goto :usage
set /a "linenum = 0"
for /f "usebackq delims=" %%i in (%1) do (
if !linenum! geq %2 goto :break1
echo %%i
set /a "linenum = linenum + 1"
)
:break1
endlocal
goto :finish
:usage
echo.winhead ^<file^> ^<numlines^>
echo. ^<file^>
echo. is the file to process
echo. (surround with double quotes if it contains spaces).
echo. ^<numlines^>
echo. is the number of lines to print from file start.
goto :finish
:finish
endlocal
why not use the more +1 command via a pipe?
e.g.
type something | more +1

Resources