R
R
RedTea2019-06-28 12:08:37
PowerShell
RedTea, 2019-06-28 12:08:37

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 }

It adds all the logs from subfolders, but only adds to the root.
How is it possible to copy the logs with the file infrastructure where they lie?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Roman Ratkin, 2019-06-28
@Hanharr

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 }

E
Eugene, 2019-06-29
@yellowmew

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}

K
Konstantin Tsvetkov, 2019-06-28
@tsklab

ROBOCOPY

R
res2001, 2019-06-28
@res2001

xcopy

D
Diman89, 2019-06-28
@Diman89

Total Commander
when you press F5 write the exact file name (if it is the same in all directories), or a mask

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question