【问题标题】:Trying to copy a group of files contained in a text file试图复制包含在文本文件中的一组文件
【发布时间】:2011-10-03 22:53:40
【问题描述】:

我正在尝试从 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}

我得到的错误是:

复制项:找不到驱动器。名为“D”的驱动器不会 存在。在 C:\users\mhyman\scripts\copyfiles.ps1:2 char:81 + 获取内容 c:\users\mhyman\scripts\files-to-fix.txt | Foreach 对象 {复制项


我知道这很简单,但我非常感谢一些帮助。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我认为是引起问题的原因是周围的引号(如错误所示,找不到名称为 "D 的驱动器。试试这个:

    get-content c:\users\scripts\files-to-fix.txt | %{ copy-item $_.trim('"') d:\junk}
    

    当然,如果你可以控制txt文件,输入不带引号的列表。

    【讨论】:

      【解决方案2】:

      通过您的标签、驱动器号和反斜杠,这显然是您在其中工作的 Windows 环境,虽然我不是 PowerShell 脚本编写者,但我比大多数批处理 scipter 更好,并使用 For / If conditioanla 语句 sicne更短,您将文件提供给它,而不是将文件解析为一行中的 reduudc 命令,因此在您的示例中:

      for /F %%t in (the text file.txt) do copy /q %%t  d:\junk
      

      然后你就回家了,直到第二天早上才担心 powershell 是否有 runas ornative 模式可以解析更旧、更成熟和稳定的 DOS 命令?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        • 2012-05-14
        • 1970-01-01
        相关资源
        最近更新 更多