?
Solved

How do you copy a text file list of files from one directory to another in powershell?

Posted on 2014-10-13
9
Medium Priority
?
266 Views
Last Modified: 2014-10-23
How do you copy a text file list of files from one directory to another in powershell?
I have a text file:
c:\temp\LOF.txt
It has a list of files and their directories:
C:\test1.txt
C:\test2.txt
C:\test3.txt

I need to copy this list to another destination:
C:\tempfiles\

And i need to do it in powershell, what is the easiest way to do this?
0
Comment
Question by:josephh610
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40378815
$dest = "c:\tempfiles"
gc c:\temp\lof.txt | % {copy $_ $dest}

Open in new window

0
 

Author Comment

by:josephh610
ID: 40378857
Is there a way to add the destination in the text file like:
c:\test1.txt|c:\tempflies1
C:\test2.txt|c:tempfiles2

and have it add that to the copy?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40380294
I am not sure what you are asking:

Are you asking to create a new text file showing original path and new destination ?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:josephh610
ID: 40380583
I just figured out that i need to grab both the source and target from the test file.
So if in my text file I have the source and target and i need to grab those for the copy command.
example:
    Source       Target
c:\test1.txt|c:\tempflies1
C:\test2.txt|c:tempfiles2

So it would copy:
C:\test1.txt to c:\tempflies1

what would that code look like?
0
 

Author Comment

by:josephh610
ID: 40380660
Or maybe use a -> to divide the source and target.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40384907
Sorry was not monitoring.

You can probably just save this as a csv:

    Source       Target
c:\test1.txt|c:\tempflies1
C:\test2.txt|c:tempfiles2


import-csv file.csv -delimiter "|" | % {
	copy $_.source $_.target
}

Open in new window

0
 

Author Comment

by:josephh610
ID: 40387680
I get this when i use this code.

Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:58
+ import-csv C:\temp\filelist.csv -delimiter "|" | % {copy $_.source $_.target}
+                                                          ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
0
 
LVL 29

Expert Comment

by:becraig
ID: 40387689
your header in the csv should share the same delimiter:

    Source|Target
c:\test1.txt|c:\tempflies1
C:\test2.txt|c:tempfiles2
import-csv file.csv -delimiter "|" | % {	copy $_.source $_.target}

Open in new window

                                         

or you can not have a header
c:\test1.txt|c:\tempflies1
C:\test2.txt|c:tempfiles2

and call it this way:
 import-csv -delimiter "|" -Header Source,Target file.csv | % {	copy $_.source $_.target}

Open in new window

0
 

Author Comment

by:josephh610
ID: 40400660
Thank you! that was perfect.
Cheers!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question