Author Posts

August 21, 2018 at 4:28 am

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.

August 21, 2018 at 4:30 am

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

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

August 21, 2018 at 12:31 pm

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.

August 21, 2018 at 4:21 pm

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.

August 21, 2018 at 4:23 pm

js

I'm running PowerShell v6.0.4.

Any idea on a workaround to the problem?

Thanks in advance

August 21, 2018 at 6:42 pm

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

August 21, 2018 at 7:07 pm

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

August 21, 2018 at 7:08 pm

What os are you in?

August 21, 2018 at 7:13 pm

js

It is being run under Windows 7.

August 21, 2018 at 7:18 pm

Are you sure of the pwsh version?

pwsh -version

August 21, 2018 at 7:20 pm

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

August 22, 2018 at 1:57 pm

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'

August 22, 2018 at 2:39 pm

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 '$'.

August 22, 2018 at 5:20 pm

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.

August 22, 2018 at 5:26 pm

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

August 22, 2018 at 5:38 pm

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.

 

August 22, 2018 at 6:00 pm

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 '-','_' }

August 22, 2018 at 6:05 pm

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.

August 22, 2018 at 6:19 pm

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")

August 22, 2018 at 6:37 pm

kvprasoon

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

August 22, 2018 at 6:52 pm

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

August 22, 2018 at 6:54 pm

@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

August 22, 2018 at 7:57 pm

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