none
Check for a file before running RRS feed

  • Question

  • 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}


    Tuesday, July 8, 2014 6:12 PM

Answers

  • Keep in mind that it's unlikely that someone is going to copy and paste your code, make all the appropriate edits to paths and such, try to understand all of your code in order to fix a bug, etc. (We're volunteers, after all.)

    I recommend starting with a very specific question and short, self-contained, sample code that demonstrates the problem you're having. You also need to tell the exact error(s) you're getting, if any (copy and paste is best).


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by goosebags Tuesday, July 8, 2014 7:55 PM
    Tuesday, July 8, 2014 6:19 PM
    Moderator

All replies

  • Keep in mind that it's unlikely that someone is going to copy and paste your code, make all the appropriate edits to paths and such, try to understand all of your code in order to fix a bug, etc. (We're volunteers, after all.)

    I recommend starting with a very specific question and short, self-contained, sample code that demonstrates the problem you're having. You also need to tell the exact error(s) you're getting, if any (copy and paste is best).


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by goosebags Tuesday, July 8, 2014 7:55 PM
    Tuesday, July 8, 2014 6:19 PM
    Moderator
  • Thanks for the tip would recommend starting over or just replying to this post string?
    Tuesday, July 8, 2014 6:33 PM
  • Thanks in advance for the help. I would like to check to see if there are any .pdf files older than five days in a directory before running the script. If there is a file older than 5 days then proceed with script. If there are no files older than 5 days then terminate. Below is a sample of my script this far.

    #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
    


    • Edited by goosebags Tuesday, July 8, 2014 7:05 PM
    Tuesday, July 8, 2014 6:41 PM