![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | Re: filenames with [ characters Yet another puzzle: how to rename a file with this name: name`][ Thanks, Roman Kuzmin |
My System Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | Re: filenames with [ characters By the way, what if Quote: > copy -literalpath "I[am] a test.txt" "IAmATest.txt" Quote: > del -literalpath "I[am] test.txt" 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 Specs![]() |
| | #10 (permalink) |
| | 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" >> > Quote: >> del -literalpath "I[am] test.txt" >> > > 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 Specs![]() |
![]() |
| 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 | |||