开发者

Trying to copy a group of files contained in a text file

开发者 https://www.devze.com 2023-04-10 03:37 出处:网络
I\'m trying to copy a list of files from a txt file and as a newbie, I\'m having a hard time. Here is a bit of the text file. The real file has no extra lines, but I had to do that to :

I'm trying to copy a list of files from a txt file and as a newbie, I'm having a hard time.

Here is a bit of the text file. The real file has no extra lines, but I had to do that to :

"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"

I've tried enclosing the filename string in double-quotes as well.

Here's the simple script I'm trying to use:

Get-Content c:\users\scripts\files-to-fix.txt | Foreach-Object {copy-item $_ d:\junk}

The error I'm getting is:

Copy-Item : Cannot find drive. A drive with the name ''D' does not exist. At C:\users\mhyman\scripts\copyfiles.ps1:2 char:81 + Get-Conte开发者_开发技巧nt 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


I know this is simple, but I would really appreciate some help.


I think it is the surrounding quotes that are causing the problem ( as indicated by the error saying that a drive of name "D is not found. Try this:

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

Of course, if you can control the txt file, enter the list without the quotes.


By your tags and drive letters and backslashes it is clearly a Windows environment your working in and although I'm not a PowerShell scripter, I'm a better than most batch scipter and use a For / If conditioanla statement sicne it is shorter and you feed it your file instead of parsing out the file into reduudc commands on a line, so in your example:

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

And then you go home and never worry about until the next morning Does powershell have a runas ornative mode that can parse older, more proven and stable DOS commands ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号