I have number of workbench projects .wpbj in a folder (and subfolders). how to create archives?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 223
100 Comments 25 Answers Second Anniversary 25 Likes
✭✭✭✭

I have number of workbench projects .wpbj in a folder (and subfolders). I would like to create an archive for each .wbpj file and store it in a particular folder automatically in batch using Workbench script.

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 223
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    edited February 5

    For this, we can create a .bat file (rename extension from .txt to .bat) with the below script. Adjust the version as needed (here 2025R1 i.e. 251 is used).

    "C:\Program Files\ANSYS Inc\v251\Framework\bin\Win64\RunWB2.exe" -R archiveProjects.py
    echo
    pause
    

    Then, we would need to create a archiveProjects.py file with the below script. In this script, one would need to specify the root folder, "search_folder_path " and also the folder where archives without results needed to be stored, i.e. "archive_folder_path ". If there are subfolders within the root folder, they are searched as well and .wbpj files are extracted.

    import os
    
    def find_wbpj_files(folder_path):
        # List to store the paths of all .wbpj files
        wbpj_files = []
    
        # Walk through the directory
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                if file.endswith(".wbpj"):  # Check if the file has the .wbpj extension
                    # Append the full file path to the list
                    wbpj_files.append(os.path.join(root, file))
    
        return wbpj_files
    
    search_folder_path = r"D:\del3"  # Replace with your folder path
    archive_folder_path = r"D:\del3\allArchives"
    wbpj_file_paths = find_wbpj_files(search_folder_path)
    
    # Print the list of .wbpj file paths and archive them
    for path in wbpj_file_paths:
        print(path)
        fileName = os.path.basename(path)
        try:
            Open(FilePath=path)
            archive_file_name =fileName.split(".")[0] + ".wbpz"
            archive_file_path = os.path.join(archive_folder_path,archive_file_name)
            Archive(FilePath=archive_file_path, IncludeSkippedFiles=False)
        except:
            print("archive operation failed")
    

    So, once these two files i.e. .bat and .py files are created, they would need to be saved in a same location, and .bat file simply needs to be double clicked to start the archiving.

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 223
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    edited February 5

    The below script is a modified version of the previous script, where, the archive is stored at the same location as the .wbpj and not a provided folder path. Also, the .wbpj file and the corresponding folder (_files) is deleted once the archiving is completed.

    import os
    import shutil
    
    def find_wbpj_files(folder_path):
        # List to store the paths of all .wbpj files
        wbpj_files = []
    
        # Walk through the directory
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                if file.endswith(".wbpj"):  # Check if the file has the .wbpj extension
                    # Append the full file path to the list
                    wbpj_files.append(os.path.join(root, file))
    
        return wbpj_files
    
    search_folder_path = r"D:\del3"  # Replace with your folder path
    wbpj_file_paths = find_wbpj_files(search_folder_path)
    
    # Print the list of .wbpj file paths, archive and delete
    for path in wbpj_file_paths:
        print(path)
        fileName = os.path.basename(path)
        try:
            Open(FilePath=path)
            project_name = os.path.splitext(path)[0]
            project_folder = os.path.join(os.path.dirname(path), "{}_files".format(project_name))
            archive_file_path = project_name + ".wbpz"
            Archive(FilePath=archive_file_path, IncludeSkippedFiles=False)
            print("Archive created")
            Reset()
            # Delete the .wbpj file if it exists
            if os.path.exists(path):
                os.remove(path)
                print("wbpj file deleted")
            # Delete the corresponding _files folder if it exists
            if os.path.exists(project_folder) and os.path.isdir(project_folder):
                shutil.rmtree(project_folder)    
                print("project folder deleted")
    
        except:
            print("archive operation failed")
    
  • Matthias
    Matthias Member Posts: 25
    10 Comments Name Dropper First Anniversary
    **

    @Rohith Patchigolla said:
    print(path)
    fileName = os.path.basename(path)
    try:
    Open(FilePath=path)
    Save(Overwrite=True)
    archive_file_name =fileName.split(".")[0] + ".wbpz"
    archive_file_path = os.path.join(archive_folder_path,archive_file_name)
    Archive(FilePath=archive_file_path, IncludeSkippedFiles=False)
    except:
    print("archive operation failed")
    ```

    Really helpful script.
    A nice addition would be to check if an archive already exists. If it exists, then compare the modification date of the project and the archive and only proceed with the above try block if the project is newer than the archive or an archive does not exist.