Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

powershell script to copy file and folder from txt file path

hi,

i need a script to copy file and folder.

i have 2 txt file:

"source.txt" in this form:

D:\migration\users\jsmith
D:\migration\users\bwillis
D:\migration\users\wsmith

"destination.txt" in this form
D:\users\john.smith
D:\users\bruce.willis
D:\users\will.smith

i need to copy all folder in line 1 path in "source.txt" in the path at line 1 in "destination.txt" fodler.

for exemple copy all folder and file in :

D:\migration\users\jsmith to D:\users\john.smith
D:\migration\users\bwillis to D:\users\bruce.willis
D:\migration\users\wsmith to D:\users\will.smith
..................

very important:
i need all copied folder to inherit from destination folder security.
i need to copy all file (in certain case, the file have many caracter and cannot be copied)

thanks for help
Avatar of footech
footech
Flag of United States of America image

Try this.  Copied items should get their permissions through inheritance.
$src = Get-Content c:\temp\sources.txt
$dest = Get-Content c:\temp\destinations.txt
for ($i=0;$i -lt $src.count;$i++)
{
  Copy-Item -path $src[$i] -destination $dest[$i] -recurse -force
}

Open in new window

I don't understand your last statement.  
i need to copy all file (in certain case, the file have many caracter and cannot be copied)
Avatar of cawasaki
cawasaki

ASKER

Ok i will test this.

you can forget my last statment.

if its more simple i can use only one txt file contan source and destnation like ths:

source,destination
D:\migration\users\jsmith,D:\users\john.smith
D:\migration\users\bwillis,D:\users\bruce.willis
D:\migration\users\wsmith,D:\users\will.smith

thanks
Not only is combining the items in one file simpler, it is safer.  You can know that all your elements match correctly.  'get-help import-csv -full' will give you all you need to know on working with comma delimited data in powershell.
hi,

@footech:  its not work nothing happen
when i use just this command for only one source and one destination it work:

$src = Get-Content c:\temp\sources.txt
$dest = Get-Content c:\temp\destinations.txt

Copy-Item -path $src -destination $dest -recurse -force
You may try this..
$Source = C:\source.txt
$Dest = C:\dest.txt
Foreach ($s in $Source) {
$Dest | % {
Copy-Item -path $s -destination $_ -recurse -force
  }
}

Open in new window

Or with CSV..
Import-csv C:\input.csv | % {
Copy-Item -path $_.source -destination $_.destination -recurse -force
}

Open in new window

If the permissions are not same in destination folder then you may need to use Xcopy to copy files..
@subsun, hi and thanks.

i have an error with first script:

Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\script\copie.ps1:5 char:16
+ Copy-Item -path <<<<  $s -destination $_ -recurse -force
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCom
   mand

i have a destinations.txt file opened after this error
for second script, wish form of input.csv file i need?
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India 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
ok i have test the second script and it work very good.

van you help me with a code to delete all folder and file in specific location from txt or csv file:

the txt file like this:

D:\migration\users\jsmith
D:\migration\users\bwillis
D:\migration\users\wsmith

delete allfolder and subbfolder and file ??

thanks
Try..
GC C:\source.txt | % {
Remove-Item -Recurse -Force $_ -WhatIf
}

Open in new window

PS : -WhatIf is added for testing..
ok it work but it need do not delete parent folder, for exemple:

for D:\migration\users\jsmith, i need to delete all filde and subfolder of jsmith folder without delete jsmith folder.

thanks
GC C:\source.txt | % {
GCI $_ | Remove-Item -Recurse -Force -WhatIf
}

Open in new window

thanks all work very good :)