Solved

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

Posted on 2014-10-13
9
261 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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