header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Problem renaming files containing square brackets
Last Post 08 Nov 2008 07:09 PM by glnsize. 8 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
paulie0498User is Offline
New Member
New Member
Posts:1
Avatar

--
08 Nov 2008 02:13 AM  

I am trying to do a simple rename of files in a directory in PoSH v1.0. Some of the files contain square brackets. When I run the one liner I have written it renames any files without square brackets but errors on any that do.

My code is as follows:

ls -filter *.log | Rename-Item -NewName { $_.Name.Replace("-",".") }

The error that I get is 'Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\FileSystem::H:\Logs\xxx-xxxxx-[xxx]-xx.log' does not exist.'

I have come across a post on the Microsoft PowerShell forum which mentioned the following workaround but it seems like an incredibly convoluted method of simply renaming files. The workaround they mentioned was for a problem someone else was having but the issue was the same. The workaround:

pushd
sl c:\directory1
$dir = dir *.avi
sl c:\directory2
pushd x:\ # <-- jump to another drive
foreach ($avi in $dir)
{
    $mkv = "c:\directory2\$($avi.name.replace(".avi",".mkv"))"
    rni ($mkv -replace '\[|]','`$0') $avi.name
}
popd # <-- jump back to C:\ drive
popd

I am hoping that there is a much more elegant answer to this problem. Any help would be appreciated.

P.S. When I added < code > < /code > tags to my code the preview came up as scroll bars with no visible code so I have omitted them. Apologies if this makes it harder to read my post. If anyone knows why this is happening please let me know.

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
08 Nov 2008 03:12 AM  
First of all let me apologize for the problems with the forum. We are working on that and have an upgrade planned.

Regarding your issue, this is a known bug, which you can see here. You may have to create an account on the Connect website in order to see the URL.

Anyways, yeah, that's why the elaborate painful workaround. The way this is supposed to work is that the pipeline should just take care of it. e.g. ls "[filewithbrackets]" | rename-item -new "foo"

Or, there is a parameter which some cmdlets have (and this one doesn't for some reason) called literalPath. e.g.

rename-item -literalPath "[filewithbrackets]" -new "foo"

That would work because literalPath instructs the cmdlet not to look for wildcards and other pattern matching characters, in which the brackets are included.

Anyway, it's a pain, but it'll be fixed in powershell V2. It may be fixed in the CTP version, I don't have that handy at the moment to check.

So...long story short, I recommend enclosing the above into a function and use it in your scripts wherever you have brackets in filenames.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
glnsizeUser is Offline
Basic Member
Basic Member
Posts:101

--
08 Nov 2008 03:21 AM  

 how about a one liner... 

 

ls *.avi | %{([system.io.FileInfo]$_.fullname).moveto($_.fullname.replace(".avi",".mkv"))}

~Glenn 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
08 Nov 2008 03:24 AM  
Nice. Now turn it into a function so it's more reusable. :)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
glnsizeUser is Offline
Basic Member
Basic Member
Posts:101

--
08 Nov 2008 03:25 AM  

The single line of code with a scroll bar comes from no line breaks. When pasting in the forums..

< CODE > past your code (hit enter)
< / CODE >

at least that's how I get around it :)

glnsizeUser is Offline
Basic Member
Basic Member
Posts:101

--
08 Nov 2008 05:38 AM  

There I go again over complicating things... 

ls -filter *.avi | Move-Item -Destination { $_.Name.Replace(".avi",".mkv") }

will do the same thing...

~Glenn

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
08 Nov 2008 12:25 PM  
Are you sure? Did you test it with bracket files?
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
ShayUser is Offline
Basic Member
Basic Member
Posts:272
Avatar

--
08 Nov 2008 12:53 PM  
This is working for me (not tested on PS v1):

PS > dir -filter *.log | where {$_.name -match '[\[|\]]'} | move-item -literal {$_.name} -dest {$_.name -replace '[\[|\]]' }
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
glnsizeUser is Offline
Basic Member
Basic Member
Posts:101

--
08 Nov 2008 07:09 PM  

 yup... Last night after I posted that first oneliner I got to thinking about why it worked.  It's not performing a rename operation just changing the path.  After a little playing around with the object I figured out that move-item is performing the same exact operation...   A word of caution ,since the path is just being changed, be careful with binary files.

 

no faith hal 

~Glenn

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer