Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

Copy item powershell script

Dear experts

Iam looking for a solution to copy files and folders to specific place with the same filename and same folder names, my code of working in progress so far:
[code]
$archieve = @()
$files = @()
$a = @()
$standardpath = @()
$topath = 'c:\test\'
$time = (Get-Date).AddDays(-20)
$archieve = 'C:\archieve\*\log'
$sourcepaths = gci -Recurse $archieve | where {$_.CreationTime -le $time}
foreach ($path in $sourcepaths) {
Copy-Item $path.FullName -Destination $topath -Recurse
} [/code]

Above code is not working, now the $archieve folders looks like this:
C:\archieve\server1\log
C:\archieve\server2\log
C:\archieve\server3\log
For exemple, (Servername can be complex like: "Fewgws-92" nvm mind that, I changed them for easier understanding.)
I want those folder items copy to $topath like this:
C:\test\archieve\server1\log etc etc
now I tried, substring to cut the string out: "archieve\server1\log" with substring (2, 17) but this wouldnt work, the lenght of the servername folder are diffirent, its impossible to use substring... I have no idea how I can make this work...

Any ideas?
Avatar of WeTi
WeTi

ASKER

$archieve = @()
$files = @()
$a = @()
$standardpath = @()
$topath = 'c:\test\'
$time = (Get-Date).AddDays(-20)
$archieve = 'C:\archieve\*\log'
$sourcepaths = gci -Recurse $archieve | where {$_.CreationTime -le $time}
foreach ($path in $sourcepaths) {
[string]$a= $path.FullName 
$a -replace "C:\archieve\", $topath
}

Open in new window

Now I tried to replace the path string to other, well error, patter c:\archieve is not valid?
Avatar of WeTi

ASKER

$sourcepaths = gci -Recurse $archieve | where {$_.CreationTime -le $time}
($sourcepaths.fullname).replace("C:\Archieve","$topath")

Open in new window

This works, but i still out of idea... how do i copy this?
Avatar of oBdA
In your initial question, you wanted
      C:\test\archieve\server1\log
as target path.
With the "replace" in your later comments, you'd be using
      C:\test\server1\log
as target path.
Which should it be?
Why don't you just use robocopy with /minage or /maxage parameters? You are trying to reinvent the wheel.
Avatar of WeTi

ASKER

C:\test\server1\log would be best option.
Avatar of WeTi

ASKER

robocopy can it do: scanning all folders in the C:\archieve\*\log ? and create a new folder with the name: C:\test\server1\log as it scanned in C:\archieve\*\log?
Im still lost in the action part of the PS script... how should i do it with mkdir? copy-item? or other ways?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Avatar of WeTi

ASKER

:) And if I want to use powershell actions to do it? not robocopy :) 
Avatar of WeTi

ASKER

After testing the robocopy, this is not what Im trying to do... Maybe I was unclear this is what I want:
$archieve array are:
C:\archieve\server1\log
C:\archieve\server2\log
C:\archieve\server3\log

I want to copy to a test folder with same path like:
C:\test\archieve\server1\log
C:\test\archieve\server2\log
C:\test\archieve\server3\log
Now what I meant before was it can be:
C:\test\server1\log
C:\test\server2\log
C:\test\server3\log
But stil the dynamic name of the server still should be there.     
Avatar of WeTi

ASKER

The result of the robocopy:
C:\test\server1\ under there are all the files. This is not what I am after, Im after:
C:\test\server1\log\, C:\test\server2\log\ etc
Sorry that I was unclear before...