Answer the question
In order to leave comments, you need to log in
How to copy files by mask with the entire file structure?
Good afternoon!
I could not find a solution to the problem, can someone tell me how to copy files in powershell using a mask with all intermediate folders, for example:
- SourceFolder
file1.txt
file1.log
- Subfolder1
file2.txt
file2.log
- SubSubFolder
file3.txt
doc3.txt
When copying all log files should come out:
- SourceFolder
file1.log
- Subfolder1
file2.log
I use the line:
Get-ChildItem $QUIK -filter "*.log" -Recurse | ForEach-Object { Copy-Item $_.fullname -Destination $BackupFolder -Container }
Answer the question
In order to leave comments, you need to log in
As an option:
$TargetFolder = 'F:\temp\test1'
$DestinationFolder = 'F:\temp\test2'
Get-ChildItem $TargetFolder -Recurse |
Where-Object { $_.Name -match '.log$' -and !$_.PsIsContainer } |
Copy-Item -Destination { mkdir $_.DirectoryName.Replace("$TargetFolder","$DestinationFolder") -Force }
foreach {$file = $_; $destination = (join-path $BackupFolder $(Split-Path $file.Directory -NoQualifier)); if (!(test-path $destination)) {New-Item -itemtype Directory -Path $destination}; Copy-Item $file.fullname -Destination $destination -Container}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question