powershell - Export function -


i trying create function in user has give filename can not containt empty string. besides that, string can not contain dot. when run function, keep looping when enter "test" example. idea why?

please , thank you!

 function export-output{     do{          $exportinvoke = read-host "do want export output new .txt file? [y/n]"       }until($exportinvoke -eq "y" -or "n")              if($exportinvoke -eq "y"){                                      {                    $script:newlog = read-host "please enter filename! (exclude extension)"                        if($script:newlog.length -lt 1 -or $script:newlog -match ".*"){                          write-host "wrong input!" -for red                       }                    }while ($script:newlog.length -lt 1 -or $script:newlog -match ".*")                   ni "$script:workingdirectory\$script:newlog.txt" -type file -value $exportvalue |out-null             }      } 

edit:

on related note:

do{      $exportinvoke = read-host "do want export output new .txt file? [y/n]"   }until($exportinvoke -eq "y" -or "n") 

when use these lines of code can hit enter circumvent read-host. when replace "y" -or "n" "y" not. idea why happening ?

the -match operator checks against regular expression, this:

$script:newlog -match ".*" 

is testing if filename contains charachter except newline (.) 0 or more times (*). condition true, creating infinite loop.

if want test literal dot, must escape it:

$script:newlog -match '\.' 

as other question, you're misunderstanding how logical , comparison operators work. $exportinvoke -eq "y" -or "n" not mean $exportinvoke -eq ("y" -or "n"), i.e. variable equals either "y" or "n". means ($exportinvoke -eq "y") -or ("n"). since expression "n" not evaluate zero, powershell interprets $true, condition becomes ($exportinvoke -eq "y") -or $true, true. need change condition this:

$exportinvoke -eq "y" -or $exportinvoke -eq "n" 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -