Close specific or all excel files

Hi, how can I make knime (optional: check if a specific or any excel file is open and) close a specific or all excel files?

Hi @knndcc, welcome to the forum!

For some context, are you talking about Excel files that are already open outside of Knime (i.e. they are open in Excel itself), or are you referring to an Excel file that is currently being written to by one Knime node, when another node attempts to act on the same file?

2 Likes

Hi @takbb, I am talking about Excel files that are already open outside of Knime.

Hi @knndcc , I am not sure if this can be done directly in Knime (I am pretty sure it cannot be done). I am also not sure how you can “close” any file for that matter, except by issuing a kill command. And for that, you would need to check the process list, identify the file from the process list, identify the pid (process id) of the process, and issue the kill command to kill that pid.

The External Tool or External Tool (Labs) nodes allow you to execute external programs:

You could build an external script to do what I described above (identify pid, issue kill command, etc), and call the script from Knime and dynamically pass the file name to the script.

Of course, you will need the proper privilege to be able to kill a process.

Personally, I don’t like to use the Kill command to close a file, as it bypasses the verifications that closing the file properly does. For example, if the file was modified, close the file properly will prompt you to decide if you want to save your changes or not. The Kill command will just kill the process and could corrupt the file if it is in the middle of saving changes.

But if you really need to implement something, that’s an alternative, but with the risks I mentioned above.

4 Likes

Hi @knndcc, further to @bruno29a 's thoughts… it’s not common for one application to be able to tell another to close, and even if it could do it, you’d have be able to first identify which application had the file open, and then deal with other issues such as a “would you like to save your changes y/n?” type message appearing.

I’m guessing from your question that you are running into the problem of trying to write an Excel file at the end of a long workflow only to discover that the file is still open from you (or somebody else) viewing it the last time round, or some similar problem? This is quite a common problem, and not unique to KNIME. Other workflow/analytics tools that I’ve used, such as Alteryx, hit the same problem.

If this is the issue, the only recommendation I really have is to try to put something at the beginning of your workflow to attempt writing to your output files (if that is possible and you know what the files will be) early on. At least then you have early failure that you can hopefully deal with.

I’ve uploaded a demo workflow to the hub that uses a java snippet to test if one (or a set) of files is unlocked. It fails if any locked files are detected before commencing with the main part of the workflow. I know it doesn’t answer your original question, but it’s a possible “half-way-house”. This could probably be turned into a reusable component if it’s something you’d need to do often. (Assuming somebody (@ipazin ? :thinking:) doesn’t come along and tell me there’s already a node that does this… :wink: )

4 Likes

Hello @takbb,

if there is I still haven’t found (what I’m looking for) :sweat_smile:

But seriously you should start writing your own nodes and building extensions. You’ve got Java knowledge, vast experience (1985!) and you detect properly areas which could be improved around KNIME to help others :wink:

Br,
Ivan

Br,
Ivan

4 Likes

Hi @ipazin, thanks for the compliments. Writing nodes is certainly on my bucket list… :grin: I’m just waiting for the next release of KNIME before I start looking into doing that though as I’m too lazy to have two different versions of KNIME and jdk installed side by side… :wink:

3 Likes

@ipazin It would be awesome if we could write extensions in python :grimacing:
Not everybody has @takbb s incredible java skills.

On topic. This sounds rather like an RPA task then a KNIME topic

2 Likes

Hello @Daniel_Weikert,

well there are nodes reaching out to Python to perform their actual tasks. See here more:

Br,
Ivan

2 Likes

Hi and thanks for your replies,
My knowledge in Knime is rather limited :sweat_smile: so not really sure how I could make it work with “External Tool”. Just for more context, somebody created a python script node for me which downloads several excel files (that remain open) from another program and those are the files that I wanted to close (the files remain unchanged so wouldn’t be a problem to “kill”). I thought if it’s possible to run commands in another program it would be possible to close excel files or excel at all in an “easy” way but doesn’t seem so. Thanks for your help.

Then it might make sense to not open them after download in the code
br

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.