Small Error in Batch File - Missing Operand - Need Help.

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Small Error in Batch File - Missing Operand - Need Help.

Post by MigrationUser »

03 May 2010 18:05
Ben Tiessen

Code: Select all

@echo on
:main
cls
set /p smokedDaily= How many times do you smoke a day?
set /p bummedSmoke= How many smokes out of %smokedDaily% are bummed or bought from someone?

set /p smokedWeekly= How many days a week do you smoke (1-7)?

set /p yearsSmoked= How many years have you smoked for?

:: selfBought calculates how many ciggarets they smoked that were bummed or bought.

set /a selfBought=%smokedDaily%-%bummedSmoke%

set /a costperCig= 10.50/25.00
::The program says Missing Operand right after this statement. But it still seems to run fine.
::I noticed that set /a will try and round everything to a whole number. Is there a problem with my ::numbers because of that?
::daily

set /a dailyCost=%selfBought%*%costperCig%+%bummedSmoke%
set /a weeklyCost=%dailyCost%*%smokedWeekly%
set /a yearlyCost=%weeklyCost%*52
set /a totalCost=%yearlyCost%*%yearsSmoked%

echo You will spend $%yearlyCost% on ciggarets this year.
echo You have spent $%totalCost% in your life on ciggarets.
pause
goto main
----------------------------

#2 03 May 2010 18:31
RG


Ben,
You need to use integers only. Change line to;

set /a costperCig= 1050/25

And then divide by 100 to convert from cents to $ later before you display.

Last edited by RG (03 May 2010 18:38)

Windows Shell Scripting and InstallShield

----------------------------

#3 03 May 2010 23:44
Ben Tiessen


Awesome. Thanks alot for the help. Works great now.

I also have an issue at school where annoying students make thousands of empty folders for no reason on the shared directory.
I wrote a program to delete everything in Shared that that person owns when they click on it.
It works because i named it Conterstrike 1.6
But is there anyway to delete empty folders that are read-only.
I cannot edit there files or anything

But it seems like the administrators have issues trying to delete them themselves.

Got any code insight?

Thanks smile

----------------------------

#4 04 May 2010 00:45
RG


Credit for the :IsDirEmpty subroutine goes to Ritchie Lawrence... see here.
https://www.commandline.co.uk/lib/treeview/index.php
Use it something like this: (note that in my example, the code following && only gets executed if ERRORLEVEL=0 (directory empty)

Code: Select all

@ECHO OFF
REM If you want to display deleted files do the next few lines
SET /A Empty=0
FOR /F "tokens=*" %%A IN ('DIR /B /D') DO CALL :IsDirEmpty "%%A" && (ECHO.%%A & RMDIR /Q "%%A" &  SET /A Empty+=1)
IF %Empty% GTR 0 (
   ECHO.The above %Empty% empty folders were deleted.
   ECHO.
   )

REM If you dont want to display deleted files, remove the stuff above and use the line below
FOR /F "tokens=*" %%A IN ('DIR /B /D') DO CALL :IsDirEmpty "%%A" && RMDIR /Q "%%A"
pause
GOTO :eof

:IsDirEmpty %dir%
::
:: By:   Ritchie Lawrence, 2003-09-22. Version 1.0
::
:: Func: If specified directory is empty of files and directories, then
::       errorlevel is set to zero. If directory is not empty or does not
::       exist, errorlevel is non-zero one. For NT4/2000/XP/2003.
:: 
:: Args: %1 Name of directory to be tested (by val)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS & dir/ad %1 >nul 2>nul || (endlocal & goto :EOF)
set i=0 & for /f %%a in ('dir %1/a/b 2^>nul') do set/a "i+=1"
md;2>nul & (if %i%==0 ver>nul) & endlocal & goto :EOF
Last edited by RG (04 May 2010 00:47)

Windows Shell Scripting and InstallShield

----------------------------

#5 04 May 2010 02:07
Ben Tiessen


Will this override user account privileges on a school network do you think?

----------------------------

#6 04 May 2010 02:17
RG


I don't think so. Maybe your sysadmin will give you delete privileges so he doesn't have to do the cleanup.
Or if you have some sort of logon script it could be added to that.

Windows Shell Scripting and InstallShield

----------------------------

#7 04 May 2010 02:24
Ben Tiessen


Okay cool. Well thanks for the help.

is there anyway to get a desktop shortcut to press F5 or something?

----------------------------

#8 04 May 2010 02:25
RG


BTW,
In your program above you may want to correct the spelling of cigarettes smile
Normally I wouldn't nit pick over that... but since you are a teacher and this may be used for students... let's save the embarassment.
On the other hand... I don't know any French. Is that the French spelling?

Windows Shell Scripting and InstallShield

----------------------------

#9 04 May 2010 02:33
Ben Tiessen


Haha i still can't spell them! And i actually am a student. But i am against all the other students crap. They are so disrespectful to the teachers and everything thats in there path. I hate it. But i want to make the shared directory something useful instead of a bunch of empty stuff.

I am actually only 17 years old. But i love batch programming. Alot easier than C++ in my oppinion and you don't need to install a compiler tongue

Any ideas? xd

----------------------------

#10 04 May 2010 02:50
RG


If you want to help out the admin you could modify the bat file that I posted above (probably delete the lines for displayng the deleted files). Test it on your folders, and then give it to the sysadmin so that they can run it. You may want to put a /S in the DIR command so that it traverses all subdirectories. See DIR /?
Of course, even after you have tested it, it would be wise to run it immediately after having done a backup. Nothing worse than deleting a whole bunch of stuff accidently. Practice on a USB stick. Good luck!

Windows Shell Scripting and InstallShield

----------------------------

#11 04 May 2010 03:04
RG
Ben Tiessen wrote:

I am actually only 17 years old. But i love batch programming. Alot easier than C++ in my oppinion and you don't need to install a compiler tongue

Any ideas? xd
Yes, Got to college and learn the C++ (or Java, or whatever). You will have a much better chance of landing a good job if you want a job working with computers.
Then when you have that job you can still make good use of batch programming skills.

Windows Shell Scripting and InstallShield
Post Reply