[英]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? 有什么建议么?
-Directory
-Directory
Copy-Item
accepts input from the pipeline, no ForEach-Object
necessary. Copy-Item
接受来自管道的输入,不需要ForEach-Object
。 -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.