Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I modified the script a bit so that the IDF Editor is opened and closed for every file being sorted. This prevents crashing on my machine.

WARNING!!!
This script is dangerous. One of the steps in this script is opening a new IDF file from inside the IDF Editor. I almost messed up a bunch of IDF files because the Open File window defaults to the last directory that an IDF was opened in. You need to make sure the default directory is the correct directory.
If you don't understand what I'm talking about, then don't run this script. If you're paranoid, run it in a virtual machine.
image description

"""
idf_editor_sorter.py
~~~~~~~~~~~~~~~~~

A simple script which controls the IDF Editor application, taking advantage of
the fact that it tidies up the file when saving.

It would be better if this could happen in the background but the simplest way
to control IDF Editor from Python is using SendKeys which requires the window
to be in the foreground.

This script should be run from the folder containing the IDF files to be sorted.
Filenames will be unaffected.

"""

############################ WARNING ###################################
#The Open File window defaults to the directory of the last opened file.
#Be cautious of where the files that are being modified are.
#If you're not sure what that means, then DON'T RUN this script!!!
#https://unmethours.com/question/535/sorting-a-macro-generated-idf/

import os
import time

import win32com.client

EPLUS_VERSION = '9.2'  # The EnergyPlus version number of the IDFs to be sorted 
IDF_EDITOR_PATH = "C:\EnergyPlusV9-2-0\PreProcess\IDFEditor\IDFEditor.exe"

def sort_idf(idf_name):
    wait_time = 2
    shell = win32com.client.DispatchEx("WScript.Shell")
    time.sleep(wait_time)
    shell.Run(IDF_EDITOR_PATH)
    time.sleep(wait_time)
    shell.AppActivate("IDF Editor")
    time.sleep(wait_time)  # Wait for the IDF Editor to open - you may need to increase this
    shell.SendKeys("^o")
    time.sleep(wait_time)  # Wait for dialog to open - you may be able to reduce this
    shell.SendKeys(idf_name + "{ENTER}")
    time.sleep(wait_time)
    shell.SendKeys("{TAB}" * 8)
    time.sleep(wait_time)
    shell.SendKeys("{F2}XX{ENTER}^s")  # This is where the sorting occurs
    time.sleep(wait_time)
    shell.SendKeys("{F2}%s{ENTER}^s" % EPLUS_VERSION)  # Write the correct value
    time.sleep(wait_time)
    shell.SendKeys("%{F4}")
    time.sleep(wait_time)

idfs = (f for f in os.listdir('.') if os.path.splitext(f)[1] == '.idf')
for idf in idfs:
    sort_idf(idf)

WARNING!!!
This script is dangerous. One of the steps in this script is opening a new IDF file from inside the IDF Editor. I almost messed up a bunch of IDF files because the Open File window defaults to the last directory that an IDF was opened in. You need to make sure the default directory is the correct directory.
If you don't understand what I'm talking about, then don't run this script. If you're paranoid, run it in a virtual machine.
image description