Solved

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

Posted on 2014-10-13
9
265 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 500 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
Problems using Powershell and Active Directory?

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

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

695 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