Problem with Powershell script using a path with square bracket containing a dash

Welcome Forums General PowerShell Q&A Problem with Powershell script using a path with square bracket containing a dash

This topic contains 22 replies, has 5 voices, and was last updated by

js
 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #109126

    Participant
    Points: 0
    Rank: Member

    I have a batch file that calls a script and in the script it has a command similar to this that reports an error.

    This is an example of one portion of the script that is giving me trouble.

    $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
    $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
    $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
    

    When run as a script from a batch command via the powershell -File scriptname.ps1 I get the following error

    The specified wildcard pattern is not valid: [Sun-King] Master - Photo Set #1
        + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : RuntimeException
    

    Any help would be appreciated.

  • #109136

    Moderator
    Points: 49
    Team Member
    Rank: Member

    You might have to escape special characters in your path, such as the pound/hash # sign

    https://ss64.com/ps/syntax-esc.html

  • #109153
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    There's some kind of a bug in ps5 with literalpath and wildcards.
    You might have to pipe to where-object.

    https://social.technet.microsoft.com/Forums/windows/en-US/3a7c057f-babf-4afe-b014-c7ed55b119f9/getchilditem-files-includes-literalpath-and-square-brackets?forum=winserverpowershell

    For example, this lists files or folders starting with a or b:

    get-item [a-b]*

    -include, -exclude, -literalpath, and -filter, all have problems in ps5.

    • #109211

      Participant
      Points: 0
      Rank: Member

      js

      I'm running PowerShell v6.0.4.

      Any idea on a workaround to the problem?

      Thanks in advance

  • #109210

    Participant
    Points: 0
    Rank: Member

    AlexW

    The escaping issue is between the square brackets, the pound sign is okay.

    if I change the directory and file name

    $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
    

    to

    $(Get-Item -LiteralPath 'C:\A\[Sun King] Master - Photo Set #1\[Sun King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
    

    and then run the ps1 file with the command

    pwsh -f filelist.ps1
    

    it works fine but I have far too many directories (including some that are archived) to replace each and every occurrence of the dash between the square brackets.

  • #109475
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    Actually, it works for me in PS 5.1.16299.611 and 6.1.0-preview.2. I'm getting confused.

    • #109480

      Participant
      Points: 0
      Rank: Member

      Sample directory of images

      C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg
      C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg
      C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg
      

      contents of script named filelist.ps1

      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_002.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').LastAccessTime=$(Get-Date "2/16/2017 4:32:02 PM")
      $(Get-Item -LiteralPath 'C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_003.jpg').LastWriteTime=$(Get-Date "2/16/2017 4:32:02 PM")
      

      run at windows command line

      pwsh -f filelist.ps1
      

      Results in this error

      Tue 08/21/2018 15:05:24.71 C:\A\[Sun-King] Master - Photo Set #1>pwsh -f filelist.ps1
      The specified wildcard character pattern is not valid: [Sun-King] Master - Photo Set #1
      + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : RuntimeException
      
  • #109483
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    What os are you in?

    • #109486

      Participant
      Points: 0
      Rank: Member

      js

      It is being run under Windows 7.

  • #109489
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    Are you sure of the pwsh version?

    pwsh -version
    
    • #109490

      Participant
      Points: 0
      Rank: Member

      Tue 08/21/2018 15:05:42.95 C:\A\[Sun-King] Master – Photo Set #1>pwsh -version
      PowerShell v6.0.4

  • #109547

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    I'm not sure why the -LiteralPath or even using single qoutes is attempting to parse the path as a wildcard, but maybe you can try a different method. It looks like you are just getting JPG's from a dir, so can see if this works:

    $files = Get-ChildItem -Path 'C:\A\[Sun-King] Master - Photo Set #1\*.jpg'
    
    $date = Get-Date "2/16/2017 4:32:02 PM"
    foreach ($file in $files) {
        $file.CreationTime=$date
        $file.LastAccessTime=$date
    }
    

    If it does not, you will need to escape the characters, like the square brackets. The square brackets are most likely the issue as they are regex meta characters. So, you may need to make the path like so:

    'C:\A\`[Sun-King`] Master - Photo Set #1\*.jpg'
    
    • #109585

      Participant
      Points: 0
      Rank: Member

      Rob & js

      Tried both ideas and still same issue.  I'm at a loss.  I'm going to maybe see if I can do this via a cygwin session as a workaround for directories with a dash between square brackets.

      And it is the dash that is mucking things up.  If I copy the directory and contents and rename said directory and contents with an underscore, space, etc it works flawlessly.  Unfortunately renaming every single file/directory currently on disk and the ones archived is not feasible.

  • #109556
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    Strange. I tried powershell core in win7 sp1 64-bit and had no errors. I guess you can try using '?' instead of brackets and -path instead of -literalpath. You don't really need either '$'.

  • #109588
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    How about a ? instead of the dash? Are you sure it's not an emdash or something?

    • #109594

      Participant
      Points: 0
      Rank: Member

      js

      Yes I tried the ? in place of the – and no change (and yes I'm sure it is a dash and not an em dash).  Just tried it with an em dash (alt 0151) in place of the – and it worked fine...it is just the dash that breaks it.

       

  • #109601
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    You can rename like this. Since -newname can get input from the pipeline, it can take a script block:

    get-childitem | rename-item -newname  { $_.name -replace '-','_' }
    • #109603

      Participant
      Points: 0
      Rank: Member

      js

      Unfortunately, like I said, renaming is not an option as it breaks with the archived files.  I'll just have to keep digging and seeing what I can come up with.

  • #109604

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    You can try replace instead of rename to escape the rgex .

    $(Get-Item -LiteralPath (('C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg' -replace '\]','`]') -replace '\[','`[')).CreationTime=$(Get-Date "2/16/2017 4:32:02 PM")
    
    • #109607

      Participant
      Points: 0
      Rank: Member

      kvprasoon

      Doesn't help the situation.  The dash in the Sun-King is the problem.  It is not the square brackets.

  • #109615

    Participant
    Points: 0
    Rank: Member

    I've found a utility that I can use to handle my needs called FileTouch.  Have to give up the idea of PowerShell.  I've lost too many days.  Thanks for the help but alas there is no solution to the PowerShell wildcard issue

  • #109616

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    @Gary, I can't repro this issue in 6.0.4 in Win 10.

    Name                           Value
    ----                           -----
    PSVersion                      6.0.4
    PSEdition                      Core
    GitCommitId                    v6.0.4
    OS                             Microsoft Windows 10.0.16299
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    
    [34]KVP> $(Get-Item -LiteralPath (('C:\A\[Sun-King] Master - Photo Set #1\[Sun-King] Master - Photo Set #1_001.jpg')))
    
    
        Directory: C:\A\[Sun-King] Master - Photo Set #1
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----        8/23/2018  12:22 AM              0 [Sun-King] Master - Photo Set #1_001.jpg
    
  • #109621
    js

    Participant
    Points: 202
    Helping Hand
    Rank: Participant

    Is there something unusual about the language or the file system?

You must be logged in to reply to this topic.