Test-Path before Subfolders creation..

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.)

Parents
  • I started working through this code, and you really need to rethink this effort. You have several things here that don't make sense (imho), or out of place, of forces way too much user interaction. This can be implemented far easier, using all native PowerShell cmdlets and $env options.

Reply
  • Thanks for taking the time, I would love to know how to easily implement it as I was unable to determine an easier way to do it.

    The idea is simple I just create 2nd level subfolders after the Main Project folder, however, only in the main project and 1st level subfolder it checks if a subfolder user is trying to create already exists or not..

    Thanks a lot..

Children