Link to home
Start Free TrialLog in
Avatar of josephh610
josephh610

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of josephh610
josephh610

ASKER

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?
I am not sure what you are asking:

Are you asking to create a new text file showing original path and new destination ?
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?
Or maybe use a -> to divide the source and target.
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

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

Thank you! that was perfect.
Cheers!