Welcome Forums General PowerShell Q&A Script Help

Viewing 12 reply threads
  • Author
    Posts
    • #165952
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

      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
      
      }
      }
      
    • #165958
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

      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
      }
    • #165970
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

      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

      I don’t want it to be hard coded into the code, rather a user enter a string ?

      • #165979
        Participant
        Topics: 4
        Replies: 54
        Points: 406
        Helping Hand
        Rank: Contributor
        
        $Items = Get-ChildItem -Depth 2
        $RootPath = Get-ChildItem
        $folderName = Read-Host “Enter Folder to Find”
        $targetFolder = Read-Host “Enter Target Path”
        
        

         

        Youre $items isnt filled correctly

        it should be more like this

        
        $Items = Get-ChildItem $foldername -Depth 2
        
        

        otherwise your item wont be filled correctly

    • #165973
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

      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.

    • #165976
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

      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.

      I have to hard code a folder, then prompt a user for a folder ?

    • #165982
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

      I have to hard code a folder, then prompt a user for a folder ?

      If you have a time machine – then yes – that’s the right order. 😉 😀

      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.

    • #165988
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

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

      • #166003
        Participant
        Topics: 2
        Replies: 1752
        Points: 3,621
        Helping Hand
        Rank: Community Hero

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

        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.

    • #165991
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member
      PowerShell
      7 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
      5
      6
      7
      $Items = Get-ChildItem Depth 2
      $RootPath = Get-ChildItem
      $folderName = Read-Host “Enter Folder to Find”
      $targetFolder = Read-Host “Enter Target Path”
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Youre $items isnt filled correctly

      it should be more like this

      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
      $Items = Get-ChildItem $foldername Depth 2
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      otherwise your item wont be filled correctly

      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.

    • #166006
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

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

      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.

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

      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 ?

    • #166054
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

      That is what I did,

      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.

      Is it required to hard-code ?

      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.

    • #166325
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member
      $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
      
      }
      }

      @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 ?

    • #166340
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

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

      … and asks for a $targetFolder …

      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.

    • #166343
      Participant
      Topics: 6
      Replies: 22
      Points: 78
      Rank: Member

      @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 ?

      • #166349
        Participant
        Topics: 2
        Replies: 1752
        Points: 3,621
        Helping Hand
        Rank: Community Hero

        but I don’t want to create it before running the script; I want the script to create it for me.

        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.

Viewing 12 reply threads
  • The topic ‘Script Help’ is closed to new replies.