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 - rename-item, move-item and special chars.

Reply
 
Old 07-26-2009   #1 (permalink)


Vista Home Premium 32bit
 
 

rename-item, move-item and special chars.

Unluckily another problem has risen.
Within my nested folders may occur that different files have the same name.

c:\my_path\file[].doc
c:\my_path\folder1\file[].doc
c:\my_path\folder2\file[].doc

and so on.

I need to move all these files to the same folder. To ensure that each file has a unique name I wrote this

$i = 1
get-ChildItem -recurse -filter *.doc | % {
rename-Item $_.fullname -newname ($i.ToString()+"#"+$_.name)
$i++
}

but obviously it doesn't work with special chars like "[" and "]".
I've already read similar thread on this forum and I know that rename-item doesn't have -literalpath switch while move-item has it.
How can I move those files to same folder giving them names like

1#file[].doc, 2#file[].doc, 3#file[].doc and so on. Thanks again.

My System SpecsSystem Spec
Old 07-26-2009   #2 (permalink)


Vista Home Premium 32bit
 
 

Re: rename-item, move-item and special chars.

Quote  Quote: Originally Posted by sardinian_guy View Post
Unluckily another problem has risen.
Within my nested folders may occur that different files have the same name.

c:\my_path\file[].doc
c:\my_path\folder1\file[].doc
c:\my_path\folder2\file[].doc

and so on.

I need to move all these files to the same folder. To ensure that each file has a unique name I wrote this

$i = 1
get-ChildItem -recurse -filter *.doc | % {
rename-Item $_.fullname -newname ($i.ToString()+"#"+$_.name)
$i++
}

but obviously it doesn't work with special chars like "[" and "]".
I've already read similar thread on this forum and I know that rename-item doesn't have -literalpath switch while move-item has it.
How can I move those files to same folder giving them names like

1#file[].doc, 2#file[].doc, 3#file[].doc and so on. Thanks again.
Ok, I've found this link

http://www.microsoft.com/communities...&sloc=&m=1&p=1

I wrote

$i = 1
get-ChildItem -recurse -filter *.doc | % {
move-Item -literalpath $_.fullname -destination ($_.directoryname+"\"+$i.ToString()+"#"+$_.name) -whatif
$i++
}

and it seems to work well. I can now write my paths to a txt file and move files with unique names to the same folders.
My System SpecsSystem Spec
Old 07-26-2009   #3 (permalink)
Bob Landau


 
 

Re: rename-item, move-item and special chars.

This is a bug

Rename-Item does not take wildcards so doesn't need the LiteralPath

rename-item file.doc file[].doc ## works just fine

so the recepical

rename-item file[].doc file.doc ## should work

I suspect that Rename-Item is calling the equivalent of Get-Item passing the
path qualifying it to be used as a literal path.

"sardinian_guy" wrote:
Quote:

>
> sardinian_guy;1099209 Wrote:
Quote:

> > Unluckily another problem has risen.
> > Within my nested folders may occur that different files have the same
> > name.
> >
> > c:\my_path\file[].doc
> > c:\my_path\folder1\file[].doc
> > c:\my_path\folder2\file[].doc
> >
> > and so on.
> >
> > I need to move all these files to the same folder. To ensure that each
> > file has a unique name I wrote this
> >
> > $i = 1
> > get-ChildItem -recurse -filter *.doc | % {
> > rename-Item $_.fullname -newname ($i.ToString()+"#"+$_.name)
> > $i++
> > }
> >
> > but obviously it doesn't work with special chars like "[" and "]".
> > I've already read similar thread on this forum and I know that
> > rename-item doesn't have -literalpath switch while move-item has it.
> > How can I move those files to same folder giving them names like
> >
> > 1#file[].doc, 2#file[].doc, 3#file[].doc and so on. Thanks again.
>
> Ok, I've found this link
>
> http://www.microsoft.com/communities...&sloc=&m=1&p=1
>
> I wrote
>
> $i = 1
> get-ChildItem -recurse -filter *.doc | % {
> move-Item -literalpath $_.fullname -destination
> ($_.directoryname+"\"+$i.ToString()+"#"+$_.name) -whatif
> $i++
> }
>
> and it seems to work well. I can now write my paths to a txt file and
> move files with unique names to the same folders.
>
>
> --
> sardinian_guy
>
My System SpecsSystem Spec
Old 07-26-2009   #4 (permalink)


Vista Home Premium 32bit
 
 

Re: rename-item, move-item and special chars.

Quote  Quote: Originally Posted by Bob Landau View Post
This is a bug

Rename-Item does not take wildcards so doesn't need the LiteralPath

rename-item file.doc file[].doc ## works just fine

so the recepical

rename-item file[].doc file.doc ## should work

I suspect that Rename-Item is calling the equivalent of Get-Item passing the
path qualifying it to be used as a literal path.

"sardinian_guy" wrote:
Quote:

>
> sardinian_guy;1099209 Wrote:
Quote:

> > Unluckily another problem has risen.
> > Within my nested folders may occur that different files have the same
> > name.
> >
> > c:\my_path\file[].doc
> > c:\my_path\folder1\file[].doc
> > c:\my_path\folder2\file[].doc
> >
> > and so on.
> >
> > I need to move all these files to the same folder. To ensure that each
> > file has a unique name I wrote this
> >
> > $i = 1
> > get-ChildItem -recurse -filter *.doc | % {
> > rename-Item $_.fullname -newname ($i.ToString()+"#"+$_.name)
> > $i++
> > }
> >
> > but obviously it doesn't work with special chars like "[" and "]".
> > I've already read similar thread on this forum and I know that
> > rename-item doesn't have -literalpath switch while move-item has it.
> > How can I move those files to same folder giving them names like
> >
> > 1#file[].doc, 2#file[].doc, 3#file[].doc and so on. Thanks again.
>
> Ok, I've found this link
>
> Replace special characters in filenames - how? in Windows Powershell
>
> I wrote
>
> $i = 1
> get-ChildItem -recurse -filter *.doc | % {
> move-Item -literalpath $_.fullname -destination
> ($_.directoryname+"\"+$i.ToString()+"#"+$_.name) -whatif
> $i++
> }
>
> and it seems to work well. I can now write my paths to a txt file and
> move files with unique names to the same folders.
>
>
> --
> sardinian_guy
>
Thanks again Bob for your answer. You're very kind.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
rename-item problem PowerShell
Invoke-Item cannot find paths with special characters PowerShell
Rename-Item problem PowerShell
Conflicting behaviour of rename-item cmdlet PowerShell
Rename-Item bug? 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