0

I want to copy list of files specified in text file to list of destination by creating directory. I have one Excel file in which there are 2 columns, source and destination, each source has a destination.

Example Source:

\\10.0.0.1\share\abd.nsf
\\20.0.0.1\share\red.nsf

Example Dest:

\\10.0.0.2\share\abd\
\\10.0.0.2\share\red\

Currently I am using below code, but involves n number of lines so it is tedious.

copy-item "\\10.0.0.1\share\abd.nsf" -destination (New-item "\\10.0.0.2\share\abd\" -Type container -force) -force
copy-item "\\20.0.0.1\share\red.nsf" -destination (New-item "\\10.0.0.2\share\red\" -Type container -force) -force
8
  • Not quite sure what the question is here. Are you looking for logic that moves all nsf files into their own folders? Commented Dec 29, 2015 at 2:08
  • i am looking for a logic to take backup of resigned user nsf files to user_name folder Commented Dec 29, 2015 at 2:35
  • i can copy list of files to common destination, but i want to copy it in username folders Commented Dec 29, 2015 at 2:37
  • How do you determine which folder to copy to? Can you show an example from your source file? is it just "\\10.0.0.1\share\abd.nsf" Commented Dec 29, 2015 at 2:37
  • I didn't get your question; i have multiple sources like have specified in example, want to copy files to specific destination; each source file will have specific destination, Commented Dec 29, 2015 at 2:59

2 Answers 2

1

This is a rather simple one. Make your Excel file a 2 column csv where the columns are labelled Source and Destination. You don't have to use those just know that if yours differ that you need to adjust the properties called in the code.

$path = "c:\path\to\file.csv"
$jobs = Import-CSV $path
$jobs | ForEach-Object{
    # Check if the target folder exists. If not create it.
    If(-not (Test-Path -PathType Container $_.destination)){New-item "$_.destination" -Type Container -Force}

    # If the source file exists copy it to the destination directory. 
    If(Test-Path $_.Source){Copy-Item $_.Source $_.Destination -Force}
}

Creates the destination directory if it does not exist (It is possible the path is still wrong but its better than nothing.). Then copy the file, assuming it also exists, to the destination.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Matt the script is working fine, and this was main requirement
1
$source = get-content c:\source.txt
$destination = get-content c:\destination.txt
$Count= $source.Count
$i = 0

For($i -eq 0;$i -Lt $count; $i++){
Try{new-item -itemtype directory $destination[$i]}
Catch {}
Finally{copy-item $source[$i] $destination[$i]}
}

Source and destination in text file should be in same order

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.