get childitem - use powershell to copy files and folders to archive folder older 30 minutes -
i want copy files on 30 minutes old live folder archive folder , retain folder structure using powershell. have command far although moves files , folders puts files in top level of destination.
get-childitem -recurse -path "c:\input folder" | where-object {$_.creationtime -lt (get-date).addminutes(-90)} | copy-item -destination "e:\output archive" so if source looks -
c:\input folder\sub1\filea.txt
c:\input folder\sub2\fileb.txt
c:\input folder\sub3\filec.txt
it looks command in destination wrong want retain folder structure -
e:\output archive\sub1\
e:\output archive\sub2\
e:\output archive\sub3\
e:\output archive\filea.txt
e:\output archive\fileb.txt
e:\output archive\filec.txt
it should -
c:\output archive\sub1\filea.txt
c:\output archive\sub2\fileb.txt
c:\output archive\sub3\filec.txt
what missing command?
you need preserve partial path relative source folder when copying files. copy statements don't copy file whatever (source) subfolder destination folder.
replace source folder part in full name of each copied item destination folder:
$src = 'c:\input folder' $dst = 'e:\output archive' $pattern = [regex]::escape($src) $refdate = (get-date).addminutes(-90) get-childitem -recurse -path $src | where-object { $_.creationtime -lt $refdate } | copy-item -destination { $_.fullname -replace "^$pattern", $dst }
Comments
Post a Comment