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 - Using Paths with Special Characters and/or Spaces

Reply
 
Old 08-04-2006   #1 (permalink)
public.microsoft.com


 
 

Using Paths with Special Characters and/or Spaces

Im pretty new to Powershell, but what I am trying to do is move a list of
files in the root, to a folder named after the files.

Example
Name Mode
---- ----
Feb 06 d----
Feb 06 001.jpg -a---
Feb 06 002.jpg -a---
Feb 06 003.jpg -a---
Feb 06 004.jpg -a---
Feb 06 005.jpg -a---
Feb 06 006.jpg -a---
Feb 06 007.jpg -a---
Feb 06 008.jpg -a---
Feb 06 009.jpg -a---
Feb 06 010.jpg -a---
Feb 06 011.jpg -a---

Here is the failing code
------------------------
$list = dir | where {$_.PsIsContainer -eq $true}
ForEach ($folder in $list) {
$files = dir $folder
ForEach ($File in $Files) {
move-item -path $File.Name -destination $folder.Name
}
}



My System SpecsSystem Spec
Old 08-05-2006   #2 (permalink)
=?Utf-8?B?QW5kcmV3IFNhdmlueWto?=


 
 

RE: Using Paths with Special Characters and/or Spaces

I'm not sure what you are trying to achieve. Could you please try to explain
it better. I.e. what files/directories you have and what you want to get as
the result of your operation. Your script looks like you are trying to copy
files onto themselves.

"public.microsoft.com" wrote:

> Im pretty new to Powershell, but what I am trying to do is move a list of
> files in the root, to a folder named after the files.
>
> Example
> Name Mode
> ---- ----
> Feb 06 d----
> Feb 06 001.jpg -a---
> Feb 06 002.jpg -a---
> Feb 06 003.jpg -a---
> Feb 06 004.jpg -a---
> Feb 06 005.jpg -a---
> Feb 06 006.jpg -a---
> Feb 06 007.jpg -a---
> Feb 06 008.jpg -a---
> Feb 06 009.jpg -a---
> Feb 06 010.jpg -a---
> Feb 06 011.jpg -a---
>
> Here is the failing code
> ------------------------
> $list = dir | where {$_.PsIsContainer -eq $true}
> ForEach ($folder in $list) {
> $files = dir $folder
> ForEach ($File in $Files) {
> move-item -path $File.Name -destination $folder.Name
> }
> }
>
>
>

My System SpecsSystem Spec
Old 08-05-2006   #3 (permalink)
Brandon Shell


 
 

Re: Using Paths with Special Characters and/or Spaces

I appologize for not explaining differently. I am trying to find some tasks
that I need to done and do them with powershell. To get better familar with
it.

One such task is moving JPEG files to a certain file based on filename. Im
lucky in the sense that the Destination folder contains the file name i.e.:
Folder = Feb 06
Files = Feb 06 001.jpg
Feb 06 002.jpg
Feb 06 003.jpg
Feb 06 004.jpg

My problem is the Files/Folders have spaces in them.

I may be approaching it incorrectly, but I figured I could get a list of
folders.
For Each of the folders I do a dir for files that contain the dir Name and
put object in a varible. (this works)
For Each of the files in the list I try to copy them to the destination
folder. (this gives error about file not existing)

>> $list = dir | where {$_.PsIsContainer -eq $true}
>> ForEach ($folder in $list) {
>> $files = dir $folder
>> ForEach ($File in $Files) {
>> move-item -path $File.Name -destination $folder.Name
>> }
>> }


"Andrew Savinykh" <AndrewSavinykh@discussions.microsoft.com> wrote in
message news:F14B9E48-89D3-4D6D-AE4B-508F310CDAE8@microsoft.com...
> I'm not sure what you are trying to achieve. Could you please try to
> explain
> it better. I.e. what files/directories you have and what you want to get
> as
> the result of your operation. Your script looks like you are trying to
> copy
> files onto themselves.
>
> "public.microsoft.com" wrote:
>
>> Im pretty new to Powershell, but what I am trying to do is move a list of
>> files in the root, to a folder named after the files.
>>
>> Example
>> Name Mode
>> ---- ----
>> Feb 06 d----
>> Feb 06 001.jpg -a---
>> Feb 06 002.jpg -a---
>> Feb 06 003.jpg -a---
>> Feb 06 004.jpg -a---
>> Feb 06 005.jpg -a---
>> Feb 06 006.jpg -a---
>> Feb 06 007.jpg -a---
>> Feb 06 008.jpg -a---
>> Feb 06 009.jpg -a---
>> Feb 06 010.jpg -a---
>> Feb 06 011.jpg -a---
>>
>> Here is the failing code
>> ------------------------
>> $list = dir | where {$_.PsIsContainer -eq $true}
>> ForEach ($folder in $list) {
>> $files = dir $folder
>> ForEach ($File in $Files) {
>> move-item -path $File.Name -destination $folder.Name
>> }
>> }
>>
>>
>>



My System SpecsSystem Spec
Old 08-05-2006   #4 (permalink)
Jacques Barathon [MS]


 
 

Re: Using Paths with Special Characters and/or Spaces

Your second "dir" ($files = dir $folder) does not have any wildcard so what
you get then is just your folder name ("Feb 06"). You also need to filter
the resulting list so it does not include folders (otherwise you will move
folders along with your files).

Try this, it should work as expected:

$list = dir | ? {$_.PSIsContainer}
$list | % {$dir = $_.name; dir "$dir*" | ? {!$_.PSIsContainer} | % {move $_
$dir}}

Jacques

"Brandon Shell" <tshell.mask@gmail.com> a écrit dans le message de news:
%23a8WMAKuGHA.4748@TK2MSFTNGP03.phx.gbl...
>I appologize for not explaining differently. I am trying to find some tasks
>that I need to done and do them with powershell. To get better familar with
>it.
>
> One such task is moving JPEG files to a certain file based on filename. Im
> lucky in the sense that the Destination folder contains the file name
> i.e.:
> Folder = Feb 06
> Files = Feb 06 001.jpg
> Feb 06 002.jpg
> Feb 06 003.jpg
> Feb 06 004.jpg
>
> My problem is the Files/Folders have spaces in them.
>
> I may be approaching it incorrectly, but I figured I could get a list of
> folders.
> For Each of the folders I do a dir for files that contain the dir Name and
> put object in a varible. (this works)
> For Each of the files in the list I try to copy them to the destination
> folder. (this gives error about file not existing)
>
>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>> ForEach ($folder in $list) {
>>> $files = dir $folder
>>> ForEach ($File in $Files) {
>>> move-item -path $File.Name -destination $folder.Name
>>> }
>>> }

>
> "Andrew Savinykh" <AndrewSavinykh@discussions.microsoft.com> wrote in
> message news:F14B9E48-89D3-4D6D-AE4B-508F310CDAE8@microsoft.com...
>> I'm not sure what you are trying to achieve. Could you please try to
>> explain
>> it better. I.e. what files/directories you have and what you want to get
>> as
>> the result of your operation. Your script looks like you are trying to
>> copy
>> files onto themselves.
>>
>> "public.microsoft.com" wrote:
>>
>>> Im pretty new to Powershell, but what I am trying to do is move a list
>>> of
>>> files in the root, to a folder named after the files.
>>>
>>> Example
>>> Name Mode
>>> ---- ----
>>> Feb 06 d----
>>> Feb 06 001.jpg -a---
>>> Feb 06 002.jpg -a---
>>> Feb 06 003.jpg -a---
>>> Feb 06 004.jpg -a---
>>> Feb 06 005.jpg -a---
>>> Feb 06 006.jpg -a---
>>> Feb 06 007.jpg -a---
>>> Feb 06 008.jpg -a---
>>> Feb 06 009.jpg -a---
>>> Feb 06 010.jpg -a---
>>> Feb 06 011.jpg -a---
>>>
>>> Here is the failing code
>>> ------------------------
>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>> ForEach ($folder in $list) {
>>> $files = dir $folder
>>> ForEach ($File in $Files) {
>>> move-item -path $File.Name -destination $folder.Name
>>> }
>>> }
>>>
>>>
>>>

>
>



My System SpecsSystem Spec
Old 08-06-2006   #5 (permalink)
Brandon Shell


 
 

Re: Using Paths with Special Characters and/or Spaces

That Worked like a charm... I dont suppose you could explain exactly what
you were doing?
Specifically: $list | % {$dir = $_.name; dir "$dir*" | ? {!$_.PSIsContainer}
| % {move $_ $dir}}
I think I have the idea, I just want to make sure I understand what your
doing


"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:uEdT6dKuGHA.452@TK2MSFTNGP05.phx.gbl...
> Your second "dir" ($files = dir $folder) does not have any wildcard so
> what you get then is just your folder name ("Feb 06"). You also need to
> filter the resulting list so it does not include folders (otherwise you
> will move folders along with your files).
>
> Try this, it should work as expected:
>
> $list = dir | ? {$_.PSIsContainer}
> $list | % {$dir = $_.name; dir "$dir*" | ? {!$_.PSIsContainer} | % {move
> $_ $dir}}
>
> Jacques
>
> "Brandon Shell" <tshell.mask@gmail.com> a écrit dans le message de news:
> %23a8WMAKuGHA.4748@TK2MSFTNGP03.phx.gbl...
>>I appologize for not explaining differently. I am trying to find some
>>tasks that I need to done and do them with powershell. To get better
>>familar with it.
>>
>> One such task is moving JPEG files to a certain file based on filename.
>> Im lucky in the sense that the Destination folder contains the file name
>> i.e.:
>> Folder = Feb 06
>> Files = Feb 06 001.jpg
>> Feb 06 002.jpg
>> Feb 06 003.jpg
>> Feb 06 004.jpg
>>
>> My problem is the Files/Folders have spaces in them.
>>
>> I may be approaching it incorrectly, but I figured I could get a list of
>> folders.
>> For Each of the folders I do a dir for files that contain the dir Name
>> and put object in a varible. (this works)
>> For Each of the files in the list I try to copy them to the destination
>> folder. (this gives error about file not existing)
>>
>>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>>> ForEach ($folder in $list) {
>>>> $files = dir $folder
>>>> ForEach ($File in $Files) {
>>>> move-item -path $File.Name -destination $folder.Name
>>>> }
>>>> }

>>
>> "Andrew Savinykh" <AndrewSavinykh@discussions.microsoft.com> wrote in
>> message news:F14B9E48-89D3-4D6D-AE4B-508F310CDAE8@microsoft.com...
>>> I'm not sure what you are trying to achieve. Could you please try to
>>> explain
>>> it better. I.e. what files/directories you have and what you want to get
>>> as
>>> the result of your operation. Your script looks like you are trying to
>>> copy
>>> files onto themselves.
>>>
>>> "public.microsoft.com" wrote:
>>>
>>>> Im pretty new to Powershell, but what I am trying to do is move a list
>>>> of
>>>> files in the root, to a folder named after the files.
>>>>
>>>> Example
>>>> Name Mode
>>>> ---- ----
>>>> Feb 06 d----
>>>> Feb 06 001.jpg -a---
>>>> Feb 06 002.jpg -a---
>>>> Feb 06 003.jpg -a---
>>>> Feb 06 004.jpg -a---
>>>> Feb 06 005.jpg -a---
>>>> Feb 06 006.jpg -a---
>>>> Feb 06 007.jpg -a---
>>>> Feb 06 008.jpg -a---
>>>> Feb 06 009.jpg -a---
>>>> Feb 06 010.jpg -a---
>>>> Feb 06 011.jpg -a---
>>>>
>>>> Here is the failing code
>>>> ------------------------
>>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>>> ForEach ($folder in $list) {
>>>> $files = dir $folder
>>>> ForEach ($File in $Files) {
>>>> move-item -path $File.Name -destination $folder.Name
>>>> }
>>>> }
>>>>
>>>>
>>>>

>>
>>

>
>



My System SpecsSystem Spec
Old 08-06-2006   #6 (permalink)
Jacques Barathon [MS]


 
 

Re: Using Paths with Special Characters and/or Spaces

"Brandon Shell" <tshell.mask@gmail.com> a écrit dans le message de news:
eVhY71WuGHA.1224@TK2MSFTNGP03.phx.gbl...
> That Worked like a charm... I dont suppose you could explain exactly what
> you were doing?
> Specifically: $list | % {$dir = $_.name; dir "$dir*" | ?
> {!$_.PSIsContainer} | % {move $_ $dir}}
> I think I have the idea, I just want to make sure I understand what your
> doing


Before all, % is an alias for foreach-object, and ? is an alias for
where-object.

I am basically doing the same thing you were doing, ie a series of foreach
loops. I just used the pipeline to pass objects to the next set of commands
instead of assigning them to variables (eg foreach $folder in $list, foreach
$file in $files).

I start with getting a list of folders:
$list = dir | ? {$_.PSIsContainer}

.... then for each folder I take all files (not folders) that have their
names starting with the folder name:
$list | % {$dir = $_.name; dir "$dir*" | ? {!$_.PSIsContainer}
(here I assign the folder name to $dir because I need to use it again for
the move instruction where $_ will point to another object)

Finally I take all resulting files and I move them to the folder:
.... | % {move $_ $dir}}

Jacques

>
> "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
> news:uEdT6dKuGHA.452@TK2MSFTNGP05.phx.gbl...
>> Your second "dir" ($files = dir $folder) does not have any wildcard so
>> what you get then is just your folder name ("Feb 06"). You also need to
>> filter the resulting list so it does not include folders (otherwise you
>> will move folders along with your files).
>>
>> Try this, it should work as expected:
>>
>> $list = dir | ? {$_.PSIsContainer}
>> $list | % {$dir = $_.name; dir "$dir*" | ? {!$_.PSIsContainer} | % {move
>> $_ $dir}}
>>
>> Jacques
>>
>> "Brandon Shell" <tshell.mask@gmail.com> a écrit dans le message de news:
>> %23a8WMAKuGHA.4748@TK2MSFTNGP03.phx.gbl...
>>>I appologize for not explaining differently. I am trying to find some
>>>tasks that I need to done and do them with powershell. To get better
>>>familar with it.
>>>
>>> One such task is moving JPEG files to a certain file based on filename.
>>> Im lucky in the sense that the Destination folder contains the file name
>>> i.e.:
>>> Folder = Feb 06
>>> Files = Feb 06 001.jpg
>>> Feb 06 002.jpg
>>> Feb 06 003.jpg
>>> Feb 06 004.jpg
>>>
>>> My problem is the Files/Folders have spaces in them.
>>>
>>> I may be approaching it incorrectly, but I figured I could get a list of
>>> folders.
>>> For Each of the folders I do a dir for files that contain the dir Name
>>> and put object in a varible. (this works)
>>> For Each of the files in the list I try to copy them to the destination
>>> folder. (this gives error about file not existing)
>>>
>>>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>>>> ForEach ($folder in $list) {
>>>>> $files = dir $folder
>>>>> ForEach ($File in $Files) {
>>>>> move-item -path $File.Name -destination $folder.Name
>>>>> }
>>>>> }
>>>
>>> "Andrew Savinykh" <AndrewSavinykh@discussions.microsoft.com> wrote in
>>> message news:F14B9E48-89D3-4D6D-AE4B-508F310CDAE8@microsoft.com...
>>>> I'm not sure what you are trying to achieve. Could you please try to
>>>> explain
>>>> it better. I.e. what files/directories you have and what you want to
>>>> get as
>>>> the result of your operation. Your script looks like you are trying to
>>>> copy
>>>> files onto themselves.
>>>>
>>>> "public.microsoft.com" wrote:
>>>>
>>>>> Im pretty new to Powershell, but what I am trying to do is move a list
>>>>> of
>>>>> files in the root, to a folder named after the files.
>>>>>
>>>>> Example
>>>>> Name Mode
>>>>> ---- ----
>>>>> Feb 06 d----
>>>>> Feb 06 001.jpg -a---
>>>>> Feb 06 002.jpg -a---
>>>>> Feb 06 003.jpg -a---
>>>>> Feb 06 004.jpg -a---
>>>>> Feb 06 005.jpg -a---
>>>>> Feb 06 006.jpg -a---
>>>>> Feb 06 007.jpg -a---
>>>>> Feb 06 008.jpg -a---
>>>>> Feb 06 009.jpg -a---
>>>>> Feb 06 010.jpg -a---
>>>>> Feb 06 011.jpg -a---
>>>>>
>>>>> Here is the failing code
>>>>> ------------------------
>>>>> $list = dir | where {$_.PsIsContainer -eq $true}
>>>>> ForEach ($folder in $list) {
>>>>> $files = dir $folder
>>>>> ForEach ($File in $Files) {
>>>>> move-item -path $File.Name -destination $folder.Name
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>>
>>>
>>>

>>
>>

>
>



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
dreaded spaces in shell special folders Vista file management
Invoke-Item cannot find paths with special characters PowerShell
Special Characters - how? Vista General
How to handle spaces in command line paths? PowerShell
RE: How to handle spaces in command line paths? 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