Script Help

Two problems with this script, Target Path and the symbolic link is not being created ?

$Items = Get-ChildItem -Depth 2
$RootPath = Get-ChildItem
$folderName = Read-Host "Enter Folder to Find"
$targetFolder = Read-Host "Enter Target Path"

#$Symbolics = @()
ForEach ($Item In $Items)
{

If ($Item -eq $folderName)
{
Get-ChildItem -Exclude $folderName
New-Item -ItemType "SymbolicLink" -Path Split-Path $RootPath -Parent -Target $targetFolder

}
}

Please format your code as code. Read the information I linked for you in the other thread.

If the target folder does not exist you have to create it first.
Example:

$Path = ‘C:\Test’
if (-not (Test-Path -Path $Path)) {
New-Item -Path $Path -ItemType Directory
}

[quote quote=165958]Please format your code as code. Read the information I linked for you in the other thread.

If the target folder does not exist you have to create it first.

Example:

PowerShell
4 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
$Path = 'C:\Test'
if (-not (Test-Path -Path $Path)) {
New-Item -Path $Path -ItemType Directory
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] I don't want it to be hard coded into the code, rather a user enter a string ?

The point is you have to create a non existent folder before you copy something to it!! How you provide the name is totally up to you.

[quote quote=165973]The point is you have to create a non existent folder before you copy something to it!! How you provide the name is totally up to you.

[/quote]
I have to hard code a folder, then prompt a user for a folder ?

[pre]

$Items = Get-ChildItem -Depth 2
$RootPath = Get-ChildItem
$folderName = Read-Host “Enter Folder to Find”
$targetFolder = Read-Host “Enter Target Path”

[/pre]

 

Youre $items isnt filled correctly

it should be more like this

[pre]

$Items = Get-ChildItem $foldername -Depth 2

[/pre]

otherwise your item wont be filled correctly

If you have a time machine - then yes - that’s the right order. :wink: :smiley:

If you’re really willing to depend on the unchecked input of the user you should prompt for the name and then create the folder with the desired name.

[quote quote=165982][/quote]

If you are talking about the target folder, I did that here; $targetFolder = Read-Host “Enter Target Path” ?

Don’t I want to go two levels deep from the parent, then search for the $foldername ? I think I see what you mean, go to the $foldername which is two levels deep.

So … you take this $targetFolder variable and create a new folder from it. Then you’re able to copy files to this newly created forlder.

That is what I did, but your code hard-coded it into the file here; $Path = ‘C:\Test’

Is it required to hard-code ?

No! You did not! You requested the input from the user with Read-Host and you tried to use the provided input in your loop but you did not create the target folder.

No. It is not required to hard code the target folder.

It is required that you create the target folder BEFORE you try to copy some files to it.

[pre]$RootPath = Get-ChildItem
$folderName = Read-Host “Enter Folder to Find”
$targetFolder = Read-Host “Enter Target Path”
$Items = Get-ChildItem $foldername -Depth 2

#$Symbolics = @()
ForEach ($Item In $Items)
{

If ($Item -eq $folderName)
{
Get-ChildItem -Exclude $folderName
New-Item -ItemType “SymbolicLink” -Path Split-Path $RootPath -Target $targetFolder

}
}[/pre]

@Olaf, I hope the following changes reflect what you mean, if not; could you please explain ?

Currently the code asks for a folder name as in $folderName, and asks for a $targetFolder but the symbolic link is not being created from the parent folder, excluding the folder name as in the $folderName variable ?

That will be my last answer for you. I don’t know how to explain it for you that you can understand it.

Does this target folder already exist?? If your answer is “NO” - YOU WILL HAVE TO CREATE THIS RARGET FOLDER FIRST … before you try to create a new file in it.

@Olaf - Hold on a second, you tell me, that I must literally create the target folder, but I don’t want to create it before running the script; I want the script to create it for me.

Is that not what you are saying ?

But you have to code this. Powershell is not magic … it does what you tell it to do.

I think you should make a big step back and start with learning the very basics of Powershell first. That will save you from a lot of frustration and wasted time.