VBA - detect open/close events of all file types within a specific parent folder - vba

Just after initial pointers on using VBA for the following:
Create a time keeping app running in the background that detects when any File within the 'Projects' folder is opened, and ask user if they want to start recording time to this job. Timer would stop when file from a different Project is opened, or when the original File is closed. I guess i'd need to filter out certain file types (temp files etc.)
Alternative approach could be to detect if active window is a project file. Perhaps this better than the above, as it would allow for scenarios where files from different projects are open, as well as being able to build in a delay - i.e. no need to log time if only active for 2 minutes.
Possible?

Related

Excel vba macro to download a file automatically when it's added to a Sharepoint site

I am running an excel macro, it downloads some files from SharePoint. It's working fine but my problem is, Everytime i need to check if the file exists there or no then i download it.
Is there a possibility to add a condition when the file is added on SharePoint then the macro will run automatically. Because the reports are uploaded in different times by other users for example one day at 9 am, another day 11 am, another day at 1pm... and now i am checking everyday many times on the sharepoint site if the report was added or no before i run my macro.
I want to avoid this and link somehow the sharepoint site to my macro.
For example for the reports i receive on Outlook i created a macro when i receive an email with attachment containing a specific name it will download automatically. I want something similar on Sharepoint without checking everytime on Sharepoint if the other users uploaded their reports or not.
Any suggestion please ?
Assuming that you are using SharePoint 2013 OnPremise you can write an ItemAdded event receiver which will execute your macro (you can do this only if you are allowed to deploy code to SharePoint).
You could also write a Service / Job that will run every 5-10 minutes and check which files have been added in the last 5-10 minutes (this frequency will depend on what is the urgency for executing the macro), download the file and then execute the macro on those.
Irrespective of the environment this code could run directly from your local machine if you have no good place to host it.

Writing files safely (WinRT)

What approach do you use to write critical app files like settings, configuration files, user files in WinRT, or in general?
To illustrate my concern - in my app I am saving the list of user selected data sources as a JSON file. In case the user updates the list and saves it, I just overwrite the current file with the new JSON serialized list. But if the app were killed from the task manager or the computer lost power in that very moment when the file is being written, it would stay in an inconsistent state and would probably cause the app not to launch or the user would definitely lose data.
I considered writing into a different file and then swap them when finished. Is this solution the best one possible?

I need to automate a process to open files created by another application

I have a JAVA application on a server that creates log files.
I have, in other server, an application (VB.NET) that process those logs to search some strings; if I manually select the file it works all OK.
Now I need that the second application automatically open every new log file on the remote server every one minute.
So I wonder if there's any way to know when a new log file is created or what are the new files created since last minute.
The files have the following format name server.log.*.log so the files with different format name must be ignored.
More info:
There's a second JAVA application on the first sever that delete logs older than one day.
You can use Directory.GetFiles every minutes to get the list of all the files in the folder. Compare it to the previous list (that you keep in memory) and process the new files.
An other option is to monitor the folder for any changes. This can be done using the System.IO.FileSystemWatcher class. By setting the Path and the proper NotifyFilter you can see which files were created with the Created event as soon (or almost) as they appear.

Monitoring a folder for a specific file

I have a program that uploads .txt or .rje files from a folder. Now when you put any other file format into the folder, like .jar, then the application crashes.
Now I cannot change the mechanics of the application, so I would like to know if there is a type of program/script that I can use that monitors the folder for any files that are non-txt/rje and then move them out of the folder once they are put there...
Is this possible using a script? (I do not want to use a .exe application to do this...not allowed to install 3rd party software onto the server this folder exists...)
Thank you
Your solution won't work as you have a race condition between the program doing the upload and the one doing the deletion. If upload runs first it still crashes.
The correct solution is to modify the upload program to cope with this scenario.
If that is not possible then the only safe work around would be to use a new folder to drop the files in, have a script run that constantly scans the folder and if a new file appears either move it to the processing folder or deletes it as appropriate.
(For the actual detection that's not my area of expertise but the simplest would be to have a bat file that just runs periodically (or even just runs once and loops with a wait, check, move, wait, check, move, etc) and processes everything in the folder when it runs).

Prevent file being overwritten

Imagine there are 3 or more independent locations where a file can be modified. These locations communicate to each other through email or mail (direct flash drive restoration). Though there is a big room for flow - to make simultaneous editing to the file and screw up things, this client won't change too much. He rather call everyone that he is working on the last update or tell the other guys that he is waiting for third guy's last update. Anyway, at some point after several exchanges, due to one of participants unintentional error THE LAST VERSION of the file eventually gets mixed up. From this point everyone searches for the last version BY LOOKING THE CONTENT of the file.
This client wants to have a central location (he has actually, that is his PC's some location) and let everybody (including himself) copy any new or suspected new file to this location but prevent file's last version being copied. From this location he has to easily copy, send or open the file and work.
So, here is my concept (2 steps):
step 1: I made an ad to the main application where this file is created or edited. This ad prompts the user to give a version number to the file with every invoked save command from the editing application. In fact the file can be re-saved multiple times but not considered modified (file attributes creation, save etc. do not have great meaning here). This said the user can cancel my ad-in but have saved the file, not saving a new file version.
step 2: multiple solutions:
solution A: I'm thinking to have a folder/file watch and prevent the last version of the file being overwritten. As you know, FileSystemWatcher will fire the change/delete etc., events AFTER FACT so, I have to back copy overwritten file after the fact (w/ some tricks).
solution B: have a database to store all version of files and built-in some shell extension to extract/view files from the database. Move all copied/pasted files to the database (my program folder) and restore latest file in working folder after watcher fires change/delete event.
solution 3: find out built-in windows tools (API etc.) to greatly rely on it with some programming.
Any ideas?
Thanks in advance.