![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | math with "GB/MB/KB" in variables fails, without variables works? I'm making a script to calculate time for a file to download, I want to be able to input things like "10GB" and "5GB" and "3.3KB" in the following script: $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)" $dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes allowed)" $dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)" $time=($totalsize-$dledsize)/$dlspeed/60/60/24 write-host [string]($time) + " days until file is downloaded." but it gives me an error that says: "Method invocation failed because [System.String] doesn't contain a method named 'op_Subtraction'. At line:1 char:19 + $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24" but when I try the script in the shell, I can do things like "$totalsize-5GB" and get the correct result. But I can't use two variables with the size suffixes. Is this by design or a bug? And is there a (simple) way around it? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: math with "GB/MB/KB" in variables fails, without variables works? On Nov 1, 12:27*pm, "James R. Gentile" <n...@xxxxxx> wrote: Quote: > I'm making a script to calculate time for a file to download, I want to be > able to input things like "10GB" and "5GB" and "3.3KB" in the following > script: > > $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)" > $dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes > allowed)" > $dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)" > > $time=($totalsize-$dledsize)/$dlspeed/60/60/24 > > write-host [string]($time) + " days until file is downloaded." > > but it gives me an error that says: > > "Method invocation failed because [System.String] doesn't contain a method > named 'op_Subtraction'. > At line:1 char:19 > + $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24" > > but when I try the script in the shell, I can do things like > "$totalsize-5GB" and get the correct result. But I can't use two variables > with the size suffixes. Is this by design or a bug? And is there a (simple) > way around it? Use the Invoke-Expression Cmdlet on each of the input values to evaluate them to their numerical equivalents: $value = Invoke-Expression ( Read-Host "Input file size..." ) Jeff |
My System Specs![]() |
| | #3 (permalink) |
| | Re: math with "GB/MB/KB" in variables fails, without variables works? On Nov 1, 2:48*am, Jeff <jeff.hill...@xxxxxx> wrote: Quote: > On Nov 1, 12:27*pm, "James R. Gentile" <n...@xxxxxx> wrote: > > > Quote: > > I'm making a script to calculate time for a file to download, I want tobe > > able to input things like "10GB" and "5GB" and "3.3KB" in the following > > script: Quote: > > $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)" > > $dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes > > allowed)" > > $dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)" Quote: > > $time=($totalsize-$dledsize)/$dlspeed/60/60/24 Quote: > > write-host [string]($time) + " days until file is downloaded." Quote: > > but it gives me an error that says: Quote: > > "Method invocation failed because [System.String] doesn't contain a method > > named 'op_Subtraction'. > > At line:1 char:19 > > + $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24" Quote: > > but when I try the script in the shell, I can do things like > > "$totalsize-5GB" and get the correct result. But I can't use two variables > > with the size suffixes. Is this by design or a bug? And is there a (simple) > > way around it? > James, > > Use the Invoke-Expression Cmdlet on each of the input values to > evaluate them to their numerical equivalents: > > $value = Invoke-Expression ( Read-Host "Input file size..." ) > > Jeff before running Invoke-Expression, otherwise a user could enter a command as their input and it would be executed: $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)" if ($totalsize -match '^\d+[GKM]B$') { $totalsize = Invoke-Expression $totalsize } else { Write-Error "I don't think so." } |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| When do I have do define variables with "DIM" ? | VB Script | |||
| Vista Upgrade fails at "Gathering Files" when two users share the same user profile (ProfileImagePath). Message: "the upgrade was cancelled". | Vista installation & setup | |||
| Patch works with "right-click-apply", fails MsiApplyPatch, MsiExec | Vista hardware & devices | |||