Hi,
I have the following script that creates 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 if the folder exists otherwise creates the folder, 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 Write-Host "Number of Slave folders.." -ForegroundColor Yellow $numSlavefolder = Read-Host Write-Host "Number of days logged.." -ForegroundColor Yellow $numDaylogs = Read-Host This is the part that does not work, what is suppose to happen here to that the script asks for the number of subfolders required in the Main Project folder and iterate that many times each time asking the name of the subfolder, checking if the folder exists, if no create. This part partially works as it can iterate as per the number of subfolders required, and ask the names as well, however, when it tries to check if the folder exists it gets stuck in the loop.
For example, once the 2nd subfolder is named as a subfolder that already exists the script shows the error that a folder with the name exists and start the loop from 1 again asking to enter name of subfolder 1 when it should only show to re-enter name of subfolder 2.
do { For($i=1;$i -le $numSlavefolder;$i++) { 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 } } }until (-Not(chkAddress -folderPath $tPath))
Any thought, Thanks…