Powershell copy file after a date has passed with file structure -


i trying copy file off server , onto another, want keep structure of file c:\folder\folder\file! if folder there copy file it, if not create folders , copy it! filter out files still needed want keep files 30 days , move them!

blockquote

`[int]$count = 0   $filter = (get-date).adddays(-15).tostring("mm/dd/yyyy")  get-wmiobject win32_logicaldisk | foreach-object{  $searchfolders = get-childitem ($_.deviceid + "\crams") -recurse   $filelist = $searchfolders |  where-object {$_.name -like "stdout_*" -and $_.lastwritetime -le $filter}    [int]$totalfiles = ($filelist | measure-object).count   write-host "there total of $totalfiles found."   echo $filelist  start-sleep 30    [int]  foreach ($item in $filelist)   {$count++  $file = $item   write-host "now moving $file"   $destination ="c:\stdlogfiles\"   $path = test-path (get-childitem $destination -exclude "stdout_*")    if ($path -eq $true) {     write-host "directory exists"     copy-item $file -destination $destination    }     elseif ($path -eq $false) {          cd $destination          mkdir $file          copy-item  $file -destination  $destination       }   } }` 

is have far has changed lot due trying work search works , date part can not keep structure of file!

okay took out bottom part , put in foreach ($item in get-childitem $filelist) tried get-content path null {$count++ $destination = "c:\stdlogfiles" $file = $item write-host "now moving $file $destination" copy-item -path $file.fullname -destination $destination -force}} copying in c folder not files not understand doing now! had copying files wen older version , can't work again! going leave before break more!

any or thoughts appreciated

i think robocopy simpler solution honest. but, if insist on using powershell going need setup destination better if want keep file structure. want leave filter date [datetime] object instead of converting string since comparing (lastwritetime) [datetime] object. you'll need like:

$filter = (get-date).adddays(-15) $filelist = get-wmiobject win32_logicaldisk | foreach-object{     get-childitem ($_.deviceid + "\crams") -recurse  | where-object {$_.name -like "stdout_*" -and $_.lastwritetime -le $filter}  }  $totalfiles = $filelist.count   for($i = 1;$i -le $totalfiles; $i++)  {     $file = $filelist[($i-1)]     write-progress -activity "backing old files" -currentoperation ("copying file: " + $file.name) -status "$i of $totalfiles files" -percentcomplete ($i*100/$totalfiles)     $destination = (split-path $file.fullname) -replace "^.*?\\crams", "c:\stdlogfiles"     if(!(test-path $destination)){         new-item -path $destination -itemtype directory | out-null     }     copy-item $file -destination $destination } write-progress -completed 

that gathers files need move disks. takes count of them, , enters loop cycle many times have files. in loop assigns current item variable, updates progress bar based on progress. parses destination replacing beginning of file's full path (minus file name) target destination of 'c:\stdlogfiles'. d:\crams\holypregnantnunsbatman\stdout04122015.log becomes c:\stdlogfiles\holypregnantnunsbatman. tests path, , if it's not valid creates (piped out-null avoid spam). copy file destination , move on next item. after files done close out progress bar.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -