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 - copy files in folders to a single folder

Reply
 
Old 02-16-2008   #1 (permalink)
hugh


 
 

copy files in folders to a single folder

Hi all

I am trying to find out a way to copy all the files that are in a folder
and its subfolders (a folder tree) into a single folder (so all files
are in the same folder. In other words I am trying to consolidate al
files from a tree of folders into a single folder with no subfolders).
If the file already exist in the target folder, the script should add a
progressive number to the the file name.

I have tried to use robocopy and xcopy to to this, perhaps powershell
can handle it?

Thanks a lot!

My System SpecsSystem Spec
Old 02-16-2008   #2 (permalink)
Shay Levi


 
 

Re: copy files in folders to a single folder


Remove copy-item' -whatIf switch when you're ready to go.


#################################


$root="c:\temp"
$destination="d:\temp"
$counter=0

dir $root -recurse | where {!$_.PSIsContainer} | foreach {

$tmp=$_.name
$path=join-path $destination $tmp

if(test-path $path){

while(test-path (join-path $destination $tmp)){
$tmp= "{0}{1}{2}" -f [io.path]::GetFileNameWithOutExtension($_.name),$counter,$_.Extension
$counter++
}

$counter=0
}

copy-item $_ (join-path $destination $tmp) -whatIf
}




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi all
>
> I am trying to find out a way to copy all the files that are in a
> folder and its subfolders (a folder tree) into a single folder (so all
> files are in the same folder. In other words I am trying to
> consolidate al files from a tree of folders into a single folder with
> no subfolders). If the file already exist in the target folder, the
> script should add a progressive number to the the file name.
>
> I have tried to use robocopy and xcopy to to this, perhaps powershell
> can handle it?
>
> Thanks a lot!
>

My System SpecsSystem Spec
Old 02-16-2008   #3 (permalink)
hugh


 
 

Re: copy files in folders to a single folder

Shay Levi wrote:
Quote:

>
> Remove copy-item' -whatIf switch when you're ready to go.
>
>
> #################################
>
>
> $root="c:\temp"
> $destination="d:\temp"
> $counter=0
>
> dir $root -recurse | where {!$_.PSIsContainer} | foreach {
>
> $tmp=$_.name
> $path=join-path $destination $tmp
>
> if(test-path $path){
>
> while(test-path (join-path $destination $tmp)){
> $tmp= "{0}{1}{2}" -f
> [io.path]::GetFileNameWithOutExtension($_.name),$counter,$_.Extension
> $counter++
> }
>
> $counter=0
> }
>
> copy-item $_ (join-path $destination $tmp) -whatIf
> }
>
Ohhhh

thanks a lot :-) !!!

I am going to study the code...

Thank you again!

hugh

Quote:

>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

>> Hi all
>>
>> I am trying to find out a way to copy all the files that are in a
>> folder and its subfolders (a folder tree) into a single folder (so all
>> files are in the same folder. In other words I am trying to
>> consolidate al files from a tree of folders into a single folder with
>> no subfolders). If the file already exist in the target folder, the
>> script should add a progressive number to the the file name.
>>
>> I have tried to use robocopy and xcopy to to this, perhaps powershell
>> can handle it?
>>
>> Thanks a lot!
>>
>
>
My System SpecsSystem Spec
Old 02-16-2008   #4 (permalink)
hugh


 
 

Re: copy files in folders to a single folder


Hi I tried it but i get an error , I think th eproblem might be in the
variables?


Copy-Item : Cannot find path 'C:\sandbox\file1.txt' because it does not
exist.
At C:\sandbox\copytosinglefolder.ps1:20 char:14
+ copy-item <<<< $_ (join-path $destination $tmp) -whatIf
Copy-Item : Cannot find path 'C:\sandbox\file2.txt' because it does not
exist.
At C:\sandbox\copytosinglefolder.ps1:20 char:14
+ copy-item <<<< $_ (join-path $destination $tmp) -whatIf
Copy-Item : Cannot find path 'C:\sandbox\file3.txt' because it does not
exist.
At C:\sandbox\copytosinglefolder.ps1:20 char:14
+ copy-item <<<< $_ (join-path $destination $tmp) -whatIf





Shay Levi wrote:
Quote:

>
> Remove copy-item' -whatIf switch when you're ready to go.
>
>
My System SpecsSystem Spec
Old 02-16-2008   #5 (permalink)
Shay Levi


 
 

Re: copy files in folders to a single folder



Try this:



$root="c:\temp"
$destination="d:\temp"
$counter=0

dir $root -recurse | where {!$_.PSIsContainer} | foreach {

$tmp= join-path $destination $_.name

if(test-path $tmp){

while(test-path $tmp){
$tmp= "{0}\{1}{2}{3}" -f $destination,[io.path]::GetFileNameWithOutExtension($_.name),$counter,$_.Extension
$counter++
}

$counter=0
}

copy-item $_.fullname $tmp
}







-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi I tried it but i get an error , I think th eproblem might be in the
> variables?
>
> Copy-Item : Cannot find path 'C:\sandbox\file1.txt' because it does
> not
> exist.
> At C:\sandbox\copytosinglefolder.ps1:20 char:14
> + copy-item <<<< $_ (join-path $destination $tmp) -whatIf
> Copy-Item : Cannot find path 'C:\sandbox\file2.txt' because it does
> not
> exist.
> At C:\sandbox\copytosinglefolder.ps1:20 char:14
> + copy-item <<<< $_ (join-path $destination $tmp) -whatIf
> Copy-Item : Cannot find path 'C:\sandbox\file3.txt' because it does
> not
> exist.
> At C:\sandbox\copytosinglefolder.ps1:20 char:14
> + copy-item <<<< $_ (join-path $destination $tmp) -whatIf
> Shay Levi wrote:
>
Quote:

>> Remove copy-item' -whatIf switch when you're ready to go.
>>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
WinExplorer > Opening folders, not files, on single-click Vista General
Copy files with folders Vista file management
Vista converts all single music files into a folder.. any way to s Vista music pictures video
Multiple files and folders, single user Vista account administration
Re: Copying CD files to folder, then copy folder to another hard drive, explorer hangs Vista file management


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