简体   繁体   English

使用PowerShell v5,搜索特定的文件夹,将这些文件夹/文件复制到其他驱动器的路径中

[英]Using PowerShell v5, search for specific folder(s) copy those folders/files, AND the paths to another drive

I'm new to PowerShell. 我是PowerShell的新手。 Spent several days trying to get this, think I am very close, but need an expert to show me the final step. 花了几天时间试图做到这一点,认为我已经很接近了,但是需要专家向我展示最后一步。

Using PowerShell v5, Need to search for folder(s) which match specific name, then copy those folders and their files, AND the path to the Folders to another drive. 使用PowerShell v5,需要搜索与特定名称匹配的文件夹,然后将这些文件夹及其文件以及文件夹的路径复制到另一个驱动器。

Script now: 现在脚本:

Get-ChildItem s:\ -Filter (Read-Host -Prompt "Enter") -Recurse -ErrorAction SilentlyContinue -Force | 
    Select-Object FullName | ForEach-Object {Copy-Item -Path $_.FullName -Destination 'C:\Robotics\AA\ConfigFiles\Automation Anywhere Files\Automation Anywhere\My Tasks\' -recurse -Force}

This does a search for the Folder, returns results and then copies the folder and contents into the destination location. 这将搜索文件夹,返回结果,然后将文件夹和内容复制到目标位置。

The problem is, I actually needed the source path appended to my destination path. 问题是,我实际上需要将源路径附加到我的目标路径。 Source is variable number of folders deep. 来源是可变数量的文件夹。

Any suggestions? 有什么建议么?

  • To limit the Get-ChildItem to folders I inserted -Directory 要将Get-ChildItem限制为我插入的文件夹-Directory
  • Copy-Item accepts input from the pipeline, no ForEach-Object necessary. Copy-Item接受来自管道的输入,不需要ForEach-Object
  • the -replace is RegEx based and requires the backslash in the path S:\\ to be escaped by another backslash S:\\\\ . -replace基于RegEx,并且要求路径S:\\中的反斜杠由另一个反斜杠S:\\\\进行转义。

$DestBase = 'C:\Robotics\AA\ConfigFiles\Automation Anywhere Files\Automation Anywhere\My Tasks\'
$Search = Read-Host -Prompt "Enter folder:"
Get-ChildItem -Path S:\ -Filter $Search -Directory -Recurse -ErrorAction SilentlyContinue -Force | 
    Copy-Item -Destination {$($_.Fullname) -replace 'S:\\',"$DestBase"} -Recurse -Force -Whatif

If the output looks OK, remove the -WhatIf paramter in the last line. 如果输出看起来正常,请在最后一行删除-WhatIf参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 DOS(命令提示符)将所有文件和文件夹从一个驱动器复制到另一个驱动器 - copy all files and folders from one drive to another drive using DOS (command prompt) 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell 在SVN中的目录中搜索具有特定文件扩展名的文件,然后复制到另一个文件夹? - Search directory in SVN for files with specific file extension and copy to another folder? 复制没有文件的文件夹,没有文件夹的文件或使用PowerShell的所有文件 - Copy folders without files, files without folders, or everything using PowerShell 如何使用Powershell将这些文件备份到特定文件夹中 - How to backup these files into specific folders using powershell 将文件或文件夹链接到另一个驱动器 - Linking files or folders to another drive 将文件从文件夹和子文件夹复制到另一个文件夹并保存文件夹的结构 - Copy files from folders and sub-folders to another folder and saving the structure of the folders Applescript将文件复制到特定文件夹 - Applescript copy files to specific folders 如何使用php将多个文件从一个文件夹复制到另一个具有特定文件名的文件夹? - How to copy multiple files from one folder to another with specific file names using php? 在 Unix/Linux 中将特定文件从一个文件夹复制到另一个文件夹 - Copy specific files from one folder to another in Unix/Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM