Windows Vista Forums

Outputting variables & accepting keystrokes without pressing enter
  1. #1


    Kari Guest

    Outputting variables & accepting keystrokes without pressing enter

    Hi!

    no1:

    Let's say that I have two variables:

    $var1 = "variable1"
    $var2 = "variable2"

    I would need to print out the values of these variables so that the output
    would be like:



    variable1_variable2

    If I try output it:
    [PS] E:\>$var1 = "variable1"
    [PS] E:\>$var2 = "variable2"
    [PS] E:\>write-host "$var1_$var2"
    variable2

    .... I can't get both of the values out. I also need to use this to specify
    an output file in cvs:

    [querystuff] | export-csv -path "c:\variables_$var1_$var2"

    I realize the difference between double quotes and single quotes. I just
    don't understand how to combine two variables.

    --------

    no2

    If I'm running some sort of loops and I need to query user for like
    "Press y to continue or n to quit"

    .... how can I do this so that user only needs to choose y and the query
    starts (without pressing enter)?

      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    RE: Outputting variables & accepting keystrokes without pressing enter

    # 1.-
    $var1 = "variable1"
    $var2 = "variable2"

    # the first variable's name is altered and PowerShell tries to expand
    # the variable $var1_, if it does not exist $null is expanded...
    "c:\variables_$var1_$var2"

    # ...if it exists, $var1_ is expanded
    $var1_ = 'oops!'
    "c:\variables_$var1_$var2"

    # to avoid this enclose the variable's name in '{}'
    "c:\variables_${var1}_$var2"

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
    # 2.-
    You can use the [Management.Automation.Host.KeyInfo]'s VirtualKeyCode
    Property to check which key was pressed. It also has a Character Property.
    Label the outer loop, then prompt the user and either continue or exit the
    outer loop.

    :myLoop foreach ($num in 1..10) {
    "`$num = $num"
    do {
    write-host Press y to continue or n to quit
    $action = $host.ui.rawUI.readkey('NoEcho, IncludeKeyDown')
    switch -r ($action.virtualKeyCode) {
    89 { # <-- 'y' or 'Y'
    "`$num * 2 = $($num * 2)`n$('-' * 25)"
    break
    }
    78 { # <-- 'n' or 'N'
    write-host Exiting myLoop
    sleep -m 200
    break myLoop # <-- break out of the Foreach loop, labeled 'myLoop'
    }
    }
    # continue prompting if other keys were pressed
    } while ($action.virtualKeyCode -notMatch '89|78')
    }

    --
    Kiron

      My System SpecsSystem Spec

Outputting variables & accepting keystrokes without pressing enter problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the "RunWorkerCompleted" never comes after pressing ENTER? senglory .NET General 1 04 Aug 2009
Can't open pics by pressing enter or double click esmnh Vista music pictures video 2 05 Apr 2008
Pop-up to enter variables to pass to script Anatoli PowerShell 2 17 Nov 2007
Sundeep Raina:) variables being handled in Workflow and How to declare New Variables raisundeep@gmail.com WinFX General 1 09 Aug 2006
Sundeep Raina(Iopsis): Is variables being handled in Workflow and How to declare New Variables sunny Avalon 0 30 Jun 2006