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?
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?
ASKER
$sourcepaths = gci -Recurse $archieve | where {$_.CreationTime -le $time}
($sourcepaths.fullname).replace("C:\Archieve","$topath")
This works, but i still out of idea... how do i copy this?
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?
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.
ASKER
C:\test\server1\log would be best option.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
:) And if I want to use powershell actions to do it? not robocopy :)
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.
$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.
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...
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...
ASKER
Open in new window
Now I tried to replace the path string to other, well error, patter c:\archieve is not valid?