Jump to content
Private Messaging is activated - check "How to" on how to disable it ×

Is there a way to send different reports to different folders


---
 Share

Recommended Posts

I want three different reports for a program that each go to there own folder. 

 

A single part

A table

A trend chart. 

 

Making all three is easy but they all go to the same folder, when I try to change one the rest all update. I have to be missing something simple

Link to comment
Share on other sites

It's probably easiest to create a script for the report_end.bat that moves each report to the specific directory you want them to go. 

 

Link to comment
Share on other sites

Andrew,

This may very well be possible in Calypso, but I don't know how.

Instead, I use a python script to continually monitor a specific folder that acts as a collection spot for results.  The script then moves the files to other folders based on specific text in the name of the pdf file.

Here's a generic version of the same script.  You can modify the target path and output paths fairly easily as well as the text that the script looks for.

The script requires a one-time simple install of a python package named watchdog.  This has worked better for my needs than a batch file and scheduled windows tasks because a batch script cannot actively monitor the folder in real time like the Python script.

To install watchdog, open a cmd window and type:


pip install watchdog


Here's the code for the pdf file organizer.   Currently, this will look for a pdfs with the text "single-file," "table," and "trend" and move them from:

C:\Users\Public\Documents\ZEISS\CALYPSO\workarea\results\

to

C:\Users\Public\Documents\ZEISS\CALYPSO\workarea\results\single-part
C:\Users\Public\Documents\ZEISS\CALYPSO\workarea\results\table
C:\Users\Public\Documents\ZEISS\CALYPSO\workarea\results\trend




Code for File Transfer Python Script:

 

import os
import time
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# Paths
WATCH_FOLDER = r"C:\Users\Public\Documents\ZEISS\CALYPSO\workarea\results"
SINGLE_PART_FOLDER = os.path.join(WATCH_FOLDER, "single-part")
TABLE_FOLDER = os.path.join(WATCH_FOLDER, "table")
TREND_FOLDER = os.path.join(WATCH_FOLDER, "trend")
LOG_FILE = os.path.join(WATCH_FOLDER, "file-transfer-log.txt")

# Ensure destination directories exist
for folder in [SINGLE_PART_FOLDER, TABLE_FOLDER, TREND_FOLDER]:
    os.makedirs(folder, exist_ok=True)

# Logging function
def log_file_transfer(file_name, destination):
    timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
    log_entry = f"[{timestamp}] Moved: {file_name} → {destination}\n"
    with open(LOG_FILE, "a") as log:
        log.write(log_entry)

# Event handler class
class PDFMoveHandler(FileSystemEventHandler):
    def on_created(self, event):
        if event.is_directory:
            return

        file_path = event.src_path
        file_name = os.path.basename(file_path)

        if file_name.lower().endswith(".pdf"):
            if "single-part" in file_name.lower():
                destination = os.path.join(SINGLE_PART_FOLDER, file_name)
            elif "table" in file_name.lower():
                destination = os.path.join(TABLE_FOLDER, file_name)
            elif "trend" in file_name.lower():
                destination = os.path.join(TREND_FOLDER, file_name)
            else:
                return  # Ignore files that do not match criteria

            # Move the file
            try:
                shutil.move(file_path, destination)
                log_file_transfer(file_name, destination)
                print(f"Moved: {file_name} → {destination}")
            except Exception as e:
                print(f"Error moving {file_name}: {e}")

# Watcher function
def start_watcher():
    event_handler = PDFMoveHandler()
    observer = Observer()
    observer.schedule(event_handler, WATCH_FOLDER, recursive=False)
    observer.start()
    print(f"Monitoring folder: {WATCH_FOLDER}")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == "__main__":
    start_watcher()



 

Edited
Link to comment
Share on other sites

Yeah, unfortunately PiWeb reports, which I am assuming you are using, cannot be named individually, only together (ending with "1.pdf", "2.pdf", "3.pdf", etc if you have more than one report based on the ordering).

 

The only way to do this would be by using batch file (named "report_end.bat" stored in the inspection plan folder as Richard mentioned) that will move them. Jeff's idea would work as well, if you or someone else is familiar with program development.

 

I'm not super familiar with python specifically, but I think you could do it in just about any language. The batch file I would think might be a bit easier, though....

 

I might suggest looking through "My Voice". I know this has been mentioned on the phone lines before and for enhancement requests, we usually recommend the customers go there to add their suggestion. It might be there already, but I don't know for certain.

Edited
Link to comment
Share on other sites

Please sign in to view this quote.

ChatGPT is your friend. Lol. 

I never got into coding/scripting until much later in life, so while I know how to use Google to help find what I want to do, it's never going to be exactly what I want implemented, so being able to send a request, have GPT give me code (and break it down), and give feedback on changes is pretty invaluable. 

I've expanded a lot of my knowledge on scripting by just asking GPT and learning from it - it isn't perfect, but it's pretty good. 

Link to comment
Share on other sites

Please sign in to view this quote.

I created a Powershell file that is similar except it watches a files 'date modified' attribute and then executes another function. I think I also mentioned this on another thread where someone wanted to create a notification when someone crashed the CMM (poor man's way to monitor crash log). Code would look like this in PS:

 

# Define the file to monitor
$FilePath = "C:\path\to\your\file.txt"

# Define email parameters
$SmtpServer = "smtp.your-email-provider.com"
$SmtpPort = 587 # or 25, 465 depending on your email provider
$EmailFrom = "your-email@example.com"
$EmailTo = "recipient@example.com"
$Subject = "File Modified Alert"
$Body = "The file $FilePath has been modified."
$Username = "your-email@example.com"
$Password = "your-email-password"

# Get the initial LastWriteTime of the file
$LastWriteTime = (Get-Item $FilePath).LastWriteTime

# Continuous monitoring
while ($true) {
    # Get the current LastWriteTime of the file
    $CurrentWriteTime = (Get-Item $FilePath).LastWriteTime

    # Check if the LastWriteTime has changed
    if ($CurrentWriteTime -ne $LastWriteTime) {
        # Update the LastWriteTime
        $LastWriteTime = $CurrentWriteTime

        # Send an email notification
        $Message = @{
            SmtpServer = $SmtpServer
            Port = $SmtpPort
            From = $EmailFrom
            To = $EmailTo
            Subject = $Subject
            Body = $Body
            Credential = New-Object System.Management.Automation.PSCredential($Username, (ConvertTo-SecureString $Password -AsPlainText -Force))
            UseSsl = $true
        }
        
        Send-MailMessage @Message
        Write-Host "Email sent: $Body"
    }

    # Wait for a specified duration before checking again
    Start-Sleep -Seconds 10
}

 

And Richard is spot on.. Any programmer who wants to up their game significantly is using GPT. Don't get left behind!

Link to comment
Share on other sites

 Share

×
×
  • Create New...