change content in a file list and copy files including folder structure.

Hi all,

I'm looking to read the contents of a text file, change some content, set variables and use that content (a file list) to copy files to a new folder including the folder structure as below: I had to use New-Item to work around not being able to create the folder structure while using Copy-Item.

$Dst - $env:userprofile + '\desktop\copied-files

Get-Content filename.txt|
    Foreach-Object
{
        ($a = '.' + $_ -replace "/", "\"); ($d = $dst + $_ -replace "/", "\")|
        New-Item -ItemType File -Path $d -Force|
        Copy-Item -Include $a -Destination $d -Force
}

This is failing at the copy-item portion. I can run copy-item $a -dest $d manually afterward and it runs perfectly.

the filename.txt is a list of files using linux based '/' which I am converting to '\'

ie: 'filename.txt'
/logs/jan/31/10am.log
/logs/jan/31/1pm.log
/logs/mar/13/3pm.log

copy to '.\desktop\copied-files\logs\lan\31\10am.log', '.\desktop\copied-files\logs\mar\13\3pm.log', etc.

Copy-Item : Cannot copy item C:\Users\Jomal\desktop\deployment\archive-test\weblogfiles\W3SVC2\u_ex140612.log onto itself. At line:1 char:161 + get-content filename.txt|Foreach-Object {($a = '.' + $_ -replace "/", "\"); ($d = $dst + $_ -replace "/", "\")|New-Item -ItemType File -Path $d -Force|copy-it em <<<< -Include $a -Destination $d -Force} + CategoryInfo : WriteError: (C:\Users\Jomal...\u_ex140612.log:String) [Copy-Item], IOException + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Please advise,

Jomal


  • Edited by Jo Mal 17 hours 25 minutes ago
March 25th, 2015 9:43am

I think this:

 Copy-Item -Include $a -Destination $d -Force
 

Should be:

 Copy-Item -Path $a -Destination $d -Force

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 3:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics