howto get totals from .bat math problem with integers? - variables

Originally I had this file while it did work it required alot of extra work and was not the most effective or efficient way of doing so
#echo off
set /P x="Percentage:"
set /P y="Price:"
set /P Z="Fixed Rate:"
cscript //nologo calculate.vbs %x% %y% %z% > results.txt
set /P charge= < results.txt
pause
del results.txt
echo charge Fee Total: %charge%
cscript //nologo calculate2.vbs %y% %charge% > results2.txt
set /P total= < results2.txt
pause
del results2.txt
echo sale total amount: %total%
pause
after several steps in getting the results I was looking for a Stackoverflow member Gerhard was kind enough to help me with a more efficient and better way of coding what I was looking to achieve. However I am still missing one line of code that I can not figure out I am sure its simple but I do not fully understand how to do so ...
here is the code he was gracious enough to assist with
#echo off
if not exist "calculations.txt" call :setVars
if exist "calculations.txt" for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
echo fixed rate = %Z%
echo Percentage = %x%
choice /c CR /M "Continue or re-set?"
if errorlevel 2 call :setVars
for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
set /P "y=Price: "
for /F "delims=" %%i in ('powershell ((%x% / 100^)*%y% + %z%^)') do echo %%i
goto :EOF
:setVars
set /P "x=Percentage: "
set /P "Z=Fixed Rate: "x
(echo x=%x%
echo z=%z%
)>"calculations.txt"
the final piece of the puzzle I am looking for is how to also give the results of the equation at the end before the EOF and the original price ... so basically the value of do echo %%i + %y% to echo to the screen after the results of the equation before the EOF if anyone can assist or direct me on how I can add these I would be most grateful and Thank you in advance

Related

Iterating through a folder batch

I'm somewhat new to batch, and I'm trying to build a program in batch that renames all the files to numbers, but it just doesn't work, I have no idea what the problem is here, I hope someone can help me find it.
#echo off
SET x=0
for %%I in (*) do(
SET /a x+=1
REN %%I %x%
)
pause
How about this quick one ?
I provided an if condition to prevent the batch from renaming itself in case you have it in the same directory where your files are. (e.g. while testing)
You might want to change this according to your needs...
numberize.bat
#ECHO OFF
SetLocal EnableDelayedExpansion
set /a x=0
for %%i in (*) do ( call :doit "%%i" )
goto eof
:doit
if /I %1 NEQ "numberize.bat" (
set /a "x=x+1"
ren %1 %x%
)
EXIT /B
:eof
EndLocal

for /f loop "tokens and delimiters" didn't work with variables (batch file)

recently I was making a batch file I ran into a problem,
when I made a for /f loop, I used a variable in "tokens", like this example:
for /f "delims= tokens=!count2!" %%a in ('type test.txt 2^>nul') do (
like you can see, i have the variable !count2!, in the loop.
and when i tested it, it displayed !count2!" was unexpected at this time. And I dont know why?
can anyone help?
here is the full code of what I have tried:
#echo off
setlocal enableextensions enabledelayedexpansion
set "count1=0"
set "count2=0"
for /l %%b in (1 1 10) do (
set /a "count2+=1"
set /a "count1+=1"
for /f "delims= tokens=!count2!" %%a in ('type test.txt 2^>nul') do (
set "str!count1!=%%a"
)
)
echo !str1! !str2! !str3! !str4!
pause
by the way, test.txt contains test hello # ###
and, I want to set test, hello, # and ### as their own variable (str1, str2, str3 and str4).
and yes I have tried to do it with % instead of !
tell me if i wasn't clear enough!
thanks for all help :)
why bothering with tokens?
#echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "delims=" %%i in (test.txt) do for %%j in (%%i) do (
set /a count+=1
set str!count!=%%j
)
set str

CMD: extract part of a filename and use it as variable

I ripped my CDs with EAC to CUE+WAV files, added cover art and all my files are in the same folder with filenames pattern "Album artist - Album title", for ex.:
Clannad - Legend.wav/cue/jpg
David Bowie - Best Of Bowie [Disc 1].wav/cue/jpg
David Bowie - Best Of Bowie [Disc 2].wav/cue/jpg
I'm new to this so I wrote a simple CMD batch to convert my music to FLAC format, but it requires manual copying and pasting of the actual wav/cue/jpg filenames and input of album artist, disc no. and total disc no. for their corresponding tags. It cannot be stored in cuesheet file for some reason, but in my case I have them in filenames as you can see above).
ECHO WAV/CUE/JPG FILENAME
SET /P "input="
ECHO ALBUMARTIST
SET /P "albumartist="
ECHO DISCNUMBER
SET /P "discnumber="
ECHO TOTALDISCS
SET /P "totaldiscs="
flac.exe -0 --picture="D:\Music\%input%.jpg" --tag-from-file="CUESHEET=D:\Music\%input%.cue" -T "ALBUMARTIST=%albumartist%" -T "DISCNUMBER=%discnumber%" -T "TOTALDISCS=%totaldiscs%" "D:\Music\%input%.wav"
My question is about automation of converting all my ripped albums. How can I extract album artist/disc no./total disc no. info from filenames and loop that for every .wav file?
#ECHO OFF
SETLOCAL
SET "destdir=U:\destdir"
PUSHD "%destdir%"
:: Find all .jpgs where there is a .wav and .cue with the same name
FOR /f "delims=" %%a IN ('dir /b /a-d *.jpg') DO IF EXIST "%%~na.wav" IF EXIST "%%~na.cue" (
FOR %%b IN (input albumartist discnumber totaldiscs) DO SET "%%b="
SET "input=%%~na"
FOR /f "delims=-" %%b IN ("%%a") DO SET "albumartist=%%b"
FOR /f "tokens=2delims=[]" %%b IN ("%%a") DO SET "disc=%%b"
IF DEFINED disc (
FOR /f "tokens=1delims=[]" %%d IN ("%%a") DO FOR /f %%c IN ('dir /b "%%d[*.wav"') DO SET /a totaldiscs+=1
)
CALL :gflac
)
POPD
GOTO :EOF
:gflac
:: remove trailing spaces from INPUT
IF "%albumartist:~-1%"==" " SET "albumartist=%albumartist:~0,-1%"&GOTO gflac
:: presume default for disc and totaldiscs
IF DEFINED disc (FOR /f "tokens=2" %%d IN ("%disc%") DO SET /a disc=%%d) ELSE (SET /a disc=1)
IF NOT DEFINED totaldiscs SET /a totaldiscs=1
ECHO( flac.exe -0 --picture="D:\Music\%input%.jpg" --tag-from-file="CUESHEET=D:\Music\%input%.cue" -T "ALBUMARTIST=%albumartist%" -T "DISCNUMBER=%discnumber%" -T "TOTALDISCS=%totaldiscs%" "D:\Music\%input%.wav"
GOTO :eof
You would need to change the setting of destdir to suit your circumstances.
The above will merely echo the required flac line. I left it as you posted but set the scant test data you posted up as I interpret it (ie. there is a set of 3 files) on my U: drive.
Sadly, you've given us insufficient information. I've assumed that you need all three files to be present, and the default for totaldiscs is 1.
First, look for all .jpgs, and if there is a corresponding .wav and .cue then process for flac generation as follows:
Set input to the name part of the .jpg found
set albumartist to the first part of the filename, up to the -
get the [disc n] string if it's present
count the number of .wavs that start with the filename up to the [
generate the flac line.
Within the generation of the flac line, we strip off the trailing spaces from input, convert disc n to n or set disc to 1 (although this information may not be needed), and set the totaldiscs to 1 if it's not been calculated.
You don't say what flac produces as output, but I'd suggest that you further gate that filetype so that the procedure doesn't run if the %input%.finalproductwhateverthatis file is present.
[edited per dbenham's comments]
This is very similar to Magoo's answer, with some bug fixes, and everything is done in one master loop without a CALL. As with Magoo's answer, modify your destination folder at the beginning to suit your needs.
#echo off
:: Delayed expansion must be disabled to protect ! when expanding FOR variables.
:: It is normally disabled by default, but I'm making it explicit, just to be sure.
setlocal disableDelayedExpansion
:: Define where source files are coming from
set "source=D:\Music"
:: Define where output should be stored
set "destination=D:\Music"
pushd "%destination%"
:: Iterate each .wav file (A) and only proceed if .jpg and .cue also exists
for /f "delims=" %%A in ('dir /b /a-d "%source%\*.wav"') do if exist "%source%\%%~nA.jpg" if exist "%source%\%%~nA.cue" (
%= Get base name with path, but without extension =%
set "file=%source%\%%~nA"
%= Extract "artist - album " (B) and "Disc #" (C) from base name (~nA) =%
for /f "delims=[] tokens=1,2" %%B in ("%%~nA") do (
%= Extract "artst " (D) from "artist - abum ". (~nxD) trims trailing space =%
for /f "delims=-" %%D in ("%%B") do set "artist=%%~nxD"
%= Extract the number (E) from "Disc #", use 1 as default if not there =%
set "disc=1"
for /f "tokens=2" %%E in ("%%C") do set "disc=%%E"
%= Count the number of discs (F), will be 0 if no [Disc #] =%
%= The [ is appended to name to prevent something like "Greatest Hits 2" from matching "Greatist Hits" =%
for /f %%F in ('dir /b /a-d "%source%\%%B[*.wav" 2^>nul ^|find /c /v ""') do set "count=%%F"
%= temporarily enable delayed expansion to access variables set within loop =%
setlocal enableDelayedExpansion
%= Set count to 1 if no [Disc #] =%
if !count! equ 0 set /a count=1
flac.exe -0 --picture="!file!.jpg" --tag-from-file="CUESHEET=!file!.cue" -T "ALBUMARTIST=!artist!" -T "DISCNUMBER=!disc!" -T "TOTALDISCS=!count!" "!file!.wav"
%= pop the setlocal stack to get back to state at beginning of loop =%
endlocal
)
)
popd
You may want to add a check to only proceed if the FLAC file does not already exist so you can run the script multiple times without reprocessing files. The outer loop would look something like this, but I can't be sure since I don't know the format of the output file name:
for /f "delims=" %%A in ('dir /b /a-d "%source%\*.wav"') do if exist "%source%\%%~nA.jpg" if exist "%source%\%%~nA.cue" if not exist "%destination%\%%~nA.flac" (
The logic is much simpler using my JREPL.BAT utility if you understand regular expressions:
#echo off
:: Delayed expansion must be disabled to protect ! when expanding FOR variables.
:: It is normally disabled by default, but I'm making it explicit, just to be sure.
setlocal disableDelayedExpansion
:: Define where source files are coming from
set "source=D:\music"
:: Define where output should be stored
set "destination=D:\music"
pushd "%destination%"
:: Iterate all *.wav and use JREPL to format result as "fullFileName|artist - album|artist|Disc#"
:: %%A %%B %%C %%D
:: Only proceed if .jpg and .cue also exist
for /f "tokens=1-4 delims=|" %%A in (
'dir /b /a-d "%source%\*.wav"^|jrepl "^((.+?) - .+?)(?:\[Disc (\d+)])?\.wav$" "$&|$1|$2|$3" /i'
) do if exist "%%~nA.jpg" if exist "%%~nA.cue" (
%= disc and count are both 1 if %%D is empty =%
if "%%D" equ "" (
flac.exe -0 --picture="%source%\%%~nA.jpg" --tag-from-file="CUESHEET=%source%\%%~nA.cue" -T "ALBUMARTIST=%%C" -T "DISCNUMBER=1" -T "TOTALDISCS=1" "%source%\%%A"
%= else count the number of .wav files =%
) else for /f %%E in ('dir /b /a-d "%%B[*.wav"^|find /c /v ""') do (
flac.exe -0 --picture="%source%\%%~nA.jpg" --tag-from-file="CUESHEET=%source%\%%~nA.cue" -T "ALBUMARTIST=%%C" -T "DISCNUMBER=%%D" -T "TOTALDISCS=%%E" "%source%\%%A"
)
)
popd
Again, you can add an IF to the outer loop to proceed only if the .flac file does not already exist in the destination.

Batch For Variables Printing Registry CSV

When querying the below via cmd
REG QUERY "HKCU\Printers\Connections"
The output is:
HKEY_CURRENT_USER\Printers\Connections\,,xxx-SD-KDP11,Colour
HKEY_CURRENT_USER\Printers\Connections\,,xxx-SD-KDP11,Mono
However when running the Batch file containing code below
#echo OFF
setlocal enableextensions enabledelayedexpansion
echo ============================
echo Network Printers ...
if exist network_printers.txt del network_printers.txt
if exist network_printers2.txt del network_printers2.txt
set reg_keys=
set blank=
set printers_network_paths=
set printers_network_types=
REG QUERY "HKCU\Printers\Connections" > network_printers.txt
for /f "tokens=* delims=" %%A in ('type network_printers.txt') do set network_printers_init=!network_printers_init! %%A
echo !network_printers_init! > network_printers2.txt
echo !network_printers_init!
pause
for /f "usebackq tokens=1,* delims=, skip=4" %%a in ('network_printers2.txt') do (
set reg_keys = !reg_keys! %%a
)
for /f "tokens=2,* delims=, skip=4" %%a in ('type network_printers2.txt') do (
set blank = !blank! %%b
)
for /f "tokens=3,* delims=, skip=4" %%a in ('type network_printers2.txt') do (
set printers_network_names = !printers_network_names! %%c
)
for /f "tokens=4,* delims=, skip=4" %%a in ('type network_printers2.txt') do (
set printers_network_types = !printers_network_types! %%d
)
echo reg_keys: !reg_keys!
echo blank: !blank!
echo Network Printers paths: !printers_network_names!
echo Network Printers types: !printers_network_types!
echo Network Printers paths: !printers_network_names! >> %file%
echo Network Printers types: !printers_network_types! >> %file%
pause
the .csv file produced has nothing for !printers_network_names! and !printers_network_types!
Also the echo lines show nothing being stored in the last 4 variables.
Please advise?
In a for /f loop, "skip=4" means skip 4 lines, but network_printers2.txt only contains one line.
There's some other stuff that can be improved. Mainly, when you set a variable in batch scripting, you should set "varname=string", quoted like that and with no spaces surrounding the =. You can also do what you need without the intermediate text files and the useless use of type.
I'm not entirely certain what you want the end result to be, but it looks like you're making your script a whole lot more complicated than it needs to be.
#echo OFF
setlocal enableextensions enabledelayedexpansion
echo ============================
echo Network Printers ...
for /f "tokens=2* delims=," %%A in ('REG QUERY "HKCU\Printers\Connections"') do (
set "printers_network_names=!printers_network_names! %%A"
set "printers_network_types=!printers_network_types! %%B"
)
echo Network printer paths: !printers_network_names!
echo Network printer types: !printers_network_types!
Result:
============================
Network Printers ...
Network printer paths: xxx-SD-KDP11 xxx-SD-KDP11
Network printer types: Colour Mono

Batch Help: Removing Characters from a Variable with number inputs

Is it possible to input the amount of characters to remove from the beginning and the end of a variable using other pre-defined variables? or did i just make a code mistake? because i've tried something like this:
#echo off
CLS
#setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=%word:~!numb1!,-!numb2!%
echo.
echo Before: !word!
echo.
echo After: !new!
echo.
pause >nul
exit
And instead of removing characters from the beginning and end of the batch with a pre defined variable, for example if input 3 for numb1 and numb2 when it echo's !new! instead of removing the characters it shows After: word:~3,-3 Some help would be appreciated please, and thanks for anyone who does help.
#echo off
CLS
#setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=!word:~%numb1%,-%numb2%!
echo.
echo Before: !word!
echo.
echo After: !new!
echo.
pause >nul
exit