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
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
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
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\j ohn.smith
D:\migration\users\bwillis ,D:\users\ bruce.will is
D:\migration\users\wsmith, D:\users\w ill.smith
thanks
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:\migration\users\bwillis
D:\migration\users\wsmith,
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.
ASKER
hi,
@footech: its not work nothing happen
@footech: its not work nothing happen
ASKER
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
$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
}
}
Or with CSV..Import-csv C:\input.csv | % {
Copy-Item -path $_.source -destination $_.destination -recurse -force
}
If the permissions are not same in destination folder then you may need to use Xcopy to copy files..
ASKER
@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], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nErrorNull NotAllowed ,Microsoft .PowerShel l.Commands .CopyItemC om
mand
i have a destinations.txt file opened after this error
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], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
mand
i have a destinations.txt file opened after this error
ASKER
for second script, wish form of input.csv file i need?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
}
PS : -WhatIf is added for testing..
ASKER
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
for D:\migration\users\jsmith,
thanks
GC C:\source.txt | % {
GCI $_ | Remove-Item -Recurse -Force -WhatIf
}
ASKER
thanks all work very good :)
Open in new window
I don't understand your last statement.