Jump to content
huud

Subfolders creation after checking existing folders..

Recommended Posts

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 folder already exists and prompts to enter name of the 1st subfolder again looping through this.)

Share this post


Link to post
Share on other sites

@huud not sure why you joined an AutoIt language forum to post a PowerShell question??

I will say at the outset this is not how I would do it. It would be better practice to allow the user to choose the name of the parent and then use some agreed-upon naming convention for the children to keep things consistent. The way you have it, you're really not saving the end user any time from simply right-clicking manually and choosing new folder.

That said, the code below works just fine for me. You should easily be able to capture the while loop into a function to cut down on repetition of lines.

 

$path = [Environment]::GetFolderPath("Desktop")

$newFolderName = Read-Host "Name for new folder"

while (1) {
    try {
        New-Item -Path "$($path)\$($newFolderName)" -ItemType directory -ErrorAction Stop | Out-Null
        break
    } catch {
        $newFolderName = Read-Host "A folder named $($newFolderName) already exists in this directory, please choose another"
    }
}

$numFolders = Read-Host "Number of child folders"
$numDaysLogged = Read-Host "Number of days logged"

foreach ($a in 1..$numFolders) {
    $childFolderName = Read-Host "Name for child folder number $($a)"

    while (1) {
        try {
            New-Item -Path "$($path)\$($newFolderName)\$($childFolderName)" -ItemType directory -ErrorAction Stop | Out-Null
            break
        } catch {
            $childFolderName = Read-Host "A folder named $($childFolderName) already exists in this directory, please choose another"
        }        
    }
}

 

beginning test...

Name for new folder: Testing
A folder named Testing already exists in this directory, please choose another: Testing
A folder named Testing already exists in this directory, please choose another: Testing1
Number of child folders: 10
Number of days logged: 11
Name for child folder: Child1
Name for child folder: CHild1
A folder named CHild1 already exists in this directory, please choose another: Child2
Name for child folder: Child2
A folder named Child2 already exists in this directory, please choose another: Child3
Name for child folder: Child3
A folder named Child3 already exists in this directory, please choose another: Child4
Name for child folder: Child4
A folder named Child4 already exists in this directory, please choose another: Child5
Name for child folder: Child5
A folder named Child5 already exists in this directory, please choose another: Child6
Name for child folder: Child6
A folder named Child6 already exists in this directory, please choose another: Child7
Name for child folder: CHild7
A folder named CHild7 already exists in this directory, please choose another: Child8
Name for child folder: Child8
A folder named Child8 already exists in this directory, please choose another: Child9
Name for child folder: Child10

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...