Solved How do i make a batch file calculate only specific numbers?

November 7, 2015 at 04:23:04
Specs: Windows 7
Hi! I have no experience with programming in batch so I'm hoping some of you could help me out! I'm curious if there is possible to make a batch file that does the follow calculation in all txt file with the same location as the batch file:

Find all numbers starting with $ and divide by 500.

Example:
Before calculated numbers.txt:
$500
$1000
£500
After calculated numbers.txt:
$1
$2
£500


See More: How do i make a batch file calculate only specific numbers?

Report •


#1
November 7, 2015 at 07:44:14
✔ Best Answer
Listing line by line using FOR, then using FIND to check if the line contains "$". If true, perform math then write to calculated.txt, if false write straight to calculated.txt

@Echo off
setlocal enabledelayedexpansion
:: Just to make sure we're in the same directory as the script.
cd "%~dp0"
for /f "tokens=*" %%A in (numbers.txt) do (
	for /f "tokens=*" %%B in ('echo %%A ^| find "$"') do (
		set num=%%B
		set num=!num:~1,-1!
                echo !num!
		set /a result=!num!/500
		echo !result!>>calculated.txt
	)
	for /f "tokens=*" %%B in ('echo %%A ^|find /v "$"') do (
		echo %%B>>calculated.txt
	)
)
pause > nul

Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ


Report •

#2
November 7, 2015 at 08:48:07
Thanks alot for looking into it! Apologize for not being precise enough but is there possible to add two more features to this script? Always adding 2 decimals and not deleting whole line but instead just replace the number?

Example;
Before calculated numbers.txt:
Balance: $500
Loan: $1000
Debt: £500
Debt1:$1100

After calculated numbers.txt:
Balance: $1.00
Loan: $2.00
Debt: £500
Debt1:$2.20

message edited by Chillit


Report •

#3
November 7, 2015 at 09:29:34
Batch is probably not the best language when it comes to math, as division of numbers often result in floating point values (numbers with decimals). There are workarounds of course.

By "Just replace the number", do you mean modify the numbers.txt file, or do you mean keeping the "loan" and "debt" text?


This adds two decimals and keeps the text before the number:

@Echo off
setlocal enabledelayedexpansion
:: Just to make sure we're in the same directory as the script.
cd "%~dp0"
echo WScript.Echo Eval^(WScript.Arguments^(0^)^)>eval.vbs
for /f "tokens=*" %%A in (numbers.txt) do (
	for /f "tokens=*" %%B in ('echo %%A ^| find "$"') do (
		set line=%%B
		for /f "tokens=1,2 delims=$" %%Y in ("!line!") do (
			set num=%%Z
			set pfx=%%Y
		)
		echo !num!
		for /f %%N in ('cscript //nologo eval.vbs "!num!/500"') do (
			set result=%%N
		)
		set result=!result:,=.!
		for /f "tokens=1,2 delims=." %%C in ("!result!") do (
			if not "%%D"=="" (
				set rmndr=%%D
				set rmndr=!rmndr:~0,2!
				set result=%%C.!rmndr!
			) else (
				set result=%%C.00
			)
		)
		echo !pfx!$!result!>>calculated.txt
	)
	for /f "tokens=*" %%B in ('echo %%A ^|find /v "$"') do (
		echo %%B>>calculated.txt
	)
)
pause > nul

Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ


Report •

Related Solutions

#4
November 7, 2015 at 10:05:32
I mean keeping the "loan" and "debt" text. Thanks alot for the script, It's works like a charm! I hope you dont mind nagging on a saturday night! The results is nearly perfect:

Balance: $1.00
Loan: $2.00
Debt: £500
Debt1:$2.2

There has to be two decimals no matter what so Debt1 would have to result in $2.20. Would this also be possible to implement? Thanks for the great (and fast) support!


Report •

#5
November 7, 2015 at 10:16:43
Sure

@Echo off
setlocal enabledelayedexpansion
:: Just to make sure we're in the same directory as the script.
cd "%~dp0"
echo WScript.Echo Eval^(WScript.Arguments^(0^)^)>eval.vbs
for /f "tokens=*" %%A in (numbers.txt) do (
	for /f "tokens=*" %%B in ('echo %%A ^| find "$"') do (
		set line=%%B
		for /f "tokens=1,2 delims=$" %%Y in ("!line!") do (
			set num=%%Z
			set pfx=%%Y
		)
		echo !num!
		for /f %%N in ('cscript //nologo eval.vbs "!num!/500"') do (
			set result=%%N
		)
		set result=!result:,=.!
		for /f "tokens=1,2 delims=." %%C in ("!result!") do (
			if not "%%D"=="" (
				set rmndr=%%D
				set rmndr=!rmndr:~0,2!
				if !rmndr! LEQ 9 set rmndr=!rmndr!0
				set rmndr=!rmndr:~0,2!
				set result=%%C.!rmndr!
			) else (
				set result=%%C.00
			)
		)
		echo !pfx!$!result!>>calculated.txt
	)
	for /f "tokens=*" %%B in ('echo %%A ^|find /v "$"') do (
		echo %%B>>calculated.txt
	)
)
pause > nul

Marking answers that solve, or help helps you solve a problem as "Best Answer" also helps other people solve similiar ones in the future.

message edited by RainBawZ


Report •

#6
November 7, 2015 at 10:21:37
Brilliant, really! I'll vote the first answer as the best answer however It's all been best answers. I'm sure not everyone is as picky as me

Report •

Ask Question