Solved

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

Posted on 2014-10-13
9
257 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 28

Accepted Solution

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

Open in new window

0
 

Author Comment

by:josephh610
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
I am not sure what you are asking:

Are you asking to create a new text file showing original path and new destination ?
0
 

Author Comment

by:josephh610
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:josephh610
Comment Utility
Or maybe use a -> to divide the source and target.
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
Thank you! that was perfect.
Cheers!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now