Do while loop

This topic contains 6 replies, has 3 voices, and was last updated by  Mark Hammonds 2 years, 1 month ago.

  • Author
    Posts
  • #31130

    D.J. Lindsey
    Participant

    So I have to come up with a script that will achieve the following. I've got all but the loop any help would be appreciated.
    store the current location (as in C:\scripts)
    test to see if a folder C:\output exists, if it does not, create it
    prompt the visitor for a folder name (no C:\)
    obtain a directory listing of that folder showing all .ps1 files only
    place the results of this listing in a file named PS1_files_[folder name].txt in the c:\output folder
    repeat this task as part of a loop until an empty string is entered
    restore the present location (for example C:\scripts)

    This is what I currently have.
    $FolderName = Read-Host "FolderName"

    cd C:\scripts

    Push-Location

    If (!(Test-Path -Path C:\output))
    {
    md c:\output
    }
    Write-Host $FolderName
    Do {
    dir $FolderName
    If ($FolderName -eq $True)
    }While($FolderName -eq "*")

  • #31131

    Mark Hammonds
    Participant

    I'm confused but if you want to write the results of a search of a folder to a file

    Do this. Keep in mind not tested wrote this on iPad maybe one of the seasoned guys can chime in if I'm wrong.

    Also I would do this
    }While (!($FolderName -eq $null))

    but I think the Logic will result in an endless loop as the value of $FolderName is not being changed after first prompt. I would look into a function for this task.

    Get-childitem -path $foldername -filter "*.ps1" | out-file -path "c:\output\PS1_files_$($foldername).txt"
  • #31132

    Mark Hammonds
    Participant

    I'm probably way off written on iPad I will check when I get to work in the morning.

    
    Function Get-psfiles{
    Param(
    [Parameter(Mandatory=$true)]
    [string]$FolderName
    )
    If(test-path $FolderName){
            Get-childitem -path $FolderName -filter "*.ps1" | out-file -path "C:\output\PS1_files_$($FolderName).txt"
            Get-psfiles
    }else{
              "folder does not exist"
               Break
    }
    }
    
    
  • #31133

    D.J. Lindsey
    Participant

    I see how to crate this loop but I cant get it to ask for input like it does with this.
    $FolderName = Read-Host "FolderName"

    cd C:\scripts

    Push-Location

    If (!(Test-Path -Path C:\output))
    {
    md c:\output
    }
    Write-Host $FolderName

  • #31134

    Mark Hammonds
    Participant

    With a function you either dot source it or run it.

    Get-psfiles -FolderName "c:\path of folder"

    Or if you have ran it once type Get-psfiles in the console and hit enter it should prompt for a folder path.

  • #31135

    Curtis Smith
    Participant

    If you want it to repeatedly ask for input, you need to put your input prompt code inside of the loop.

    Do {
    
        #any additional code you want to run before the prompt every time the loop runs
    
        $FolderName = Read-Host "FolderName"
    
        #any additional code you want to run after the prompt every time the loop runs
    
    } While($FolderName)
    
  • #31145

    Mark Hammonds
    Participant

    I was way off I think I need to keep my mouth shut when I'm on my Ipad. Here is the function that works.

    it will create a file with the appended name like you asked in the folder c:\output in this case the file name was PS1_files_My PowerShell Scripts.txt. Like Curtis said if you want it to continue asking you can put the function in a loop just remove the mandatory field and add error handling for a null value.

    Directory: C:\My PowerShell Scripts

    Mode LastWriteTime Length Name
    —- ————- —— —-

    -a— 10/7/2015 12:02 PM 2587 Agent-Load-Balancing-V1.0.ps1
    -a— 10/7/2015 11:05 AM 1506 Archive-Unsealed-MPs-V1.0.ps1

    Function Get-psfiles{
    Param(
    [Parameter(Mandatory=$true)]
    [string]$FolderName
    )
    if(!(Test-path c:\output)){md c:\output}
    If(test-path $FolderName){
            Get-childitem -path $FolderName -filter "*.ps1" | out-file  "C:\output\PS1_files_$($FolderName.Split("\")[-1]).txt"
    }else{
              "folder does not exist"
    }
    }
    Get-psfiles
    

You must be logged in to reply to this topic.