powershell - Zip files older than 30 days using 7zip -
i know question has been asked several times requirement different have read here , on google.
i have below folder structure.
c:\weblogs\servers\server1 c:\weblogs\servers\server2
server 1 folder may contain folders such as:
w3svc1 w3svc2 w3svc3
server 2 have similar folder structure.
i trying create script @ every w3svc*
folder .log
files older 30 days.
it create zip file called oldlogs-server*-w3svc*.zip
within each such w3svc*
subfolder , move older logs file. on second run move new files older 30 days respective zip file.
i not developer examples or suggestions helpful if there not existing script available.
this script believe comes closest trying achieve:
it throws error:
"\webapps\logharvest\pkzipc.exe needed"
# alias pkzip if (-not (test-path "$env:f:\webapps\logharvest\pkzipc.exe")) { throw "$env:f:\webapps\logharvest\pkzipc.exe needed" } set-alias sz "$env:webapps\logharvest\pkzipc.exe" ############################################ #### variables $filepath = "f:\weblogs\test" $log = get-childitem -recurse -path $filepath | where-object { $_.extension -eq ".log" } ########### end of varables ################## foreach ($file in $log) { $name = $file.name $directory = $file.directoryname $zipfile = $name.replace(".log",".7z") sz -t7z "$directory\$zipfile" "$directory\$name" } ########### end of script ##########
if (-not (test-path "$env:f:\webapps\logharvest\pkzipc.exe")) { throw "$env:f:\webapps\logharvest\pkzipc.exe needed" } set-alias sz "$env:webapps\logharvest\pkzipc.exe"
you're testing path $env:f:\webapps\logharvest\pkzipc.exe
, evaluates \webapps\logharvest\pkzipc.exe
, since don't have environment variable $env:f
. because of script looking executable on current drive, c:
rather f:
. causes error observed.
the next statement tries set alias $env:webapps\logharvest\pkzipc.exe
. if have environment variable $env:webapps
change code this:
$pkzip = "$env:webapps\logharvest\pkzipc.exe" if (-not (test-path $pkzip)) { throw "$pkzip needed" } set-alias sz $pkzip
if don't have environment variable change code this:
$pkzip = "f:\webapps\logharvest\pkzipc.exe" if (-not (test-path $pkzip)) { throw "$pkzip needed" } set-alias sz $pkzip
as side note: instead of creating alias script i'd use variable executable call operator (&
):
& $pkzip -t7z "$directory\$zipfile" "$directory\$name"
also, i'm not sure if pkzipc
can handle 7z archives. or if supports arguments a -t7z
in first place. arguments 7zip programs me.
Comments
Post a Comment