Error on Path with spaces, commas, parenthesis and slash

Welcome Forums General PowerShell Q&A Error on Path with spaces, commas, parenthesis and slash

Viewing 2 reply threads
  • Author
    Posts
    • #237409
      Participant
      Topics: 3
      Replies: 15
      Points: 50
      Rank: Member

      Errors when running code below, how do I bypass spaces, commas, parenthesis and slashes that is comes across in the Paths?

      $StartLevel = 2 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
      $Depth = 20 # How many levels deep to scan
      $Path = “.” # starting path

      $folders = For ($i=$StartLevel; $i -le $Depth; $i++) {
      $Levels = “\*” * $i
      (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
      Select FullName
      }

      $Folders.fullname | %{ Get-ChildItem -path $_ -Recurse | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Format-Table -Property LastWriteTime,FullName -Autosize | Out-File -Append C:\Users\mmsho\Documents\Testoutput.txt -Encoding UTF8
      }

    • #237418
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      Michelle, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      What should be the result of this code?

      Regardless of that: format cmdlets should (almost) always be the last element in a pipeline. It does not make sense to pipe the output of Format-Table to Out-File. A suitable option would be to use Export-Csv.

    • #237424
      Participant
      Topics: 6
      Replies: 17
      Points: 132
      Helping Hand
      Rank: Participant

      Any reason why you can’t do the following?:

      $Path = '.'
      $Depth = 20
      $ExportFolder = "c:\users\mmsho\documents"
      
      Get-ChildItem -Path $Path -Directory -Recurse -Depth $Depth | 
          select LastWriteTime,Fullname | export-csv "$ExportFolder\testoutput.csv"

      CSV is a cleaner export than a foreach-FormatTable.

      But to answer your question, it’s because (Resolve-Path $Path$Level).Provide.... should be (Resolve-Path "$Path$Levels").Provide...

Viewing 2 reply threads
  • You must be logged in to reply to this topic.