locked
Subfolders creation after checking existing folders.. RRS feed

  • Question

  • Hi,

    I have the following script that is suppose to create a Main Project folder taking name of this folder from user input, checks 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 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.

    What happens is if I use Do While loop, if the folder already exists it keeps on showing the error folder already exists in a loop. If I use Do Until then it only shows error once that the folder exists and finishes. If I use Do Until then it only shows error once that the folder exists and finishes.

    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)
    }
    
    Write-host "Folders created successfully, folder hierarchy can be seen below.."

    Thanks..

    • Edited by tryllzhuud Sunday, January 12, 2020 6:04 PM
    Sunday, January 12, 2020 4:03 PM

All replies

  • What do you mean by an "alternate name". Are you asking how to rename a folder or how to create another folder with a different name?

    Please clarify your request.


    \_(ツ)_/

    Sunday, January 12, 2020 5:39 PM
  • What do you mean by an "alternate name". Are you asking how to rename a folder or how to create another folder with a different name?

    Please clarify your request.


    \_(ツ)_/

    Create another folder with a different name.

    The message simply asks the user to change the name of the folder that he needs to create, as a folder with the name he is trying to create already exists.

    • Edited by tryllzhuud Sunday, January 12, 2020 5:48 PM
    Sunday, January 12, 2020 5:47 PM
  • And why can't you do that. Just check if the name exists and prompt the user for a new name.


    \_(ツ)_/

    Sunday, January 12, 2020 6:10 PM
  • And why can't you do that. Just check if the name exists and prompt the user for a new name.


    \_(ツ)_/

    I can check the name and let the user know that a folder exists, after that it goes into an endless loop.

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

    Sunday, January 12, 2020 6:31 PM
  • To check a folder or path use Test-Path

    if(Test-Path $folderpath){
           #folder exists
    }else{
    
          # folder does not exist.
    }

    To check in a loop place the test at the end of the loop.  Negate the test to end when the folder is NOT found.

    do{
         # get name
    }
    until(-Not (Test-Path $folderpath))


    \_(ツ)_/


    Sunday, January 12, 2020 7:15 PM
  • Thanks for clarifying that..
    Sunday, January 12, 2020 7:32 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, January 15, 2020 8:47 AM