Test-Path before Subfolders creation..

Home Forums Scripting PowerShell Test-Path before Subfolders creation..

This topic contains 2 replies, has 2 voices, and was last updated by Avatar TryllZ 1 week, 3 days ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    TryllZ
    Participant
    #625674

    Hi,

    I have the following script that creates a Main Project folder taking name of this folder from user input, checks and shows error if the folder exists and asks user to change the folder name, this part works.

    $path = [Environment]::GetFolderPath(“Desktop”)

    Function chkAddress($folderPath)
    {
    Test-Path $folderPath -PathType Container
    return
    }

    do
    {
    Write-Host “Name of Project folder..” -ForegroundColor Yellow
    $prjFolder = Read-Host
    $tPath = Join-Path $path $prjFolder

    if (chkAddress -folderPath $tPath)
    {
    Write-host “A folder with the name $prjFolder already exists, choose a different name..” -ForegroundColor Red
    }
    }
    While (chkAddress -folderPath $tPath)

    $nprjFolder = New-item (Join-Path $path $prjFolder) -itemtype directory -Force
    Then it asks for number of subfolders required in the Main project folder, and iterates as many subfolders required asking each subfolder’s name (till here it works). Then it is suppose to check each subfolder’s name and tell the user if folder already exists and once user changes the name, it should finish the application, however, this does not happen.

    Write-Host “Number of Slave folders..” -ForegroundColor Yellow
    $numSlavefolder = Read-Host

    Write-Host “Number of days logged..” -ForegroundColor Yellow
    $numDaylogs = Read-Host

    For($i=1;$i -le $numSlavefolder;$i++)
    {
    do
    {
    Write-host “Enter Slave $i folder name..” -ForegroundColor Yellow
    $slaveFoldername = Read-Host
    $jPath = Join-Path $nprjFolder $slaveFoldername

    if (chkAddress -folderPath $jPath)
    {
    Write-host “A folder with the name $slaveFoldername already exists, choose a different name..” -ForegroundColor Red
    }
    else
    {
    $slaveFolder = New-Item (Join-Path $nprjFolder.FullName $slaveFoldername) -itemtype directory -Force -ErrorAction SilentlyContinue
    }
    }
    while (chkAddress -folderPath $jPath)
    }
    What happens is if the folder already exists it keeps on showing the error folder already and keeps on asking names of the subfolders from the start (for example if the user required 2 subfolders, it would ask 1st subfolder name, check if it exists, if not create the subfolder, then it asks for the 2nd subfolder, checks if the subfolder exists, if subfolder exists it shows the error sbfolder already exists and prompts to enter name of the 1st subfolder again looping through this.)

    Rems
    Rems
    Moderator
    #625731

    Hi,
    The reason why $i does not and cannot add up is that a “Do While” loop statement runs while a logical expression is true. Now at the last line (while ( chkAddress -folderPath $ jPath) ) here the chkAddress routine is performed again even after the folder has just been successfully created. So “True” will be returned in all conditions and the DO-While iteration endlessly continues and the For loop does not count up.

    A tip about the function – The script relies on the return code of the routine, and it should return either True or False. To ensure a True or False based on the folder check is returned you can do the following:

    Function chkAddress($folderPath)
    $chk = Test-Path $folderPath -PathType Container
    return $chk

    In addition,
    – Ensure the folder name check is only done before the creation of the folder.
    – Create a variable to capture the return code: $fldrExist = chkAddress $jPath >>> Use here: While ($fldrExist).

    By controlling the return code of the function like done here above, you also get the opportunity to do even more in the same function. Like you can have the message about the folder name already exists send from within the function, which saves on the use of multiple similar lines in the script.

     

    Below an example (it might inspire some),

    $ParentFolder = [Environment]::GetFolderPath(“Desktop”)

    Function chkAddress($a, $b, $c)
    {
    $d = Test-Path (Join-Path $b $c) -PathType container
    if ($d)
    {
    Write-host “A $a with the name ‘$c’ already exists, please choose a different name.” -ForegroundColor Red
    }
    return $d
    }

    Clear-Host ; Write-Host
    $ft = “Project folder”

    Do
    {
    Write-Host -NoNewLine “Enter a name for the ” -ForegroundColor Yellow
    Write-Host -NoNewLine $ft -ForegroundColor Yellow -BackgroundColor Red
    Write-Host -NoNewLine “: ” -ForegroundColor Yellow
    $fldrName = Read-Host
    $fldrExist = chkAddress $ft $ParentFolder $fldrName
    }
    While ($fldrExist)

    $prjFolder = Join-Path $ParentFolder $fldrName

    Write-Host
    Write-Host -NoNewLine “Enter the ” -ForegroundColor Yellow
    Write-Host -NoNewLine “Number” -ForegroundColor Yellow -BackgroundColor Red
    Write-Host -NoNewLine ” of Slave folders [0-9]: ” -ForegroundColor Yellow
    [int]$numSlavefolder = Read-Host

    Write-Host -NoNewLine “Number of days logged [1-180]: ” -ForegroundColor Yellow
    [int]$numDaylogs = Read-Host
    Write-Host

    if ($numSlavefolder -lt 1)
    {
    New-item ($prjFolder) -itemtype directory -Force | Out-Null
    }
    else
    {
    For ($i=1; $i -le $numSlavefolder; $i++)
    {
    $ft = “$(‘Slave folder’) $($i)”

    Do
    {
    Write-host -NoNewLine “Enter $ft name: ” -ForegroundColor Yellow
    $fldrName = Read-Host
    $fldrExist = chkAddress $ft $prjFolder $fldrName
    }
    While ($fldrExist)

    New-Item (Join-Path $prjFolder $fldrName) -itemtype directory -Force -ErrorAction SilentlyContinue | Out-Null
    }
    }

    Avatar
    TryllZ
    Participant
    #625732

    Hi Rems,

    Thanks a lot for clarifying that up, appreciate that.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.