Expand directory list and copy - powershell

I need to copy the following subset of folders from $sourceDir to $targetDir:
abc0001
abc0643
abc0456
...
The number of folders is unknown, but they all match a pattern abc0*.
Is there an elegant solution to expand abc0* to the actual list of folders and then copy them? I tried this:
dir "$sourceDir\abc0*" -Recurse | copy -Destination $targetDir -WhatIf
But it does not preserve the path, so all files end up in the root of $targetDir.

Give this a try:
dir $sourceDir abc0* | where {$_.psiscontainer} | copy -dest $targetDir -recurse

Related

Get files from specific folders in a directory in PowerShell

I have 4 folders in a directory. All of those folders contains some files. I want to list the file names only from the two folders.
C:\MainFolder\FolderOne\FileOne.txt
C:\MainFolder\FolderTwo\FileTwo.txt
C:\MainFolder\FolderThree\FileThree.txt
C:\MainFolder\FolderFour\FileFour.txt
I only want to list the files under FolderTwo and FolderThree.
If I use -Recurse -Include "FolderNames" it'll list only the folder names.
$source="c:\MainFolder" #location of starting directory
$files=#("*.txt", "*.doc") #if you want to include extensions add -include ($files) to get-ChildItem
Get-ChildItem -recurse ($source) -File | Where-Object {$_.PSParentPath -match "Two|Three"}
get-childitem -recurse 'foldertwo','folderthree'
Did it like this
$arrPath ='C:\MainFolder\FolderTwo','C:\MainFolder\FolderThree'
get-childitem -recurse $arrPath

Delete files from folder structure with exclude

currently I am trying to delete files inside a folder structure
root
|
|__subfolder1 (includes files)
|
|__subfolder2 (includes files)
|
etc
The script has to delete all files inside the subfolders except subfolder1 and not delete the subfolders. The thing I am not getting to work is to exclude the files inside of "subfolder1".
I am trying something like this
Get-ChildItem -Path E:\root -Include *.* -File -Recurse -Exclude E:\root\subfolder1 | foreach {$_.Delete()}
Since the subfolder you want to exclude is always directly under the root folder I'd do the processing in 2 steps:
Enumerate the child folders of the root folder and exclude subfolder1.
Enumerate all files from the remaining folders and delete them.
Something like this:
$root = 'root'
$excludes = 'subfolder1'
Get-ChildItem $root -Directory -Exclude $excludes | ForEach-Object {
Get-ChildItem $_.FullName -File -Recurse -Force | Remove-Item -Force
}

Copying Folders with Wildcards

I am trying to copy a whole bunch of files using Powershell, from one directory to another on my computer.
I used Get-ChildItem C:\Users\Tom\Google Drive\My Files\*\Assessment 1\* to identify that this was the path that I wanted to copy too, and I know about Copy-Item, but I want to maintain parts of the path name when copied.
Example:
If I copy from C:\Users\Tom\Google Drive\My Files\Cool Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\Cool Stuff\Assessment 1
Whereas if I copy from C:\Users\Tom\Google Drive\My Files\New Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\New Stuff\Assessment 1
You could use the Get-ChildItem cmdlet to recursively find all Assessment 1 folders within your base directory and then remove the base path using -replace to finally copy the items using the Copy-Item cmdlet:
$baseDir = 'C:\Users\Tom\Google Drive\My Files\'
$destination = 'C:\Users\Tom\Archive\'
Get-ChildItem $baseDir -directory -Filter 'Assessment 1' -Recurse | ForEach-Object {
$newPath = Join-Path $destination ($_.FullName -replace [regex]::Escape($baseDir))
Copy-Item $_.FullName $newPath -Force -Recurse
}

Copy file with relative path

I would like to copy all files of a certain type from a certain sub-directory with their relative path from that sub-directory to another directory with the relative path intact. e.g.:
Source sub-dir:
c:\temp\sourcedirectory
Source files:
c:\temp\sourcedirectory\tonymontana\fileOne.txt
c:\temp\sourcedirectory\poker\fileTwo.txt
Target dir:
c:\temp\targetdirectory
Desired result:
c:\temp\targetdirectory\tonymontana\fileOne.txt
c:\temp\targetdirectory\poker\fileTwo.txt
So far I've come up with:
Set-Location $srcRoot
Get-ChildItem -Path $srcRoot -Filter $filePattern -Recurse |
Resolve-Path -Relative |
Copy-Item -Destination {Join-Path $buildroot $_.FullName}
However, this "everything is an object" à la PowerShell is beating me down (at least that's what I suspect). I.e. the files gets copied, but without their relative path.
Anyone who could enlighten me a bit?
Don't bother with PowerShell cmdlets for this, simply use robocopy:
robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s
You can try this:
$srcroot = "c:\temp\sourcedirectory"
$builroot= "c:\temp\targetdirectory"
gci -path $srcroot -filter $filepattern -recurse |
% { Copy-Item $_.FullName -destination ($_.FullName -replace [regex]::escape($srcroot),$builroot) }
Try this:
Copy-item $srcRoot -destination $destination -recurse
To prevent copying the folder itself i.e. creating
c:\temp\targetdirectory\sourcedirectory
Change into the source folder, then use a wildcard instead of the folder as the source:
cd C:\temp\sourcedirectory\
Copy-item * -destination c:\temp\targetdirectory -recurse`

Copy-Item / Remove-Item child-content only without root folder?

Hi I'm struggling mightily with the following - suppose I have the following directory structure C:\Temp\Test1 and C:\Temp\Test2
What I'd like to do is recursively copy the child-contents of C:\Temp\Test1 to C:\Temp\Test2 without copying the actual folder C:\Temp\Test1 ..right now if I use the command
Copy-Item C:\Temp\Test1 C:\Temp\Test2 -Recurse
Will result in C:\Temp\Test2\Test1 and no combination of parameters seems to alleviate the problem
Similarly, when I wish to remove all the child content in C:\Temp\Test2 I wish to only delete the child content and not the actual folder eg
Remove-Item C:\Temp\Test2\ -Recurse
Is removing the \Test2 folder. I've tried so many variations of parameters - how can I accomplish what I am trying to do?
Take a look at the get-childitem command. You can use this in the pipeline to copy or remove all items underneath the root folders:
# recursively copy everything under C:\Temp\Test1 to C:\Temp\Test2
get-childitem "C:\Temp\Test1" | % {
copy-item $_.FullName -destination "C:\Temp\Test2\$_" -recurse
}
# recursively remove everything under C:\Temp\Test1
get-childitem "C:\Temp\Test1" -recurse | % {
remove-item $_.FullName -recurse
}
Copy-Item C:\Temp\Test1\* C:\Temp\Test2
Remove-Item "C:\Temp\Test2\*" -recurse
Works too :)