AlamedaDad
AlamedaDad

Reputation: 31

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

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


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

Upvotes: 3

Views: 4781

Answers (2)

user1947219
user1947219

Reputation: 1

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 ?

Upvotes: -1

manojlds
manojlds

Reputation: 301147

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.

Upvotes: 6

Related Questions