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 - Re: Can't rename files with certain characters in them.

Reply
 
Old 10-02-2008   #1 (permalink)
RickB


 
 

Re: Can't rename files with certain characters in them.

On Oct 2, 12:11*pm, "James R. Gentile" <n...@xxxxxx> wrote:
Quote:

> (sorry if this is a dupe, I posted this last night on microsoft communities
> newsgroup server and the post hasn't shown up yet)
>
> I have a bunch of .mkv files in one folder and the same files in another
> folder except some of them have .avi extensions, I want the file names to
> match, so I wrote a posh script to find .avi's in folder 2 and rename the
> like named .mkv files in folder 1 to the .avi filenames. *The problem is,
> for files that have characters like '(', ')', '[', or ']' the rename
> silently fails. *Here's the script:
>
> ~~~~~~~~~ mkv2avi.ps1 ~~~~~~~~~~~~~~~~~~~~~
> pushd
>
> set-location c:\directory1
>
> $dir=dir *.avi
>
> set-location c:\directory2
>
> foreach ($avi in $dir)
> {
> * *$mkv = $avi.name.replace(".avi",".mkv")
>
> * *mv ".\$($mkv)" ".\$($avi.name)" -confirm
>
> }
>
> popd
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> One file it fails on is like this:
> xxx.xxx.(xxx).xxx.xxx.xxx.xxx-xxx.[xxx.xxx].xxx
>
> How do I deal with this, or is there a better way to do this?
Are you using CTP?
I can reproduce your problem many ways on my CTP2 machine.
I'm speculating that both mv and ren check for * or ? in the filenames
passed and throw if either is found.
Then they call dir without the -literalpath param to get the source
file object for rename.
Hmm. Pretty serious bug...

Meanwhile I was able to rename successfully like this

(dir ".\$srcName" -literalpath).moveto(".\$destName")

You don't have a -confirm option with this method but you can write
your own.


My System SpecsSystem Spec
Old 10-02-2008   #2 (permalink)
James R. Gentile


 
 

Re: Can't rename files with certain characters in them.

Yes, I'm using Powershell V2 CTP. I don't understand your explanation, I'm
just a posh noob at this point, but thanks for verifying it's a bug for me.
I'll try the method you gave, in my script, thanks.

"RickB" <rbielaws@xxxxxx> wrote in message
news:7715c921-b51b-4a67-9185-f9c5feb2a1bc@xxxxxx
On Oct 2, 12:11 pm, "James R. Gentile" <n...@xxxxxx> wrote:
Quote:

> ...
Are you using CTP?
I can reproduce your problem many ways on my CTP2 machine.
I'm speculating that both mv and ren check for * or ? in the filenames
passed and throw if either is found.
Then they call dir without the -literalpath param to get the source
file object for rename.
Hmm. Pretty serious bug...

Meanwhile I was able to rename successfully like this

(dir ".\$srcName" -literalpath).moveto(".\$destName")

You don't have a -confirm option with this method but you can write
your own.

My System SpecsSystem Spec
Old 10-05-2008   #3 (permalink)
James R. Gentile


 
 

Re: Can't rename files with certain characters in them.

Your method does not work for me either. Additionally, what is wrong here:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
James Gentile@xxxxxx::C:\Users\James Gentile\PS\test$ dir -force


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\James
Gentile\PS\test


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/1/2008 5:51 PM 4 ()[]
-a--- 10/4/2008 5:24 PM 8 t
-a--- 10/1/2008 5:47 PM 4
xxx.xxx.(xxx).xxx.xxx.xxx.xxx-xxx.[xxx.xxx].xxx


James Gentile@xxxxxx::C:\Users\James Gentile\PS\test$ (get-item
"t").moveto("a")
Exception calling "MoveTo" with "1" argument(s): "Cannot create a file when
that file already exists.
"
At line:1 char:22
+ (get-item "t").moveto <<<< ("a")
James Gentile@xxxxxx::C:\Users\James Gentile\PS\test$
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The file does not exists, and I can execute "mv t a" and it works. I tried
closing Posh and reopening it and the same error occured.

"RickB" <rbielaws@xxxxxx> wrote in message
news:7715c921-b51b-4a67-9185-f9c5feb2a1bc@xxxxxx
On Oct 2, 12:11 pm, "James R. Gentile" <n...@xxxxxx> wrote:
Quote:

> (sorry if this is a dupe, I posted this last night on microsoft
> communities
> newsgroup server and the post hasn't shown up yet)
>
> I have a bunch of .mkv files in one folder and the same files in another
> folder except some of them have .avi extensions, I want the file names to
> match, so I wrote a posh script to find .avi's in folder 2 and rename the
> like named .mkv files in folder 1 to the .avi filenames. The problem is,
> for files that have characters like '(', ')', '[', or ']' the rename
> silently fails. Here's the script:
>
> ~~~~~~~~~ mkv2avi.ps1 ~~~~~~~~~~~~~~~~~~~~~
> pushd
>
> set-location c:\directory1
>
> $dir=dir *.avi
>
> set-location c:\directory2
>
> foreach ($avi in $dir)
> {
> $mkv = $avi.name.replace(".avi",".mkv")
>
> mv ".\$($mkv)" ".\$($avi.name)" -confirm
>
> }
>
> popd
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> One file it fails on is like this:
> xxx.xxx.(xxx).xxx.xxx.xxx.xxx-xxx.[xxx.xxx].xxx
>
> How do I deal with this, or is there a better way to do this?
Are you using CTP?
I can reproduce your problem many ways on my CTP2 machine.
I'm speculating that both mv and ren check for * or ? in the filenames
passed and throw if either is found.
Then they call dir without the -literalpath param to get the source
file object for rename.
Hmm. Pretty serious bug...

Meanwhile I was able to rename successfully like this

(dir ".\$srcName" -literalpath).moveto(".\$destName")

You don't have a -confirm option with this method but you can write
your own.

My System SpecsSystem Spec
Old 10-05-2008   #4 (permalink)
Kiron


 
 

Re: Can't rename files with certain characters in them.

There is a way to rename files with brackets in their name. First escape the brackets; in v2 one ` is sufficient, but in v1 two ` may be necessary. Then execute the Rename-Item statement from a drive other than the target files' drive.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
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

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Solved Rename Files General Discussion
rename all files from a directory to a list of files ... PowerShell
rename files (lots of files for newbie) PowerShell
renaming files with special characters PowerShell


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