我正在尝试从txt文件复制文件列表,作为新手,我很难过。
这是一些文本文件。真实文件没有额外的行,但我必须这样做:
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Fred 44705"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Johnson 47227"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Daniel 35434"
"D:\Shared\Customer Care\Customer Care Common\Customers Contracted\Customers Contracted\Frank, John 48273"
我也尝试用双引号括起文件名字符串。
这是我正在尝试使用的简单脚本:
Get-Content c:\users\scripts\files-to-fix.txt | Foreach-Object {copy-item $_ d:\junk}
我得到的错误是:
Copy-Item:找不到驱动器。名为“D”的驱动器不会 存在。在C:\ users \ mhyman \ scripts \ copyfiles.ps1:2 char:81 + Get-Content c:\ users \ mhyman \ scripts \ files-to-fix.txt | Foreach-Object {copy-item<<<< $ _ d:\ junk} + CategoryInfo:ObjectNotFound:('D:String)[Copy-Item], DriveNotFoundException + FullyQualifiedErrorId: DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
我知道这很简单,但我真的很感激一些帮助。
答案 0 :(得分:6)
我认为引起问题的是周围的引号(如错误所示,未找到名称"D
的驱动器。请尝试此操作:
get-content c:\users\scripts\files-to-fix.txt | %{ copy-item $_.trim('"') d:\junk}
当然,如果您可以控制txt文件,请输入不带引号的列表。
答案 1 :(得分:-1)
通过你的标签和驱动器号和反斜杠,它显然是你工作的Windows环境,虽然我不是PowerShell脚本编写者,但我比大多数批处理脚本更好,并使用For / If conditioanla语句sicne它是更短,你把它提供给你的文件,而不是将文件解析成一行的reduudc命令,所以在你的例子中:
for /F %%t in (the text file.txt) do copy /q %%t d:\junk
然后你回家,直到第二天早上再也不用担心 powershell有一个runas ornative模式,可以解析更老,更成熟和稳定的DOS命令吗?