FOR /F "tokens=1-7 delims=," %%G IN (C:\Users\asian\Desktop\FOR_LOOP\1sv1.csV) do echo %%G,%%H,%%I >> 1.csv
output:
20030701,1001,456.000000
20030701,1002,459.000000
20030701,1003,450.000000
20030701,1004,456.000000
20030701,1005,459.000000
my Query is that i want to divide value for %%I variable(456.000000)
output should be like this:
20030701,1001,228.000000
20030701,1002,229.5.000000
20030701,1003,225.000000
20030701,1004,228.000000
20030701,1005,229.5.000000
setlocal enabledelayedexpansion
(FOR /F "tokens=1-7 delims=," %%G IN (C:\Users\asian\Desktop\FOR_LOOP\1sv1.csV) do (
set /a "i=%%I*5"
echo %%G,%%H,!i:~0,-1!.!i:~-1!00000
)) > 1.csv
endlocal
Batch arithmetics has no decimals. So, to handle it, the value is multiplied by 10 (to gain an aditional position) and divided by 2 (from OP). Then, the value is splitted as the last digit is the first decimal.
Related
I know there are many arithmetic questions on here, but I have not found the specific answer to my question. I have a file with two values in it, the first always higher than the second. Today, the txt file has:
21.04
20.94
What I am trying to do is, via a batch file, subtract the second number from the first, and then insert than on a new line. Any assistance is appreciated.
Just incorporate powershell into the batch file.
to test from cmd:
#for /f %i in ('powershell 21.04 - 20.94') do #echo %i
So you can build a very basic calculator rather easily.
set /p "first=Enter first number: "
set /p "second=Enter Second Number: "
set /p "function=Select Function(+-/): "
powershell %first% %function% %second%
And offcourse you can use a for loop to assign the value to a variable should you want to use it elsewhere in your batch file.
#echo off
set /p "first=Enter first number: "
set /p "second=Enter Second Number: "
set /p "function=Select Function(+-/): "
for /f %%i in ('powershell %first% %function% %second%') do set "result=%%i"
echo %result%
in a batch-file you double the % in meta variables to %%i
Assuming file is called math.txt
#echo off
setlocal enabledelayedexpansion
set cnt=1
for /f "usebackq" %%i in ("d:\math.txt") do (
set var!cnt!=%%i
set /a cnt+=1
)
(powershell %var1% - %var2%)>output.txt
pause
This method works with numbers up to 9 total digits (and any number of decimals) as long as the input numbers have the same number of decimals:
#echo off
setlocal EnableDelayedExpansion
rem Read two numbers
( set /P "num1=" & set /P "num2=" ) < test.txt
rem Adjust *two* numbers for given decimals
set "decimals=2"
for %%i in (1 2) do (
set "num%%i=!num%%i:.=!"
for /L %%d in (1,1,%decimals%) do if "!num%%i:~0,1!" equ "0" set "num%%i=!num%%i:~1!"
)
rem Subtract second number from the first
set /A "result=num1 - num2"
rem Adjust result for given number of decimals
for /L %%d in (1,1,%decimals%) do if "!result:~%decimals%!" equ "" set "result=0!result!"
rem Output result with decimals
echo !result:~0,-%decimals%!.!result:~-%decimals%!
I think this works for reasonably sized numbers:
#echo off
rem read values from file specified as command line parameter
(
set /p value1=
set /p value2=
)<%1
rem split first value into whole and fractional parts
for /f "tokens=1,2 delims=." %%a in ("%value1%") do (
set beforedot1=%%a
set afterdot1=%%b
)
rem reconstruct first value as fixed point number
set afterdot1=%afterdot1%00000
set afterdot1=%afterdot1:~0,6%
set value1=%beforedot1%%afterdot1%
rem split second value into whole and fractional parts
for /f "tokens=1,2 delims=." %%a in ("%value2%") do (
set beforedot2=%%a
set afterdot2=%%b
)
rem reconstruct second value as fixed point number
set afterdot2=%afterdot2%00000
set afterdot2=%afterdot2:~0,6%
set value2=%beforedot2%%afterdot2%
rem subtract values
set /a diff=value1-value2
rem convert fixed point value back
if "%diff:~0,-6%" == "" (
set diff=0.%diff:~-6%
) else (
set diff=%diff:~0,-6%.%diff:~-6%
)
rem remove trailing zeros
:loop
if "%diff:~-1%" == "0" (
set diff=%diff:~0,-1%
goto :loop
)
echo %diff%
For instance:
file.txt contains:
4.3 - random1
5.6 - random2
2.2 - random3
3 - random4
1.8 - random5
I need a command that will output the highest number only, not the preceding text.
Ie.
Output = 5.6
You can give SORTN.bat a try.
Here is the code for it as well.
#ECHO OFF
if "%~1"=="/?" (
echo.Sorts text by handling first number in line as number not text
echo.
echo.%~n0 [n]
echo.
echo. n Specifies the character number, n, to
echo. begin each comparison. 3 indicates that
echo. each comparison should begin at the 3rd
echo. character in each line. Lines with fewer
echo. than n characters collate before other lines.
echo. By default comparisons start at the first
echo. character in each line.
echo.
echo.Description:
echo. 'abc10def3' is bigger than 'abc9def4' because
echo. first number in first string is 10
echo. first number in second string is 9
echo. whereas normal text compare returns
echo. 'abc10def3' smaller than 'abc9def4'
echo.
echo.Example:
echo. To sort a directory pipe the output of the dir
echo. command into %~n0 like this:
echo. dir /b^|%~n0
echo.
echo.Source: http://www.dostips.com
goto:EOF
)
if "%~1" NEQ "~" (
for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
set f=,%%B
(
set f0=!f:~0,%n%!
set f0=!f0:~1!
rem call call set f=,%%%%f:*%%f0%%=%%%%
set f=,!f:~%n%!
)
for /f "delims=1234567890" %%b in ("!f!") do (
set f1=%%b
set f1=!f1:~1!
call set f=0%%f:*%%b=%%
)
for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`##$*_-+=:;',.?/\ " %%b in ("!f!") do (
set f2=00000000000000000000%%b
set f2=!f2:~-20!
call set f=%%f:*%%b=%%
)
echo.!f1!!f2!!f!,%%B
rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)
I gave it a try using this input as an example
4.3 - random1
11.3 - random6
5.6 - random2
2.2 - random3
100.1 - random8
3 - random4
1.8 - random5
11.12 - random7
11.11 - random7
This is how I ran it but you should be able to capture the output as well using a FOR /F command just like Stephan showed you in his answer.
type sortme.txt |sortn.bat
Output
1.8 - random5
2.2 - random3
3 - random4
4.3 - random1
5.6 - random2
11.11 - random7
11.12 - random7
11.3 - random6
100.1 - random8
sort will sort in correct order (Attention: this is sorting strings, not numbers, but will work fine for your example. Note that with string comparison 5 or 6.3 are "bigger" than 15).
Put a for around to process the ouput (Standard tokens is 1 and Space is a standard delimiter, so the for /f gets only the first element - your desired number)
for /f %%a in ('sort t.txt') do set high=%%a
echo %high%
EDIT to also process numbers higher than 10. Note: there is no math involved - it's just clever string manipulation.
#echo off
setlocal enabledelayedexpansion
(
for /f "tokens=1,2 delims=. " %%a in (t.txt) do (
set a=0000%%a
if "%%b"=="-" (echo !a:~-4!) else (echo !a:~-4!.%%b)
)
)>temp.txt
type temp.txt
pause
for /f "tokens=1,2 delims=0" %%a in ('sort temp.txt') do set high=%%a
echo %high%
The following script does proper sorting of fractional numbers with eight digits before and after the decimal separator . at most:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (input file; `%~1` means first command line argument)
set /A "_ZPAD=8" & rem // (number of zeroes to be used for temporary padding)
set "_ZERO=" & for /L %%K in (0,1,%_ZPAD%) do set "_ZERO=!_ZERO!0"
for /F "usebackq delims=- " %%L in ("%_FILE%") do (
for /F "tokens=1,2 delims=." %%I in ("%%L.") do (
set "INT=%_ZERO%%%I" & set "FRA=%%J%_ZERO%"
set "$NUM_!INT:~-%_ZPAD%!_!FRA:~,%_ZPAD%!=%%L"
)
)
set "MIN=" & set "MAX="
for /F "tokens=2 delims==" %%L in ('2^> nul set $NUM') do (
if not defined MIN set "MIN=%%L"
set "MAX=%%L"
)
echo Minimum: %MIN%
echo Maximum: %MAX%
endlocal
exit /B
First of approaches, excuse me if I do not express myself well in English.
I'm debutante in batch and I need help to make a script
I articles.txt retrieves a document in which there are many lines.
some lines of my document
"T0047" ;"Tuyau 1km";"Marque2";"jardinage";"75 000";"promo"
"T00747";"Tuyau 1m";Marque2";"jardinage";"30 000";"promo"
First, I have to remove the quotation marks in the file.
It is done with:
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%a in (articles.txt) do (
set a=%%a
set a=!a:"=!
echo !a!
echo !a! >>resultat.txt
)
the result
T0047 ;Tuyau 1km;Marque2;jardinage;75 000;promo
T00747;Tuyau 1m;Marque2;jardinage;30 000;promo
Then I have to perform a multiplication on a column.
For this, I have the problem that if the space is not so mutiplication realize I made a script that removes spaces.
#echo off
setlocal enabledelayedexpansion
for /F "delims=; tokens=1-8" %%a in (resultat.txt) do (
set a=%%e
set a=!a: =!
echo %%a;%%b;%%c;%%d;!a!;%%f;%%g;%%h
echo %%a;%%b;%%c;%%d;!a!;%%f;%%g;%%h >>resultat2.txt
)
the result
T0047 ;Tuyau 1km;Marque2;jardinage;75000;promo
T00747;Tuyau 1m;Marque2;jardinage;30000;promo
Then I made my multiplication.
#echo off
setlocal enabledelayedexpansion
for /F "delims=; tokens=1-8" %%a in (resultat2.txt) do (
set a=%%e
:: set /a a=!a!/0.6
set /a a=!a!*16666/10000
echo %%a;%%b;%%c;%%d;!a!;%%f;%%g;%%h
echo %%a;%%b;%%c;%%d;!a!;%%f;%%g;%%h >>resultat3.txt
)
the result
T0047 ;Tuyau 1km;Marque2;jardinage;124995;promo
T00747;Tuyau 1m;Marque2;jardinage;49998;promo
Now, i add some text just after the first colomn
set champ2=MAGASIN_1;T
for /F "delims=; tokens=1,*" %%a in (resultat3.txt) do (
echo %%a;%champ2%;%%b
echo %%a;%champ2%;%%b >>resultat_final.txt
)
The actual result is:
T0047 ;MAGASIN_1;T;Tuyau 1km;Marque2;jardinage;124995;promo
T00747;MAGASIN_1;T;Tuyau 1m;Marque2;jardinage;49998;promo
Now I would add a space so that the figure is more readable.
T0047 ;MAGASIN_1;T;Tuyau 1km;Marque2;jardinage;124 995;promo
T00747;MAGASIN_1;T;Tuyau 1m;Marque2;jardinage;49 998;promo
This is the way I would do it:
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%A in (articles.txt) do (
set "a=%%A"
set a=!a:"=!
for /F "delims=; tokens=1-8" %%a in ("!a!") do (
set /A "g1=%%g*16666/10000"
set "g2="
for /L %%i in (1,1,3) do if defined g1 (
set "g2= !g1:~-3!!g2!"
set "g1=!g1:~0,-3!
)
echo %%a;%%b;%%c;%%d;%%e;%%f;!g2:~1!;%%h
echo %%a;%%b;%%c;%%d;%%e;%%f;!g2:~1!;%%h >> result.txt
)
)
articles.txt:
"T0047" ;"MAGASIN_1";"T";"Tuyau 1km";"Marque2";"jardinage";"75000";"promo"
"T00747";"MAGASIN_1";"T";"Tuyau 1m";Marque2";"jardinage";"30000";"promo"
result.txt:
T0047 ;MAGASIN_1;T;Tuyau 1km;Marque2;jardinage;124 995;promo
T00747;MAGASIN_1;T;Tuyau 1m;Marque2;jardinage;49 998;promo
Your program is good. Some tips:
Don't divide by a power of 10. Instead, remove the fractional part if you don't want it. Use *= . And to get the space in the number:
#echo off
set x=75000
set /a x *= 16666
set x=%x:~0,-4%
echo %x:~0,-3% %x:~-3%
I'll respond only to the multiplication section.
I can see nothing in your code that can possibly generte the two extra columns ;MAGASIN_1;Tand consequently, the target field 75000 and 30000 are in %%g, not %%e.
Comment : Do not use the "broken label" comment form ::comment within a block statement (a parenthesised series of statements) because it can terminate the block prematurely. Always use rem with a block.
So - modified code working on %%g
set a=%%g
rem set /a a=!a!/0.6
REM set /a a=!a!*16666/10000
set /a a=!a!*10/6
SET "a= !a:~-9,-6! !a:~-6,-3! !a:~-3!"
FOR /f "tokens=*" %%q IN ("!a!") DO SET "a=%%q"
echo %%a;%%b;%%c;%%d;%%e;%%f;!a!;%%h
Reason: Batch has a signed-32-bit limit, so if the source field is >~120000 then your calculation will generate a negative number (try 130000 for example) The revised calculation is more accurate and since intermediate results are less likely to exceed 2**31 can cope with larger values in the %%g field.
The set following the calculation changes the numeric value in a to
space(millions)space(thousands)space(units)
(The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".)
The for /f "tokens=*"... statement simply removes leading spaces from the value of a.
With the explanatin of the two additional columns, This revision should solve the "add-spaces" problem:
set a=%%e
rem set /a a=!a!/0.6
REM set /a a=!a!*16666/10000
set /a a=!a!*10/6
SET "a= !a:~-9,-6! !a:~-6,-3! !a:~-3!"
FOR /f "tokens=*" %%q IN ("!a!") DO SET "a=%%q"
echo %%a;%%b;%%c;%%d;!a!;%%f;%%g;%%h
however, if you want to skip the last step (insertion of 2 extra fields) then insert this line before the for line in the "multiplication" batch
set champ2=MAGASIN_1;T
and change the echo line in that batch to
echo %%a;%champ2%;%%b;%%c;%%d;!a!;%%f;%%g;%%h
Since you have a semicolon-delimited list of values where each item is enclosed within quotation marks, I would go for a standard for to get the items of each line and remove the enclosing quotation marks. The great advantage of this method is that it really cares about the quotation marks, so the list items may even contain semicolons on their own. The only disadvantage is that question marks and asterisks are not allowed in any of the list items:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Redirect all data to output file "resultat.txt" at once:
> "resultat.txt" (
rem Loop through all (non-empty) lines of input file "articles.txt":
for /F "usebackq delims=" %%L in ("articles.txt") do (
rem Reset list collector and loop index:
set "LIST="
set /A "INDEX=0"
rem Loop through the list items of the current line:
for %%I in (%%L) do (
rem Apply current list item with `""` removed, increment loop index:
set "ITEM=%%~I"
set /A "INDEX+=1"
rem Do numeric calculation for a certain list item:
setlocal EnableDelayedExpansion
if !INDEX! EQU 5 (
rem Convert item to a number, avoid error messages:
2> nul set /A "CALC=!ITEM!"
rem Do calculation with rounding (for negative and positive numbers):
if !CALC! LSS 0 (
set /A "CALC=(!CALC!*10-6/2)/6"
) else (
set /A "CALC=(!CALC!*10+6/2)/6"
)
rem Insert thousands separators (space) between every third digit:
set "CALC=!CALC:~-12,-9! !CALC:~-9,-6! !CALC:~-6,-3! !CALC:~-3!"
for /F "tokens=*" %%N in ("!CALC!") do (
set "ITEM=%%N"
)
)
rem Append separator (semicolon) and current item to list:
for /F delims^=^ eol^= %%S in ("!LIST!;!ITEM!") do (
endlocal
set "LIST=%%S"
)
)
rem Return built list, remove superfluous leading separator (`;`):
setlocal EnableDelayedExpansion
echo(!LIST:~1!
endlocal
)
)
endlocal
exit /B
The calculation herein incorporates rounding to the nearest integer, which works even for negative input numbers.
The newly generated list is stored into the new file resultat.txt.
I`m having issues with a batch file I have written to process a large amount of .wav music files with a tool. The encoded format is to have loop start and end points encoded within it.
#echo off
#echo -------CONVERSION---------
for /f "tokens=1-5" %%l in (test.txt) do (
#echo %%n.wavを%%ni.fcoへ変換中です
#echo %%o %%p
set /a endloop1=%%o
set /a endloop2=%%p
set /a endloop=endloop1+endloop2
#echo %endloop1% %endloop2% %endloop%
contool -e -br 144 -loop %endloop1% %endloop% ed%%n.wav ed%%n.fco
And the test.txt file I`m reading values from in its entirety is
bgmtbl BGM_Title 7552 73664 4997136
bgmtbl BGM_GameOver 7534 22464 4014400
bgmtbl BGM_Midtown 7101 773184 3718720
bgmtbl BGM_Cross_1 7102 112838 5827154
Where the fourth and fifth values are the ones I need - the loop start point and loop length
for some reason the endloop values use the numbers from the last line of the text file over and over! e.g
7552.wavを7552i.fcoへ変換中です
73664 4997136
112838 5827154 5939992
your parametter doesn't satisfy under the condition
0<= loopstart(112838) <= loopend(5939992) < Total Sample(5118208) element=0/1
7534.wavを7534i.fcoへ変換中です
22464 4014400
112838 5827154 5939992
your parametter doesn't satisfy under the condition
0<= loopstart(112838) <= loopend(5939992) < Total Sample(4125696) element=0/1
etc
Please help, this is very perplexing!
Another possibility:
#echo off &setlocal
for /f "tokens=1-5" %%l in (test.txt) do (
echo %%n.wavを%%ni.fcoへ変換中です
echo %%o %%p
set /a endloop1=%%o
set /a endloop2=%%p
set /a endloop=%%o+%%p
)
echo %endloop1% %endloop2% %endloop%
Here you do not need delayed expansion. The first access to the %variables% is after the end of the for code block.
#echo off
#echo -------CONVERSION---------
setlocal enableDelayedExpansion
for /f "tokens=1-5" %%l in (test.txt) do (
#echo %%n.wavを%%ni.fcoへ変換中です
#echo %%o %%p
set /a endloop1=%%o
set /a endloop2=%%p
set /a endloop=!endloop1!+!endloop2!
#echo !endloop1! !endloop2! !endloop!
contool -e -br 144 -loop !endloop1! !endloop! ed%%n.wav ed%%n.fco
)
More info : http://www.robvanderwoude.com/variableexpansion.php
i try to add two numbers received from a file.
But it shows only the last value of sum. Thx for the help!
#FOR /F "eol=; tokens=1-3 delims=, " %%i IN (test.txt) DO (
set m=%%j
set n=%%k
set /a sum=%m%+%n%
echo sum = %sum%
)
and in the test.txt i have
alex 4 5
john 6 7
and i want to see
sum=9
sum=13
it only shows
sum=13
sum=13
The problem is the percent expanding in the line set /a sum=%m%+%n% and echo sum = %sum%.
These are expanded before the FOR-loop is executed.
Therefore you got the result of the "global" set of sum.
It's better to use the delayed expansion, as then all variables enclosed with ! are expanded at runtime not parsetime
#echo off
setlocal EnableDelayedExpansion
FOR /F "eol=; tokens=1-3 delims=, " %%i IN (test.txt) DO (
set m=%%j
set n=%%k
set /a sum=m+n
echo sum = !sum!
)