So I'm working on a final project for one of my classes, and the goal is basically to have the user input an initial investment, and the number of years the value will depreciate...it depreciates by a constant value which is determined by the investment divided by the number of years.
In the for loop, it should subtract the 'annual' from the 'bookvalue' and print out the new bookvalue for each year...the math itself works correctly (I believe?) because if I echo the values outside my for loop they are what they should be, but with the echo statement inside the for loop I don't get what I need it to output (the bookvalue doesn't change inside the loop).
Not really sure where to go with this, and Google hasn't done me very well yet. Thanks for any help that can be given!
#ECHO OFF
:HEADER
REM Program : final_q8.bat
REM Author :
REM Date :
REM Purpose : if statement integer sort
REM -----------------------------------------------
:START
CLS
SET /P INVEST="Please enter the initial investment amount: "
SET /P YEARS="Please enter the # of years the machine will be depreciated: "
SET /A ANNUAL=INVEST/YEARS
SET BOOKVALUE=%INVEST%
ECHO YEAR ANNUAL DEPRECIATION BOOK VALUE
FOR /L %%C IN (1, 1, %YEARS%) DO (
SET /A BOOKVALUE=BOOKVALUE-ANNUAL
ECHO %%C %ANNUAL% %BOOKVALUE%
)
PAUSE
:END
:: Exit and return an errorcode of 0
EXIT /B 0
Try enabling Delayed expansion:
#echo off
setlocal enabledelayedexpansion
...
Then use exclamation mark inside For loop
FOR /L %%C IN (1, 1, %YEARS%) DO (
SET /A BOOKVALUE=BOOKVALUE-ANNUAL
ECHO %%C !ANNUAL! !BOOKVALUE!
)
Related
I need to create an integer array that stops at a limit number and then goes on after another fixed value. Something like [1 2 3 4 5 10 11 12 13 14 15 16], where 5 is the limit number and 10 where it restarts.
Can you do something like set arr = [1:%lim%, 10:%max%] ?
#ECHO OFF
SETLOCAL
CALL :buildlist mylist 1 5 10 16
ECHO list is %mylist%
PAUSE
GOTO :eof
:: Produce a list of integers
:: %1 is listname
:: %2 is start value
:: %3 is end-value for range
:: %4 is restart-value
:: %5 is end-value
:buildlist
SET "%1="
FOR /L %%v IN (%2,1,%5) DO (
IF %%v leq %3 CALL SET "%1=%%%1%% %%v"
IF %%v geq %4 CALL SET "%1=%%%1%% %%v"
)
CALL SET "%1=%%%1:~1%%"
GOTO :eof
Batch really doesn't have arrays, but can simulate one with a little imagination.
The above routine produces a list of values to the specification, ready for processing by a for statement.
Although it's possible to use delayedexpansion, it's possible to avoid that facility. By CALL ing a set command, the command is parsed before execution so to decode the hieroglyphics, where %1 is the variable name "var"
SET "%1=%%%1%% %%v"
is processed as:
SET "var=%var% %v"
as %% is an escaped-%, and %v will be replaced by the value of the metavariable (loop-control variable) %%v
Similarly,
SET "%1=%%%1:~1%%"
is executed as
SET "var=%var:~1%"
which deletes the first character, which will be a space.
This problem is about several for /L commands really, although it could be described in terms of nested if commands or in other different ways...
There are several different methods to give the values of the for commands, and also to implement they. I think this is the simplest one:
#echo off
setlocal EnableDelayedExpansion
set ranges="1:5" "10:16"
set "list="
for %%a in (%ranges::= 1 %) do for /L %%i in (%%~a) do set "list=!list! %%i"
echo list=%list%
Note that this method also works with several (more than two) ranges of values.
PS - This result is a list, not an array
Batch doesn't have OR operators, so you'll have to make do with two separate if statements - one for the first set of numbers and one for the second set of numbers. You can, however, put both of those in the same for loop:
#echo off
setlocal enabledelayedexpansion
set "lim=5"
set "max=10"
REM Setting counter to -1 because we're going to increment the counter and then
REM use it, and arrays famously start at zero.
set "counter=-1"
for /L %%A in (1,1,16) do (
if %%A LEQ %lim% (
set /a counter+=1
set "array[!counter!]=%%A"
)
if %%A GEQ %max% (
set /a counter+=1
set "array[!counter!]=%%A"
)
)
REM Display the contents of the array just to prove it worked.
REM Alphabetic order is used, so array[10] and array[11] are going to print before array[1]
set array[
I'm writing a script where a user has to put in a time. The issue I'm running into is that the user can enter anything and batch script will accept it. My idea to check for this was to extract where the colon should be in the time inputted and check if it's there with this:
SET check=%utime:2,1%
IF %check%==: [continue with script]
So that it'd skip the first two characters in say 01:00 and extract the semi colon, set it to check and then compare check to see if it was a colon or not. This works great but then I realized that a user could input anything with a colon in the third space and get past. For a first step my idea to check for this was to have a FOR loop check all the five possible spaces to see if they were empty or not with this code:
SET conf=1
FOR /L %%i IN (1,1,5) DO (
echo %%i
SET check=%utime:%%i,1%
IF [%check%] == [] SET conf=0
)
conf would be a true or false variable (0 or 1) that I could then evaluate with an IF statement to then see if it's a valid format. But what ended up happening is that conf was set to 0 but it was not correctly checking the characters within the time variable. Leaving the echo on I could see that check was not correctly being set meaning conf was always set to 0 no matter what was inputted from the user. Here is the code in question in context just in case (This portion is only meant to check for 5 characters being present):
:Start
SET /p utime=Please insert the time (hh:mm):
CALL :CheckTime
IF conf EQU 1 goto :Valid
ECHO Please enter a valid time!
GOTO :Start
:Valid
ECHO Thank you for entering a valid time!
PAUSE
EXIT
:CheckTime
SET conf=1
FOR /L %%i IN (1,1,5) DO (
echo %%i
SET check=%utime:%%i,1%
IF [%check%] == [] SET conf=0
)
EXIT /B
A little bit different approach then Compo.
:: Q:\Test\2018\06\14\SO_50860883.cmd
#Echo off
:Start
Set "MyTime="
SET /p MyTime=Please insert the time (hh:mm):
If not defined MYTime Exit /B 0
CALL :CheckTime "%MyTime%"|| (ECHO Please enter a valid time! &GOTO :Start)
ECHO Thank you for entering a valid time %MyTime% !
ECHO=
goto :Start
:CheckTime passed value not a fixed var
Echo=%~1|Findstr "^[0-9]:[0-5][0-9]$ ^[0-1][0-9]:[0-5][0-9]$ ^2[0-3]:[0-5][0-9]$" 2>&1>Nul || Exit /B 1
To not allow the input of a single digit hour change the last line to :
Echo=%~1|Findstr "^[0-1][0-9]:[0-5][0-9]$ ^2[0-3]:[0-5][0-9]$" 2>&1>Nul || Exit /B 1
Perhaps using FindStr would be suitable:
:Start
Set "uTime="
Set /P "uTime=Please insert the time (hh:mm): "
Set "uTime=%uTime:~,5%"
If Not Defined uTime GoTo :Start
Echo %uTime% |FindStr /R "[0-1][0-9]:[0-5][0-9] 2[0-3]:[0-5][0-9]">Nul||GoTo :Start
Echo Thank you for entering a valid time!
Pause
I am writing a batch script and I am having trouble echoing a variable, here is the script,
#echo off
set num1=1
set num2=10
set /a "out=%num1%*%num2%"
echo %out%
pause`
The output I receive is 10 which makes sense but I want it to echo 'num1' ten times instead of multiplying 'num1' by 'num2'. So I want the output to be 1111111111.
Also I don't want to loop the command 10 times as I am putting the output into a text file with 'output>> file.txt' otherwise I will end up with this in the text file,
1
1
1
1
1
1
1
1
1
1
I want to end up with 1111111111, thank you.
#ECHO OFF
SETLOCAL
set num1=1
set num2=10
SET "out="&FOR /L %%a IN (1,1,%num2%) DO CALL SET "out=%%out%%%%num1%%"
echo %out%
GOTO :EOF
The SET "out=" and FOR /L %%a IN (1,1,%num2%) DO CALL SET "out=%%out%%%%num1%%" may be on separate lines if desired. Setting out to nothing is simply a safety measure to ensure that if it contains a value, it's cleared first.
The for /L performs the call command num2 times.
The call command executes SET "out=%out%%num1%" in a subprocess because each %% is interpreted as an escaped-% (% is the escape character for %) - "escaping" a character means turning off its special meaning.
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.
Just to show a different method with set /A and a loop:
#echo off
set /A "num1=1,num2=10,out=0"
:loop
set /a "out*=10,out+=num1,num2-=1"
If %num2% gtr 0 goto :loop
echo %out%
pause
This is the simplest way to solve this problem, using Delayed Expansion.
#echo off
setlocal EnableDelayedExpansion
set num1=1
set num2=10
set "out="
for /L %%i in (1,1,%num2%) do set "out=!out!%num1%"
echo %out%
pause
PS - The multiply term is not exact in this case; perhaps "echo a variable the times indicated by another variable" be more clear...
If what you want is to print num1 num2 times in the same line you can do something like:
#echo off
set "num1=1"
set "num2=10"
(for /L %%i in (1,1,%num2%) do set /p "=%num1%" <nul
echo()>file.txt
The command set /p "=%num1%" <nul prints the text %num1% in the current line without the LF character. So num1 gets printed num2 times in the same line.
::Compare with available valid arguments
FOR /L %%i IN (1,1,!avArgc!) DO (
FOR /L %%j IN (1,1,!argc!) DO (
IF !avArgv[%%i]!==!argv[%%j]! (
echo Match: !avArgv[%%i]!
::Check the next option
SET /A nextArg=%%j
SET /A nextArg+=1
FOR /L %%n IN (!nextArg!,1,!nextArg!) DO (
IF !nextArg! LEQ !argc! (
echo next arg: !argv[%%n]!
call :CheckSubOption
)
)
)
)
)
In my above code example - How do I take for loop variable like %%j and increment itself within the for loop like this %%j++ ? Current solution that I have (which is messy and I don't like it) is to create a new variable and set it to the value of %%j and then increment that variable and start using that variable like this:
::Check the next option
SET /A nextArg=%%j
SET /A nextArg+=1
Observing your code and your intention, it would seem that you would want to skip numbers during the loop structure. The way you want to change it though would be destabilizing. In most scripting languages such as matlab,bash, and batch, the variable that is used in for-loops serves as a frame of reference within the loop. When you tell the code to run a particular for-loop, it will run that computation regardless if the parameters of it changed. A real world example of this is the professor who is using outdated figures to solve a problem and it isnt until the next day he receives the new figures. The professor cant change his answer accordingly because he doesnt have the new data yet.
This does not mean this problem is unsolvable. In fact there are a variety of ways to approach this. The first one which is a little more complicated involves a nested For structure.
#echo off
set /p maxLength=[Hi times?]
set skip=0
FOR /L %%i IN (1,1,%maxLength%) DO (call :subroutine %%i)
echo alright im done.
pause
GOTO :eof
rem the below code uses a for loop structure that only loops 1 time based on the passed argument from the overall for loop as so to make changes to how its run.
:subroutine
set /a next=%1+%skip%
FOR /L %%r IN (%next%,1,%next%+1) DO (call :routine %%r)
GOTO :eof
:routine
if %1==3 (set /a skip=1)
echo %skip%
echo %next%
echo %1
pause
GOTO :eof
When running the program, the variable next will skip the value of 3 if the maxlength variable is greater than 3.
The reason this is so is because the nested for-loop only runs once
per iteration of the overall for loop
. This gives the program time to reset the data it uses, thanks to the call command which serves as a way to update the variables. This however is extremely inefficient and can be done in much less lines of code.
The second example uses GOTO's and if statements.
#echo off
set jump=1
:heyman
set /A "x+=%jump%"
if %x%==4 (set /A "jump=2")
echo %x%
if %x% LSS 10 goto heyman
echo done!
This code will essentially echo the value of x thats incremented each time until it reaches the value of 10. However when it reaches 4, the increment increases by 1 so each time it runs the loop increments the x value by 2. From what you wanted, you wanted to be able to change the way the value of %%j increments, which can not be done as %%j is a statement of where the for-loop is in its computation. There is no difference in what can be accomplished with for-loops and goto statements except in how they are handled.
While i unfortunately don't have the correct form of your code yet, i know that code examples i have given can be utilized to achieve your particular desire.
The general solution for thoses case is to not rely on blocks inside loops/if but instead to use subroutines where you are not blocked by the level of evaluation.
FOR /L %%i IN (1,1,!avArgc!) DO call :Loop1 %%i
goto :EOF
:Loop1
FOR /L %%j IN (1,1,!argc!) DO call :Loop2 %1 %%j
goto :EOF
:Loop2
IF !avArgv[%1]!==!argv[%2]! (
echo Match: !avArgv[%1]!
::Check the next option
SET /A nextArg=%2+1
call :CheckOpt %nextArg%
)
goto :EOF
:CheckOpt
IF %1 LEQ %argc% (
echo next arg: !argv[%1]!
call :CheckSubOption
)
I'm trying to write manual document validation part of my program. It's basically opening all the pdf documents one by one in the same folder. When its open i would like to echo few possibilities for user. Here starts the problem. I have around 180 possible choices. I was thinking to ask for the first letter of choice. Then it will echo all choices with started with X letter and user has to simply enter the number of this choice. So for example we have :
1. Asomething
2. Asomename
3. Asomenametoo
4. Bname
5. Bname 2
6. Bname 3
I want user to choose first letter and print possible choices. When the choice is made program should add some string to txt file with the same name in the same folder. Here i have a problem with IF statement inside FOR loop. I wanted to use goto but i can't do it inside FOR loop.
I can set up all the strings for each number before. For example : When you choose 1 it will add SomeString to txt. It's important to use choice option to avoid any typo's. Does anybody knows any other way to do this inside FOR loop ?
CODE:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
I'm having some trouble understanding the problem you're having, but it looks like all of the statements following the IF should all be conditions of the IF, not just the ECHO statement. For that, you can put the entire block in parentheses like this:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" (
ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
) else (
goto :EOF REM Just an example of else
)
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
Were you having some problem using goto in the FOR loop?