how to detect file change VBS - vbscript

i want to make a Visual basic script console app that prints edited if a file has been modified. for example if i have a text file with some notes in and i add it to a folder when its edited the program checks the folder its in and the files then prints the name of the file and modified or not modified
how would i go about doing this i am relatively new to Visual basic script i probably have 4 months basic experience.
console.writeline("what do i do?")
console.writeline("and how do i do it")
and I'm trying to do it as a console app so the preferred outcome i would like to see would be
File Checker
test.txt - Edited
test2.pptx - Un-edited
etc etc etc

If you need an immediate notification, WMI is probably the best route. But WMI will also require your process to be running (in a blocked state) all of the time. Alternatively, you could schedule a VBScript to be launched at some interval and it could check each file's last-modified date against a text file or database that you use to store the modification date the last time the script was run.
An even easier solution would be to just check if the modification time changed since the last run. For example, if your script runs every 10 minutes and you discover a file that was changed within the last 10 minutes, report it.
With CreateObject("Scripting.FileSystemObject")
For Each File In .GetFolder("c:\folder").Files
If DateDiff("n", File.DateLastModified, Now) < 10 Then
' File has been modified in past 10 minutes.
End If
Next
End With

Related

How to append current date to property file value every day in Unix?

I've got a property file which is read several times per day by an external application in order to process some files. One of the properties tells the app where to store the processed files. Application runs on Linux.
success_path=/u02/oapp/success
The problem is that every day several files are thrown in that path and after several months, I would have thousands of files in this plane folder.
Question: How can I append the current date to this property file so it would look like:
success_path=/u02/oapp/success/dd-MMM-yyyy
This would be updated every day at 12:00AM so for example today it would be
success_path=/u02/oapp/success/28-JAN-2017
The file is /u02/oapp/configuration/oapp.properties
Thanks in advance
Instead of appending current date to the property, add additional logic to the code that stores the processed files so that:
it takes the base directory from the property file (success_path in your case)
it creates a year/month/day directory to store the files
Something like:
/u02/oapp/success/year/month/day (as in `/u02/oapp/success/2017/01/01`)
or
/u02/oapp/success/yearmonth/day (as in `/u02/oapp/success/201701/01`)
or
/u02/oapp/success/yearmonthday (as in `/u02/oapp/success/20170101`)
If you don't have the capability to change the app's behavior, you might need to write a cron job that periodically moves the files external to the app.
jq -Rr 'select(startswith("success_path="))="success_path=/u02/oapp/success/"+(now|strflocaltime("%d-%b-%Y")|ascii_upcase)' /u02/oapp/configuration/oapp.properties | sponge /u02/oapp/configuration/oapp.properties

How to process this kind of edge cases in shell script?

I have a program in Linux to write log into files on a regular interval (for
example: 10 minutes every file), so I have a directory hierachy like this:
In which, 20151001 stands for the log dir for the day
2015(year)/10(month)/01(day), and 00 stands for the first hour in the day,
and 00.00 stands for the log file for the first 10 minutes in the first hour
of the day.
In my shell script I need to detect if there is any previous log file is empty,
for example if now the time is 2015/10/09 09:32, the newest log file should be
20151009/09/09.03, the file 09.03 could be an empty file, but the other
older files should not be empty. In order to simplify the problem, I just detect
if the previous file is empty, so I just detect if the file 20151009/09/09.02
is empty. However I have to handle some edge cases, like:
If now the time is:
2015/10/09 09:01
the previous file is in another directory, it’s:
20151009/08/08.05
If now the time is:
2015/10/09 00:01
the previous file is:
20151008/23/23.05
Is there any powerful algorithm or tools to handle my problem, especially the
edge cases?

Checking if a file was opened and return path to opened file (in Windows 7)

I would like to create an application that would monitor processes that are opened. I want to use it to monitor how often do I open my mp3 files. The program would run in background and count the number of times I run each mp3 file and later I would sort my mp3 files based on this number.
I used Process Monitor to check if it can be done. When i filter the output so that it shows only my mp3 player processes and i set to see only "Process Start" operation then I can read which file was opened, for example this is the command line detail that the Process Monitor showed me files that are opend in real-time. After each file run there is a new input in Process Monitor.
As you can see I could easily count the Process Monitor outputs to get the number of times each file was started. However, I don't know how it is done because Process Monitor is an .exe and i can't see inside the code.
What is the easiest way to solve my problem? In fact programming language I would like to use doesn't matter, can be C#, C++, Python.
Thank you in advance.
I have come up with something on my own. It should be enough to use this library psutil in Python 2.7. My process name will be constant, because I always use the same program to run my mp3 / video files.
Find PID of my process
When I have the PID i can check what files are currently opened by my process.
Always remember last opened file. If the current file is different from last one, then increment current's file open count by 1. That's how I can create a list of opened files based on opening frequency.
Use while loop to check this in background.
Here's some small amount of code but in general the soultion is quite simple :)
import psutil
process_list = psutil.pids()
for i in process_list:
temp_process = psutil.Process(i)
#if process doesn't have a name then the .name() method will cause a crash, we need to try-catch it
try:
if (temp_process.name()=="mpc-hc64.exe"):
print temp_process.cmdline() #in my case opened file will be listed in the cmdline, may not always be true
print temp_process.open_files() #this however should ALWAYS return files that are opened by current process!!
except:
print "no process name"

How to check if file is being copied under macOS

Users on our network copy files on the server in a directory called "DropBox" with AFP connection, simply dragging them with the Finder.
A script running on the server checks periodically for the presence of new files inside "DropBox" and then moves them with mv into other directories.
How can the script check if a file is being copied (and wait for the process to complete before moving it away)?
I've tried with fuser filename with no success. If the file copy is issued by a remote machine fuser reports that no process is using the file.
There are two approaches to this problem:
1. flag the filename to tell the file is partial:
When a file is written (moved/copied) into the DropBox .part extension is added to the name. When the writing is over the .part extension is removed.
Other processes operate on files only if the .part extension is not present.
2. check the file size; if it's constant then the write operation is ended (maybe)
This is less accurate and more complicated that (1) but offer a good guess if option 1 is not viable for whatever reason.
For each file in the dropbox the time the size isn't changed is recorded.
When a file increases in size the time value is reset to zero.
When a file is not being written the time values remains constant.
If a file isn't grown for at least (30 seconds, 1 minute, the more time you can afford the better it is of course) then write operation are over and the file can be managed.
This approach fails when a transfer in interrupted.

How to get the folder creation time (with milliseconds)?

I need folder creation time with milliseconds in Windows.
There's a fundamental problem: the filesystem doesn't store the creation time of a file, only the time the file structure was last updated.
This is true on both Windows (read it carefully) and Unix.

Resources