Powershell复制目录-使用CSV列表-源-目标路径 [英] Powershell Copy Directories - Using A CSV Listing - Source - Destination Path

查看:89
本文介绍了Powershell复制目录-使用CSV列表-源-目标路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CSV文件将目录-文件夹和子文件夹复制到另一个位置,该文​​件列出了要复制的每个目录或文件夹的源和目标.

CSV的内容如下:

我已经引用了该线程:

https://serverfault.com/questions/399325/copying-list-of-files-through-powershell

 导入CSV C:\ Users \ WP \ Desktop \ a.csv |foreach {复制项目"$ _.源""$ _.目标"} 

收到错误

  CategoryInfo:ObjectNotFound:(@ {Source = C:String)[Copy-Item],DriveNotFoundException+ FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 

我的另一个问题是,是否要在CSV中复制到目标文件夹中没有存在的文件夹-我可以使用CSV命令Powershell创建该文件夹吗?

谢谢您的建议.

解决方案

如果默认情况下将它们放在双引号中,则PowerShell不会扩展变量并访问变量内对象的属性.仅扩展'$ _'并将'.source'固定在字符串的末尾,因此从外壳程序的角度来看,您的命令类似于 Copy-item"{source = C:\ Users \ WP \ Desktop \ a; Destination = C:\ Users \ WP \ Desktop \ a} .Source" {source = C:\ Users \ WP \ Desktop \ a; Destination = C:\ Users \ WP \ Desktop\ a}.目的地" ,这可能不是您的意思.

这是应该起作用的语法(我还包括了 -Recurse ,以便它也可以复制目录中的项目)

 导入CSV C:\ Users \ WP \ Desktop \ a.csv |foreach {复制项目-Path $ _.Source -Destination $ _.Destination -Recurse} 

注意:如果要访问双引号内的对象的属性,请使用此语法"$($ _.source)" .

I am trying to Copy Directories - folders and Sub folders to another location using a CSV file that lists the source and destination of each directory or folder to be copied.

The Contents of the CSV are as such below:

I have referenced this thread:

https://serverfault.com/questions/399325/copying-list-of-files-through-powershell

 Import-CSV C:\Users\WP\Desktop\a.csv | foreach{Copy-item "$_.Source" "$_.Destination"}

Error Received

CategoryInfo          : ObjectNotFound: (@{Source=C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

The other question I have is if in the CSV I want to copy to a folder that does not exists in the destination - can I use the CSV to command powershell to create the folder?

Thank you for your advice.

解决方案

PowerShell will not expand the variable and access the property of the object inside the variable if you have them placed in double quotes by default. Only the '$_' is being expanded and '.source' is being tacked on to the end of the string, so from the view of the shell, your command looks something like Copy-item "{source=C:\Users\WP\Desktop\a;Destination=C:\Users\WP\Desktop\a}.Source" "{source=C:\Users\WP\Desktop\a;Destination=C:\Users\WP\Desktop\a}.Destination", which is probably not what you mean.

Here is the syntax that should work (I also included -Recurse so that it will copy the items inside the directory as well)

Import-CSV C:\Users\WP\Desktop\a.csv | foreach{Copy-item  -Path $_.Source -Destination $_.Destination -Recurse}

Note: if you want to access the properties on an object inside of double quotes, use this syntax "$($_.source)".

这篇关于Powershell复制目录-使用CSV列表-源-目标路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆