Copy Files Not Directory Structure - powershell

I'm trying to copy all of the files with .py extension from one directory to a new one using PowerShell, but I don't want to recreate the directory structure. I want this to work recursively as the the py files are in numerous subfolders. In my destination directory, I want nothing but the py files.
Here's what I have now but it copies the directory structure too:
Get-ChildItem "C:\Johns Stuff\Python\" |
Copy -Destination C:\Users\dread\python -Recurse -filter *.py

How about:
Get-ChildItem "C:\Johns Stuff\Python" -File -Filter *.py -Recurse | ForEach-Object {
Copy-Item $_.FullName C:\Users\dread\python -WhatIf
}
The -File parameter (new in PowerShell 3.0 and later) will get only files and not directories.
Of course, remove -WhatIf to actually execute the command.

Related

Copy specific type files from subfolders using a Powershell script

I have many folders, some of which have subfolders. I want to find and copy to special directory all files of one type, for example .txt, from them. I use this script and it works, but it copies all folders on the way to my file too. How can I avoid this? I thought about deleting all empty folders after copying, but I'm sure that the more right way exists.
$folders = [IO.Directory]::GetDirectories("S:\other","*","AllDirectories")
Foreach ($dir in $folders) {
Copy-Item -Path $dir -Filter *.txt -Recurse -Destination 'D:\FinishFolder'
}
Use Get-ChildItem to discover the files, then copy them 1 by 1:
Get-ChildItem S:\other -Filter *.txt -Recurse |Copy-Item -Destination 'D:\FinishFolder'

PowerShell copy-item folder structure is NOT wanted

The folder named "z:\original" has hundreds of sub-folders containing multiple copies of the same .jpg files. I wanted to copy all .jpg files into a folder named "z:\dump" WITHOUT the folder structure and hopefully overwrite most of the copies. I used
copy-Item -path "Z:\original" -filter "*.jpg" -Destination "Z:\dump" -recurse -verbose
but this recreated the structure with the .jpg files. How can I dump all files into a single folder, using PowerShell?
Use combination of dir/Get-ChildItem and Copy-Item with pipeline.
$_.FullName - Full path to jpg file
$_.Name is file name only
Get-ChildItem Z:\original\*.jpg -Recurse | %{Copy-Item $_.FullName "Z:\dump\$($_.Name)"}
You might need to add -Force to overwrite same files

How can I copy all files in a directory matching certain extensions with PowerShell?

I've done something like this before with batch files
copy "%APPPATH%\*.exe" "%APPPATH%\*.exe.deploy"
So I want to copy all .exe files to `.exe.deploy'
So if I have the following in a directory:
a.exe
b.exe
c.foo
d.bar
I want to end up with:
a.exe
b.exe
c.foo
d.exe
a.exe.deploy
b.exe.deploy
d.exe.deploy
There's got to be an elegant way of doing this. BONUS I'd also be like to specify multiple extensions (*.exe, *.txt, *.blob) and do it all in one command.
With PowerShell you'd enumerate the files you want to copy and pipe the results into the Copy-Item cmdlet:
Get-ChildItem $env:APPPATH -Filter *.exe |
Copy-Item -Destination { $_.FullName + '.deploy' }
Note that -Filter supports only a single string. If you want to pass multiple extensions you need to use -Include (but that works only in combination with -Recurse):
Get-ChildItem $env:APPPATH -Include *.exe,*.foo -Recurse |
Copy-Item -Destination { $_.FullName + '.deploy' }

Use PowerShell to copy all files in many folders to a single folder

Copy-Item -Path D:\DB-DNLD\ -Filter *.csv -Destination D:\Dump\CSV -Recurse
I used this command, but it copied all .csv files along with existing folder structures, but my intention is just to copy all CSV to new single destination folder excluding source folder structures.
The Copy-Item cmdlet will maintain the tree structure. If you want to flatten the structure, you can locate the items with Get-ChildItem cmdlet then pipe that to a loop where each file it copied to the destinition folder individually.
dir -Path D:\DB-DNLD\ -Filter *.csv -Recurse | ForEach-Object { Copy-Item $_.FullName D:\Dump\CSV }

Powershell Copy files and folders

I have a PS script which Zips up the previous months logs and names the zip file FILENAME-YYYY-MM.zip
This works
What I now want to do is copy these zip files off to a network share but keeping some of the folder structure. I currently a folder structure similar to the following;
C:\Folder1\
C:\Folder1\Folder2\
C:\Folder1\Folder3\
C:\Folder1\Folder4\Folder5\
There are .zip files in every folder below c:\Folder1
What I want is for the script to copy files from c:\folder1 to \\networkshare but keeping the folder structure, so I should have 3 folders and another subfolder in folder4.
Currently I can only get it to copy the whole structure so I get c:\folder1\... in my \\networkshare
I keep running into issues such as the new folder structure doesn't exist, I can't use the -recurse switch within the Get-ChildItem command etc...
The script I have so far is;
#This returns the date and formats it for you set value after AddMonths to set archive date -1 = last month
$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('MM')
#Set destination for Zip Files
$DestinationLoc = "\\networkshare\LogArchive\$env:computername"
#Source files
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
Copy-Item $SourceFiles -Destination $DestinationLoc\ZipFiles\
Remove-Item $SourceFiles
Sometimes, you just can't (easily) use a "pure PowerShell" solution. This is one of those times, and that's OK.
Robocopy will mirror directory structures, including any empty directories, and select your files (likely faster than a filter with get-childitem will). You can copy anything older than 90 days (about 3 months) like this:
robocopy C:\SourceFiles "\\networkshare\LogArchive\$($env:computername)\ZipFiles" /E /IS /MINAGE:90 *.zip
You can specify an actual date with /MINAGE too, if you have to be that precise.
How about Copy-Item "C:\SourceFiles\" -dest $DestinationLoc\ZipFiles -container -recurse? I have tested this and have found that it copies the folder structure intact. If you only need *.zip files, you first get them, then for each you call Resolve-Path with -Relative flag set and then add the resultant path into Destination parameter.
$oldloc=get-location
Set-Location "C:\SourceFiles\" # required for relative
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
$SourceFiles | % {
$p=Resolve-Path $_.fullname -relative
copy-item $_ -destination "$DestinationLoc\ZipFiles\$p"
}
set-location $oldloc # return back