Script to count files in a folder

U

umut.tabak

Dear all,

We have a multi-platform application(Windows-Linux). Linux part of our
application is writing some input and trigger files on the a shared
drive. What I want to do is to be able to count the occurence of these
trigger files.

lets say my file is

file.start

A batch file creates this file and deletes that file in the same loop.
This file is intended to start the reading of an input file. After it
is read batch file deletes this file automatically and on the next loop
it will be written and deleted again. With this script I have to count
the occurences of the file on this specific folder.

I did not have the time to think on it but maybe you can give me some
ideas on that. And btw, I am not an expert in scirpting. I am trying to
learn Python but this task is a little bit over my head.

Any help or thoughts are appreciated.

With best regards
 
F

Fredrik Lundh

A batch file creates this file and deletes that file in the same loop.
This file is intended to start the reading of an input file. After it
is read batch file deletes this file automatically and on the next loop
it will be written and deleted again. With this script I have to count
the occurences of the file on this specific folder.

what does "count" mean here?

do you want to check if the file is there, or how many times a file with
that name has been created ?

the former is easy (just use the os.path.isfile() function), the latter is not
really possible.

</F>
 
U

umut.tabak

do you want to check if the file is there, or how many times a file with
that name has been created ?

Yes I would like to check how many times that specific file is created.
so count is the number of times of creation.
 
D

Diez B. Roggisch

Yes I would like to check how many times that specific file is created.
so count is the number of times of creation.

As Fredrik said - not really possible, at least not with standard python.
For Linux, you might be lucky using the python LUFS module:

http://www.freenet.org.nz/python/lufs-python/

But you being a beginner, I guess that is a bit over your head. Trying to
change the behaviour of the batch skript, writing out the information you
need is the more reasonable approach.

Diez
 
D

Dennis Lee Bieber

Yes I would like to check how many times that specific file is created.
so count is the number of times of creation.
Under Windows, you might be able to get close using:

win32api.FindFirstChangeNotification
win32api.FindNextChangeNotification

with

FILE_NOTIFY_CHANGE_FILE_NAME
Any file name change in the watched directory or subtree causes a change
notification wait operation to return. Changes include renaming,
creating, or deleting a file name.

Though those work on the directory level -- it is possible the file
is created and deleted before you get a chance to scan the directory in
response to the notification... Though if the name is fixed, it may not
be that time consuming:

[pseudo-code]
inFile = False
while True:
wait for notification
if inFile and not os.path.isfile("marker.file"):
#file has been deleted
inFile = False
elif not inFile and os.path.isfile("marker.file"):
#file has been created, increment counter
inFile = True

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top