Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - filenames with [ characters

Reply
 
Old 12-05-2007   #1 (permalink)
Umesh Thakur


 
 

filenames with [ characters

I have noticed that filenames that include [ or ] characters, powershell (V2
CTP) is not able
to read them using get-content command. I also tried to rename such files
manually as well using powershell scripts, but they throw errors saying file
does not exists.

PS C:\scripts\test> Set-Content test[.txt -value "This is test"
PS C:\scripts\test> dir

it shows nothing (not creating the file at all)

if there is a file that includes bracets, created elsewhere, say
"test[1].txt" you wont be able to read that using powershell.

PS C:\scripts\test> notepad "test[1].txt"
PS C:\scripts\test> Get-ChildItem 'test`[1`].txt'
Get-ChildItem : Cannot find path 'C:\scripts\test\test`[1`].txt' because it
does not exist.
At line:1 char:14
+ Get-ChildItem <<<< 'test`[1`].txt'
PS C:\scripts\test> Get-ChildItem 'test[1].txt'
PS C:\scripts\test> Get-ChildItem test[1].txt
PS C:\scripts\test>
PS C:\scripts\test> type test[1].txt

even 'type' command is also unable to show the contents of this file from
powershell shell. if you try from DOS prompt, it works just fine.

I am not sure how do I access such files in powershell. any ideas?

regards,
Umesh

"Old programmers never die. They just terminate and stay resident."


My System SpecsSystem Spec
Old 12-05-2007   #2 (permalink)
Shay Levi


 
 

Re: filenames with [ characters

It fails because square brackets are considered as metacharacters, try:

Get-ChildItem test````[1````].txt

-or-

Get-ChildItem test````[*



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I have noticed that filenames that include [ or ] characters,
> powershell (V2
> CTP) is not able
> to read them using get-content command. I also tried to rename such
> files
> manually as well using powershell scripts, but they throw errors
> saying file
> does not exists.
> PS C:\scripts\test> Set-Content test[.txt -value "This is test" PS
> C:\scripts\test> dir
>
> it shows nothing (not creating the file at all)
>
> if there is a file that includes bracets, created elsewhere, say
> "test[1].txt" you wont be able to read that using powershell.
>
> PS C:\scripts\test> notepad "test[1].txt"
> PS C:\scripts\test> Get-ChildItem 'test`[1`].txt'
> Get-ChildItem : Cannot find path 'C:\scripts\test\test`[1`].txt'
> because it
> does not exist.
> At line:1 char:14
> + Get-ChildItem <<<< 'test`[1`].txt'
> PS C:\scripts\test> Get-ChildItem 'test[1].txt'
> PS C:\scripts\test> Get-ChildItem test[1].txt
> PS C:\scripts\test>
> PS C:\scripts\test> type test[1].txt
> even 'type' command is also unable to show the contents of this file
> from powershell shell. if you try from DOS prompt, it works just fine.
>
> I am not sure how do I access such files in powershell. any ideas?
>
> regards, Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>

My System SpecsSystem Spec
Old 12-05-2007   #3 (permalink)
Roman Kuzmin


 
 

Re: filenames with [ characters

Yet another puzzle: how to rename a file with this name:
name`][

Thanks,
Roman Kuzmin


My System SpecsSystem Spec
Old 12-05-2007   #4 (permalink)
Umesh Thakur


 
 

Re: filenames with [ characters

get-childitem is able to show the file listing however, get-content or type
commands are still unable to show the content.

--
Umesh

"Old programmers never die. They just terminate and stay resident."



"Shay Levi" wrote:
Quote:

> It fails because square brackets are considered as metacharacters, try:
>
> Get-ChildItem test````[1````].txt
>
> -or-
>
> Get-ChildItem test````[*
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > I have noticed that filenames that include [ or ] characters,
> > powershell (V2
> > CTP) is not able
> > to read them using get-content command. I also tried to rename such
> > files
> > manually as well using powershell scripts, but they throw errors
> > saying file
> > does not exists.
> > PS C:\scripts\test> Set-Content test[.txt -value "This is test" PS
> > C:\scripts\test> dir
> >
> > it shows nothing (not creating the file at all)
> >
> > if there is a file that includes bracets, created elsewhere, say
> > "test[1].txt" you wont be able to read that using powershell.
> >
> > PS C:\scripts\test> notepad "test[1].txt"
> > PS C:\scripts\test> Get-ChildItem 'test`[1`].txt'
> > Get-ChildItem : Cannot find path 'C:\scripts\test\test`[1`].txt'
> > because it
> > does not exist.
> > At line:1 char:14
> > + Get-ChildItem <<<< 'test`[1`].txt'
> > PS C:\scripts\test> Get-ChildItem 'test[1].txt'
> > PS C:\scripts\test> Get-ChildItem test[1].txt
> > PS C:\scripts\test>
> > PS C:\scripts\test> type test[1].txt
> > even 'type' command is also unable to show the contents of this file
> > from powershell shell. if you try from DOS prompt, it works just fine.
> >
> > I am not sure how do I access such files in powershell. any ideas?
> >
> > regards, Umesh
> >
> > "Old programmers never die. They just terminate and stay resident."
> >
>
>
>
My System SpecsSystem Spec
Old 12-05-2007   #5 (permalink)
Shay Levi


 
 

Re: filenames with [ characters

Try this:

# create new file with content (with brackets in the file name)
PS > Set-Content -LiteralPath test[1].txt -value "bla`nbla`nbla"

# get the content
PS > Get-Content -LiteralPath test[1].txt
bla
bla
bla

# rename-item doesn't have a -LiteralPath parameter
# use move-item as a workaround
PS > move-Item -LiteralPath test[1].txt test[2].txt


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> get-childitem is able to show the file listing however, get-content or
> type commands are still unable to show the content.
>
> "Old programmers never die. They just terminate and stay resident."
>
> "Shay Levi" wrote:
>
Quote:

>> It fails because square brackets are considered as metacharacters,
>> try:
>>
>> Get-ChildItem test````[1````].txt
>>
>> -or-
>>
>> Get-ChildItem test````[*
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> I have noticed that filenames that include [ or ] characters,
>>> powershell (V2
>>> CTP) is not able
>>> to read them using get-content command. I also tried to rename such
>>> files
>>> manually as well using powershell scripts, but they throw errors
>>> saying file
>>> does not exists.
>>> PS C:\scripts\test> Set-Content test[.txt -value "This is test" PS
>>> C:\scripts\test> dir
>>> it shows nothing (not creating the file at all)
>>>
>>> if there is a file that includes bracets, created elsewhere, say
>>> "test[1].txt" you wont be able to read that using powershell.
>>>
>>> PS C:\scripts\test> notepad "test[1].txt"
>>> PS C:\scripts\test> Get-ChildItem 'test`[1`].txt'
>>> Get-ChildItem : Cannot find path 'C:\scripts\test\test`[1`].txt'
>>> because it
>>> does not exist.
>>> At line:1 char:14
>>> + Get-ChildItem <<<< 'test`[1`].txt'
>>> PS C:\scripts\test> Get-ChildItem 'test[1].txt'
>>> PS C:\scripts\test> Get-ChildItem test[1].txt
>>> PS C:\scripts\test>
>>> PS C:\scripts\test> type test[1].txt
>>> even 'type' command is also unable to show the contents of this file
>>> from powershell shell. if you try from DOS prompt, it works just
>>> fine.
>>> I am not sure how do I access such files in powershell. any ideas?
>>>
>>> regards, Umesh
>>>
>>> "Old programmers never die. They just terminate and stay resident."
>>>

My System SpecsSystem Spec
Old 12-05-2007   #6 (permalink)
Jeffery Hicks [MVP]


 
 

Re: filenames with [ characters

You might try using the -literalpath parameter. It is available for both
Get-Childitem and Get-content.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.

"Umesh Thakur" <UmeshThakur@xxxxxx> wrote in message
news:1A79D6C3-9090-4142-9A1F-F9B56A109DC8@xxxxxx
Quote:

> get-childitem is able to show the file listing however, get-content or
> type
> commands are still unable to show the content.
>
> --
> Umesh
>
> "Old programmers never die. They just terminate and stay resident."
>
>
>
> "Shay Levi" wrote:
>
Quote:

>> It fails because square brackets are considered as metacharacters, try:
>>
>> Get-ChildItem test````[1````].txt
>>
>> -or-
>>
>> Get-ChildItem test````[*
>>
>>
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>
>>
>>
Quote:

>> > I have noticed that filenames that include [ or ] characters,
>> > powershell (V2
>> > CTP) is not able
>> > to read them using get-content command. I also tried to rename such
>> > files
>> > manually as well using powershell scripts, but they throw errors
>> > saying file
>> > does not exists.
>> > PS C:\scripts\test> Set-Content test[.txt -value "This is test" PS
>> > C:\scripts\test> dir
>> >
>> > it shows nothing (not creating the file at all)
>> >
>> > if there is a file that includes bracets, created elsewhere, say
>> > "test[1].txt" you wont be able to read that using powershell.
>> >
>> > PS C:\scripts\test> notepad "test[1].txt"
>> > PS C:\scripts\test> Get-ChildItem 'test`[1`].txt'
>> > Get-ChildItem : Cannot find path 'C:\scripts\test\test`[1`].txt'
>> > because it
>> > does not exist.
>> > At line:1 char:14
>> > + Get-ChildItem <<<< 'test`[1`].txt'
>> > PS C:\scripts\test> Get-ChildItem 'test[1].txt'
>> > PS C:\scripts\test> Get-ChildItem test[1].txt
>> > PS C:\scripts\test>
>> > PS C:\scripts\test> type test[1].txt
>> > even 'type' command is also unable to show the contents of this file
>> > from powershell shell. if you try from DOS prompt, it works just fine.
>> >
>> > I am not sure how do I access such files in powershell. any ideas?
>> >
>> > regards, Umesh
>> >
>> > "Old programmers never die. They just terminate and stay resident."
>> >
>>
>>
>>
My System SpecsSystem Spec
Old 12-05-2007   #7 (permalink)
Jeffery Hicks [MVP]


 
 

Re: filenames with [ characters

The rename cmdlet doesn't use -literalpath. But you could use copy:

copy -literalpath "I[am] a test.txt" "IAmATest.txt"
del -literalpath "I[am] test.txt"

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.

"Roman Kuzmin" <z@xxxxxx> wrote in message
news:#OBtN0zNIHA.5160@xxxxxx
Quote:

> Yet another puzzle: how to rename a file with this name:
> name`][
>
> Thanks,
> Roman Kuzmin
>
My System SpecsSystem Spec
Old 12-05-2007   #8 (permalink)
Roman Kuzmin


 
 

Re: filenames with [ characters

Hi Jeffery,

Thank you, it should work, indeed.



But I am actually interested in the straightforward solution (e.g. with
escaping). It is more likely a puzzle for fun, not a real task.

"Jeffery Hicks [MVP]" <jhicks@xxxxxx> wrote in message
news:6E7A7B38-6271-4784-AE59-1766F183C509@xxxxxx
Quote:

> The rename cmdlet doesn't use -literalpath. But you could use copy:
>
> copy -literalpath "I[am] a test.txt" "IAmATest.txt"
> del -literalpath "I[am] test.txt"
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP

My System SpecsSystem Spec
Old 12-05-2007   #9 (permalink)
Roman Kuzmin


 
 

Re: filenames with [ characters

By the way, what if
Quote:

> copy -literalpath "I[am] a test.txt" "IAmATest.txt"
fails (there are many possible reasons) and
Quote:

> del -literalpath "I[am] test.txt"
succeeds?

Then this scenario simply removes a file. It is not a perfect solution yet.

Thanks,
Roman


"Jeffery Hicks [MVP]" <jhicks@xxxxxx> wrote in message
news:6E7A7B38-6271-4784-AE59-1766F183C509@xxxxxx
Quote:

> The rename cmdlet doesn't use -literalpath. But you could use copy:
>
> copy -literalpath "I[am] a test.txt" "IAmATest.txt"
> del -literalpath "I[am] test.txt"
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>
> "Roman Kuzmin" <z@xxxxxx> wrote in message
> news:#OBtN0zNIHA.5160@xxxxxx
Quote:

>> Yet another puzzle: how to rename a file with this name:
>> name`][
>>
>> Thanks,
>> Roman Kuzmin
>>

My System SpecsSystem Spec
Old 12-05-2007   #10 (permalink)
Shay Levi


 
 

Re: filenames with [ characters

I guess you didn't read my reply to the end :-)

Copy and then delete is a two steps process. Rename-item doesn't have a -LiteralPath
parameter, use move-item as a workaround

PS> move-Item -LiteralPath test[1].txt test[2].txt



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> By the way, what if
>
Quote:

>> copy -literalpath "I[am] a test.txt" "IAmATest.txt"
>>
> fails (there are many possible reasons) and
>
Quote:

>> del -literalpath "I[am] test.txt"
>>
> succeeds?
>
> Then this scenario simply removes a file. It is not a perfect solution
> yet.
>
> Thanks,
> Roman
> "Jeffery Hicks [MVP]" <jhicks@xxxxxx> wrote in message
> news:6E7A7B38-6271-4784-AE59-1766F183C509@xxxxxx
>
Quote:

>> The rename cmdlet doesn't use -literalpath. But you could use copy:
>>
>> copy -literalpath "I[am] a test.txt" "IAmATest.txt" del -literalpath
>> "I[am] test.txt"
>>
>> --
>> Jeffery Hicks
>> Microsoft PowerShell MVP
>> http://www.scriptinganswers.com
>> http://www.powershellcommunity.org
>> Now Available: WSH and VBScript Core: TFM
>> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>> "Roman Kuzmin" <z@xxxxxx> wrote in message
>> news:#OBtN0zNIHA.5160@xxxxxx
>>
Quote:

>>> Yet another puzzle: how to rename a file with this name: name`][
>>>
>>> Thanks,
>>> Roman Kuzmin

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
BUG? DirectoryInfo.GetFiles() wildcards fail on filenames > 8 characters .NET General
More missing filenames! Vista General
Replace special characters in filenames - how? PowerShell
Green Filenames - Why? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46