Test-Path before Subfolders creation..
Home › Forums › Scripting › PowerShell › Test-Path before Subfolders creation..
- This topic has 2 replies, 2 voices, and was last updated 1 year ago by
TryllZ.
-
AuthorPosts
-
TryllZMemberJan 13, 2020 at 4:24 am #625674Hi,
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 $prjFolderif (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-HostWrite-Host “Number of days logged..” -ForegroundColor Yellow
$numDaylogs = Read-HostFor($i=1;$i -le $numSlavefolder;$i++)
{
do
{
Write-host “Enter Slave $i folder name..” -ForegroundColor Yellow
$slaveFoldername = Read-Host
$jPath = Join-Path $nprjFolder $slaveFoldernameif (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.) -
AuthorPosts
You must be logged in to reply to this topic.