I have a script below that moves a file from one directory to another based on the file age. The script then compresses the file and removes the .pdf from the directory. The problem I am having is that the if there is no .pdf to move it searches some local directories and tries to remove files if a .pdf is not old enough. What I would like to do is check and see if there is a file to move. If there is then proceed. If not end the script.
#Mailstream/CASSRPT & COMPRESSION SCRIPT #move the files to Archive $date = (get-date).AddDays(-5) get-childitem \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\*.pdf | where-object {$_.LastWriteTime -lt $date} | move-item -Destination \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive -force # Alias for 7-zip if (-not (test-path "$env\\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive\7-zip")) {throw "$env\\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive\7-Zip\7z.exe needed"} set-alias sz "$env\\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive\7-Zip\7z.exe" #### Variables $filePath = "\\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive" $pdf = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".pdf" } ########### END of VARABLES foreach ($file in $pdf) { $name = $file.name $directory = $file.DirectoryName $zipfile = $name.Replace(".pdf",".zip") sz a -tzip "$directory\$zipfile" "$directory\$name" } Remove-item \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive\*.pdf* -force $date = (get-date).AddDays(-366) get-childitem \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\COMPRESSION_TESTING\*.zip | where-object {$_.LastWriteTime -lt $date} | Remove-item \\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\COMPRESSION_TESTING\Archive -force #$doirunit = (Get-ChildItem "\\csg.csgsystems.com\omafiles\SPC-ProductionDatabases\Mailstream").Count # if ($doirunit -ge 0) {exit}
↧
Check for a file before running
↧