![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 > } > 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||