Switch Statement

Hi,

   I would like to generate switch case based on certain inputs, for example

Switch (@options)

1{i would like to generate another switch statement here based on the number of files i have in the folder in which this script is present}

2{}

Is this possible?

July 17th, 2015 7:50am

Hi,

Yes, this is possible. Here's a quick example:

$ans = Read-Host 'Check files? (y/n)'

switch ($ans) {

    'y' {

            $fileCount = Get-ChildItem .\

            switch ($fileCount.Count) {

                0 { Write-Host 'Zero files found' -ForegroundColor Red }
                1 { Write-Host 'Only 1 file found' -ForegroundColor Red }
                default { Write-Host 'More than 1 file found' -ForegroundColor Green }

            }

        }

    default {

        Write-Host 'Done' -ForegroundColor Red

    }

}

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 8:25am

i want 

 0 { Write-Host 'Zero files found' -ForegroundColor Red }
                1 { Write-Host 'Only 1 file found' -ForegroundColor Red }
                default { Write-Host 'More than 1 file found' -ForegroundColor Green }

this to be dynamic, the file count will vary and that means the number of cases will also vary...

July 17th, 2015 8:30am

What is the purpose of doing this?  It doesn't make much sense as described.

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 8:41am

That's an entirely different question, but you can use a range:

switch ($fileCount.Count) {

    0 { Write-Host 'Zero files found' -ForegroundColor Red }
    1 { Write-Host 'Only 1 file found' -ForegroundColor Red }
    {2..5} { Write-Host 'Between 2 and 5 files found' -ForegroundColor DarkRed }
    default { Write-Host 'More than 5 files found' -ForegroundColor Green }

}

Or you can really do whatever you want.

July 17th, 2015 8:44am

ok here is the scenario

i have a folder which right now has 3 files, i need to create tasks with these files , i have achieved it . there are certain scenario's where i need to give only one file or more than 3 files and i need to change my code often just to accomadate these changes. what i am planning is to give user the choice of choosing the file they want to create task, there is a team who will just copy the file and i want my script to pick that and present that as a choice to the user ...

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 8:56am

Display numbered list and ask for file number:

$files=dir c:\folder\*.txt

$filenum=Read-Host 'Enter file number'

$file=$files[$filenum]

July 17th, 2015 9:01am

How about something like this?

Get-ChildItem -File | 
    Out-GridView -OutputMode Single -Title 'Select File' | 
        ForEach-Object {
            Write-Host Create task with file: $_.Name
        }

  • Proposed as answer by jrv 18 hours 41 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 9:09am

Create a numbered list:

$i=;dir *.txt|%{'      {0}. {1}' -f $i++,$_.Name}

July 17th, 2015 9:09am

hmm, the problem here is i want to take that out and put that in  a switch and give that to user to choose ...
Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 9:22am

hmm, the problem here is i want to take that out and put that in  a switch and give that to user to choose ...

There is really no technical reason for that choice.  It is not how an experienced coder would approach the problem.

Solve the requirement without attempting to reinvent the wheel.

Remember that the result of a switch statement is the stuff in {}.

$result=switch($x){
      1 {'suff1'}
      2 {'stuff 2'}
}

The outcome is the same as if you subscripted an array.

July 17th, 2015 9:30am

PS C:\scripts> $swisher=@'
>>       switch ($x){
>>          1 {'stuff1'}
>>          2 {"stuff 2"}
>>      }
>> '@
>>
PS C:\scripts> [scriptblock]::Create($swisher)
      switch ($x){
         1 {'stuff1'}
         2 {"stuff 2"}
     }
PS C:\scripts> $switch=[scriptblock]::Create($swisher)
PS C:\scripts> $switch.Invoke()
PS C:\scripts> $x=2
PS C:\scripts> $switch.Invoke()
stuff 2
PS C:\scripts>

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 9:35am

Forget the switch statement for a moment. Please try to explain in more detail what you want to achieve. What exactly do you want to present to your users, and what should happen when they choose whatever they choose? What do you mean by "create tasks with these files"?
July 17th, 2015 10:52am

i know that the result of a switch statement is the stuff in {}, i want to generate kind of dynamic switch case based on the result i.e number of files

For example:

lets say i have 5 files in the folder with static switch i can tell them choose any one of the 5 by giving option 1 2  3 4 5

as i told before, whenever the number of files change i need to edit my script to give that to user, i want to make that dynamic, i.e take the file name from the folder show that to user one by one and ask them to choose one , when they choose it should create a task which is available under that dynamic switch


Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 10:52am

OMG!! thats internal project and it should not be revealed outside lol!!!!
July 17th, 2015 11:02am

Out-GridView sounds like the perfect tool for the job. Why were you unable to use it? 

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 1:16pm

That's an entirely different question, but you can use a range:

switch ($fileCount.Count) {

    0 { Write-Host 'Zero files found' -ForegroundColor Red }
    1 { Write-Host 'Only 1 file found' -ForegroundColor Red }
    {2..5} { Write-Host 'Between 2 and 5 files found' -ForegroundColor DarkRed }
    default { Write-Host 'More than 5 files found' -ForegroundColor Green }

}

Or you can really do whatever you want.

July 18th, 2015 12:27am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics