powershell

#To set the directory

$Path = Set-Location C:\
$RSG  = Read-Host "Please provide the RSG main folder name"
$RSGUpper = $RSG.toupper()
$RSGFolder= New-Item -Path "$(Get-location)" -Name $RSGUpper -Type Directory -ErrorAction SilentlyContinue
Write-Host "Your RSG Main folder : $RSGUpper"

If ($RSGFolder -eq (Get-ChildItem -Path "$(Get-location)").name){
do{

$RSG = Read-Host "Your RSG Man folder already exist, please provide the new name"
$RSGUpper = $RSG.toupper()

#$RSGFolder= New-Item -Path "$(Get-location)" -Name $RSGUpper -Type Directory

}
While ($RSGFolder -ne (Get-ChildItem -Path "$(Get-location)").name)

$RSGFolder= New-Item -Path "$(Get-location)" -Name $RSGUpper -Type Directory

Open in new window


I have this scripts but it doesn't work what i want to be. What i want is:

1. If i create a folder which does exist in C:\, it will prompt to re-create the folder.

I had tested above script, when i create a folder which already exist c:\, system will prompt for me re-create, when i put the another name (which not exist in C:\) , system still asking me to put the another name.

Could you give me 2 options:

1. to able to create a new folder if the original folder meet the If function
2. To overwrite existing same folder name if the original folder meet the if function
LVL 19
suriyaehnopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

becraigCommented:
Here is a rough idea, not the cleanest but it works for me.

#To set the directory
$Path = Set-Location D:\
$RSG  = Read-Host "Please provide the RSG main folder name"
$RSGUpper = $RSG.toupper()
$fullpath = $Path +  $RSGUpper
while (Test-Path $fullpath)
{
$RSG = Read-Host "Your RSG Man folder already exist, press Y to overwrite N to create a new directory"
switch($RSG)
{
"Y" {Write-Host "Overwriting" $fullpath"; New-Item -Path $fullpath -Type Directory  -Force -ErrorAction SilentlyContinue; exit }
"N" {$RSG = Read-Host "Enter New Directory Name";$RSGUpper = $RSG.toupper();$fullpath = $Path +  $RSGUpper}
}

}
if (!(Test-Path $fullpath))
{
New-Item -Path $fullpath -Type Directory -ErrorAction SilentlyContinue
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J-LCommented:
Isn't this just an issue of comparing apples & oranges?

While ($RSGFolder -ne (Get-ChildItem -Path "$(Get-location)").name)

Open in new window

$RSGFolder is an object, as returned by New-Item.  The right part of your equasion is a string.

Do you have the issue when using this code?
While ($RSGFolder.Name -ne (Get-ChildItem -Path "$(Get-location)").name)

Open in new window

0
J-LCommented:
I've had a second look at the script, and I feel you're making things too difficult for yourself.  Try this code instead, which will keep on prompting you for a new folder name if it already exists, and has a Cancel-handler:
$Path = "C:\"
$RSG  = (Read-Host "Please provide the RSG main folder name").ToUpper()

while ((Test-Path (Join-Path $path $RSG)) -and ($RSG -ne $null)) {
    $RSG = (Read-Host "The folder $(Join-Path $path $RSG) already exist. Please provide a new name or cancel.").ToUpper()
} 

if ($RSG -ne $null) {
    Write-Host "I'm now creating your RSG main folder $RSG."
    try   { $RSGFolder= New-Item -Path $Path -Name $RSG -Type Directory -ErrorAction stop }
    catch { Write-Host "Unfortunately, I couldn't create $(Join-Path $path $RSG)." }
} else {
    Write-Host "You pressed cancel, so I'm quitting."
}

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

becraigCommented:
I might have this wrong but I read the request as:

1. to able to create a new folder if the original folder meet the If function
The while statement I created continues to loop if the folder already exists
while (Test-Path $fullpath)

Open in new window


2. To overwrite existing same folder name if the original folder meet the if function
I added a switch for Yes or No (Overwrite or try a new name)
"Y" {Write-Host "Overwriting" $fullpath"; New-Item -Path $fullpath -Type Directory  -Force -ErrorAction SilentlyContinue; exit }
"N" {$RSG = Read-Host "Enter New Directory Name";$RSGUpper = $RSG.toupper();$fullpath = $Path +  $RSGUpper}

So if I am reading correctly the request was to either get to the point of having a new name then create  (which my script does)  or overwrite the current folder which my script also does. 

Open in new window

0
suriyaehnopAuthor Commented:
J-L,

if i used

While ($RSGFolder.Name -ne (Get-ChildItem -Path "$(Get-location)").name)

Open in new window


1st attempt when I re-create the exisiting folder RSG123, it will prompt that Your RSG Man folder already exist, please provide the new name, then i input the another folder name which also already exist, the cmdlet will terminated.

New-Item : Item with specified name C:\RSG234 already exists.
At line:20 char:21
+ $RSGFolder= New-Item <<<<  -Path "$(Get-location)" -Name $RSGUpper -Type Directory
    + CategoryInfo          : ResourceExists: (C:\RSG234:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand


becraig,

Your script work. do "$fullpath = $Path +  $RSGUpper" will verify with while (Test-Path $fullpath)? If is true, it will prompt again?

What does this mean if (!(Test-Path $fullpath))
0
becraigCommented:
That means if the path does not exist.
If (Test-path). Means if a path exists.  

(!(test-path)). Means if it does not.  

So if there is no folder it proceeds to create it.
0
J-LCommented:
suriyaehnop, I'm confused.  The code works when I run it on my box.

But the error you post is when you run your code, right?
0
becraigCommented:
So the concept behind what I wrote is based on what you requested with just an added function of entering a new folder name if the first one entered is invalid:


The piece below simply checks your input value and verifies if a folder already exists,
If the folder name you entered already exists you get one of two options:
If you enter Y that overwrites the folder as you requested.


The added piece is, if you hit n you get to enter a new name and try again before you exist (Which kinda makes sense)


while (Test-Path $fullpath)
{
$RSG = Read-Host "Your RSG Man folder already exist, press Y to overwrite N to create a new directory"
switch($RSG)
{
"Y" {Write-Host "Overwriting" $fullpath"; New-Item -Path $fullpath -Type Directory  -Force -ErrorAction SilentlyContinue; exit }
"N" {$RSG = Read-Host "Enter New Directory Name";$RSGUpper = $RSG.toupper();$fullpath = $Path +  $RSGUpper}
}

Open in new window


The piece below executes if the name you entered is NOT already a folder then it simply creates the new directory.

if (!(Test-Path $fullpath))
{
New-Item -Path $fullpath -Type Directory -ErrorAction SilentlyContinue
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.