复制文件,包括其相对路径

发布于 2024-10-17 00:54:50 字数 517 浏览 2 评论 0原文

我需要将大量文件复制到备份文件夹,但我想保留它们的相对路径。我只需要特定的文件;即

C:\scripts\folder\File.ext1
C:\scripts\folder2\file2.ext2
C:\scripts\file3.ext1

但我只需要像这样复制 ext1 文件:

C:\backup\folder\File.ext1.bak
C:\backup\file3.ext1.bak

源路径有多个深度。 这就是我必须复制文件的内容:

$files = gci -path C:\scripts\ -recurse -include *.ext1 
$files | % { Copy-Item $_ "$($_).bak"; move-item $_ -destination C:\backup\ }

这只是将所有文件转储到 C:\backup\ 中,并且似乎没有获取任何路径。不确定该部分将如何完成。

I need to copy a large number of files to a backup folder but I want to maintain their relative paths. I only need specific files; i.e.

C:\scripts\folder\File.ext1
C:\scripts\folder2\file2.ext2
C:\scripts\file3.ext1

But I only need to copy the ext1 files like so:

C:\backup\folder\File.ext1.bak
C:\backup\file3.ext1.bak

The source paths are of multiple depths.
This is what I have to copy the files:

$files = gci -path C:\scripts\ -recurse -include *.ext1 
$files | % { Copy-Item $_ "$($_).bak"; move-item $_ -destination C:\backup\ }

This just dumps all the files into C:\backup\ and does not appear to get any of the paths. Not sure how that part would be done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

注定孤独终老 2024-10-24 00:54:50

像这样的东西可能会起作用:

gci -path C:\scripts\ -recurse -include *.ext1 | 
  % { Copy-Item $_.FullName "$($_.FullName).bak"
      move-item $_.FullName -destination ($_.FullName -replace 'C:\\scripts\\','C:\backup\') }

它并不聪明,但它很快而且很容易。脏,而且不需要太多努力就能工作。

Something like this could work:

gci -path C:\scripts\ -recurse -include *.ext1 | 
  % { Copy-Item $_.FullName "$($_.FullName).bak"
      move-item $_.FullName -destination ($_.FullName -replace 'C:\\scripts\\','C:\backup\') }

It is not clever, but it's quick & dirty and works without a lot of effort.

习ぎ惯性依靠 2024-10-24 00:54:50

get-childitem 返回绝对路径,但您可以使它们相对于当前工作目录,如下所示:

resolve-path -relative

因此,要将经过过滤的文件集从当前目录递归复制到目标目录:

$dest = "c:\dest"
$filter = "*.txt"

get-childitem -recurse -include $filter | `
    where-object { !$_.PSIsContainer } | `
    resolve-path -relative | `
    % { $destFile = join-path $dest $_; new-item -type f $destFile -force | out-null; copy-item $_ $destFile; get-item $destfile; }

需要 new-item 来创建父目录

get -item 提供它创建的所有新文件的显示

当然 robocopy 可以完成所有这些,但有时您想要进行更特殊的过滤或文件名修改...

get-childitem returns absolute paths, but you can make them relative to the current working directory as follows:

resolve-path -relative

So to copy a filtered set of files from the current directory recursively to a destination directory:

$dest = "c:\dest"
$filter = "*.txt"

get-childitem -recurse -include $filter | `
    where-object { !$_.PSIsContainer } | `
    resolve-path -relative | `
    % { $destFile = join-path $dest $_; new-item -type f $destFile -force | out-null; copy-item $_ $destFile; get-item $destfile; }

new-item is needed to create the parent directories

get-item provides a display of all the new files it created

Of course robocopy does all this, but there will be times when you want to do more special filtering or filename mangling...

寻找一个思念的角度 2024-10-24 00:54:50

使用机器人复制。

robocopy c:\scripts c:\backup *.ext1 /s

哎呀。我没有注意到您也想添加 .bak 扩展名。我仍然认为使用 robocopy 复制文件是个好主意:

dir c:\backup -recurse -include *.ext1 | % { ren $_ "$_.bak" }

Use robocopy.

robocopy c:\scripts c:\backup *.ext1 /s

Oops. I failed to notice you wanted to add the .bak extension too. I still think it is a good idea to use robocopy to copy the files then:

dir c:\backup -recurse -include *.ext1 | % { ren $_ "$_.bak" }
如日中天 2024-10-24 00:54:50

您可以尝试使用

Clear-Host
$from = "'C:\scripts\"
$to = "'C:\backup\"
$inc = @('*.ext1', '*.extx')
$files = get-childItem  -path $from -include $inc -Recurse
$files | % {$dest = (Join-Path $to $($_.FullName+".bak").SubString($from.length)); $dum = New-Item -ItemType file $dest -Force; Copy-Item -Path $_ -Destination $dest  -Recurse -Force } 

新项目来强制创建路径。

让·保罗

You can try this

Clear-Host
$from = "'C:\scripts\"
$to = "'C:\backup\"
$inc = @('*.ext1', '*.extx')
$files = get-childItem  -path $from -include $inc -Recurse
$files | % {$dest = (Join-Path $to $($_.FullName+".bak").SubString($from.length)); $dum = New-Item -ItemType file $dest -Force; Copy-Item -Path $_ -Destination $dest  -Recurse -Force } 

the new-item is there in order to force path creation.

Jean Paul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文