Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

script or code to copy freshly created files

Greetings Experts!!,

Need a solution for a scenario like this..

1. A website is opened in IE from where a rtf file and either one of the following format voice files (wav or vox or mp3 or wma) gets downloaded into the userprofile\temp folder.

2. Once the browser is closed the downloaded rtf file and voice file gets automatically deleted from the userprofile\temp folder.

3. steps 1 and 2 are repeated for getting the next set of files.

4. The task now is to automate the copy of these files as and when they are created in the temp directory to another location or drive.

5. operating details are windows xp, office 2003 ,internet explorer 6

6. if the script or code can run as a background process it will be the best as the process requires no user intervention.

Best Regards,
Mahesh
0
maheshr2k3
Asked:
maheshr2k3
  • 16
  • 15
  • 4
1 Solution
 
ramromconsultant Commented:
You can write equivalent code in any language that gives access to the windows api.

You can use the schedule service to launch the program in the background.
0
 
BillDLCommented:
There is one issue that makes this more involved than it should be.  Files fetched into loaded web pages in Internet Explorer are cached in the user's "Temporary Internet Files" folder under a dynamically created sub-folder with a random name comprising 8 characters.

1. The folder in which these are created has a dot in it
C:\Documents and Settings\<UserName>\Local Settings\Temporary Internet Files\Content.IE5\
which can confuse the issue and may be seen as a file with the *.IE5 file extension by some types of scripts.

2. The sub-folders created have the hidden attribute.

3. You cannot normally open Windows Explorer and Browse to any folder below the "Temporary Internet Files" of your own profile.  You CAN, however, launch Windows Explorer using the command that points to the "Content.IE5" folder and opens it in the Folder Tree pane so you can fully explore all sub-folders:
%SystemRoot%\explorer.exe /n, /e,%homepath%\Local Settings\Temporary Internet Files\Content.IE5

4. Although you may see files cached directly under the "Temporary Internet Files" folder, these are generally pointers to the actual files in the sub-folders.

5. Clearing the Temporary Intenet Files folder deletes the sub-folders.  New ones with random names are created when IE starts caching files again.  Closing the browser does NOT always clear the cache.  This is determined by the setting named after this in Control Panel > Internet Options > Advanced tab.

6. Files cached do not always have logical names, and in addition different cached files may have the same name in different sub-folders.  The files directly under the "Temporary Internet Files" folder generally resolve from the name as seen in Windows Explorer (ie. with long names sometimes containing weird characters) to the correct name as cached in each of the Content.IE5's sub-folders.  Sometimes the [1], [2], etc in the name will allow you to copy out the files without overwriting different files of the same name, but sometimes not.  Quite often the files may not have a file extension, and so it can be difficult to determine which is which, and can also make it harder to use wildcards in scripts to filter what files are to be copied.

8. Some file types are not totally cached until they are completely viewed, in particular multimedia files.  Copying them out before they are fully cached can leave you with incomplete and sometimes unviewable files.

There are probably other things that have to be taken account of when scripting an automated process to do what you need, but hopefully considering these aspects will help you avoid failures and wasted time.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
maheshr2k3Author Commented:
Greetings Experts!!,

Firstly i would like to thank the administrative EE moderator to help post my query to the right zones to get responses from experts.

Went through the link given by ramrom.. it is a fairly long vb code which i am not too familiar with..just a beginner with knowledge of batch file programming..if somebody could please modify that vb code for my request of monitoring C:\Documents and Settings\Administrator\Local Settings\Temp folder continuously and copy the newly created files to another drive or network location it will be of immense help..

BillDL has just gone too far thinking about the Temporary Internet Files folder..the issue is only about monitoring the C:\Documents and Settings\Administrator\Local Settings\Temp folder only.

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
I can offer you  a solution in Python. If you don't have Python then you'd need to download and install it along with the python for windows extensions. These are open source (free) and easy and quick to set up.

Shall we proceed?
0
 
ramromconsultant Commented:
Here is the code. Tested on my computer with real path names.

If youo don't have Python:

Install Python 2.5.2 and PythonWin pywin32-212.win32-py2.5.exe.

http://python.org/download/releases/2.5.2/
http://sourceforge.net/projects/pywin32/ - the big green box

If you have Python, install the version compatible version of PythonWin.

At a command prompt type the path-to-python.exe path-to-monitor.py
If all has gone well the program will print "Monitoring c:\windows\temp\ for new files"
then wait for new files, report and copy them.

monitor.py:

# thanks to Tim Golden at http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
# for moss of the code that follows!
 
# Change next 3 lines to suit your needs
path_to_watch = "c:\\windows\\temp\\"
destination = "c:\\windows\\final\\"
delay = 1000 # one second
 
import os
import shutil
import win32file
import win32event
import win32con
#
# FindFirstChangeNotification sets up a handle for watching
#  file changes. The first parameter is the path to be
#  watched; the second is a boolean indicating whether the
#  directories underneath the one specified are to be watched;
#  the third is a list of flags as to what kind of changes to
#  watch for. We're just looking at file additions / deletions.
#
change_handle = win32file.FindFirstChangeNotification (
  path_to_watch, 0, win32con.FILE_NOTIFY_CHANGE_FILE_NAME)
#
# Loop forever, listing any file changes. The WaitFor... will
#  time out every xx seconds allowing for keyboard interrupts
#  to terminate the loop.
#
print "Monitoring %s for new files" % path_to_watch
try:
  old_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
  while 1:
    result = win32event.WaitForSingleObject (change_handle, delay)
    #
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    #
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
      added = [f for f in new_path_contents if not f in old_path_contents]
      deleted = [f for f in old_path_contents if not f in new_path_contents]
      if added:
        for fn in added:
          print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
          shutil.copyfile(path_to_watch + fn, destination + fn)
      old_path_contents = new_path_contents
      win32file.FindNextChangeNotification (change_handle)
finally:
  win32file.FindCloseChangeNotification (change_handle)

Open in new window

0
 
ramromconsultant Commented:
Once the program is running then test it by droping some files in the temp folder or running IE.

When that is working then we will create a scheduled task to run it automatically or put it in the startup folder.
0
 
maheshr2k3Author Commented:
Greetings Ramrom,

installed python and tested the code after modifying the path to be monitored as path_to_watch = "C:\\Documents and Settings\\Administrator\\Local Settings\\Temp\\" but got a error as below:

C:\Python25>python.exe monitor.py
  File "monitor.py", line 6
    path_to_watch = "C:\\Documents and Settings\\Administrator\\Local Settings\\Temp"
    ^
IndentationError: unexpected indent

Then changed the long pathname to short path name format as below and tried--
# path_to_watch = "c:\\docume~1\\admini~1\\locals~1\\temp\\"  but again geting error as below:

C:\Python25>python.exe monitor.py
  File "monitor.py", line 7
    path_to_watch = "c:\\docume~1\\admini~1\\locals~1\\temp\\"
    ^
IndentationError: unexpected indent

Did web searches for this error and was not able to find anything relavent. Please suggest or rectify any anamolies..in the meantime i will search for a possible solution to the error..


Best Regards,
Mahesh
0
 
maheshr2k3Author Commented:
Hi Ramrom,

sorry for posting in a hurry-- a small space at the beginning of the sentence was the reason for the error which now is resolved and the program is printing to screen : Monitoring C:\Documents and Settings\Administrator\Local Settings\Temp\ for new files

But there seems to be a problem with copying files..the program is copying all files created--including the temporary files..and terminates when it is not able to find a temporary file or folder as in the error --  

C:\Python25>python.exe monitor.py
Monitoring C:\Documents and Settings\Administrator\Local Settings\Temp\ for new files
Copied ~DFDC4D.tmp from C:\Documents and Settings\Administrator\Local Settings\Temp\ to e:\final\
Copied am18112008 from C:\Documents and Settings\Administrator\Local Settings\Temp\ to e:\final\
Traceback (most recent call last):
  File "monitor.py", line 49, in <module>
    shutil.copyfile(path_to_watch + fn, destination + fn)
  File "C:\Python25\lib\shutil.py", line 46, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Administrator\\Local Settings\\Temp\\am18112008'


Only if the *.doc or *.rtf or *.wav or *.vox or *.dss files are copied it is more than sufficient. Please suggest a update for this .

Best Regards,
Mahesh
0
 
maheshr2k3Author Commented:
Hi Ramrom,

one more small check has to be done--the files which are downloaded from another website take some time say about 3 to 4 secs to fully come to the temp folder of the system, if the script runs before that the half downloaded files are copied to the destination folder..is it possible to put a check or delay of 5 secs after the file has been created and then copy it.. and as earlier post only if the *.doc or *.rtf or *.wav or *.vox or *.dss files are copied it is more than sufficient.

Please suggest a update for these as they almost fulfill the requirement..

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
Welcome to Python. I hope you enjoy and use it. It has been one of my languages of choice after working with and teaching MANY languages.

As you probably guess Python uses indentation to express structure. Any statement ending in : must be followed by at least one statement further indented. All statements at a certain "level" must have the same indentation. Hence the error you got.

Warning untested code.
# to ignore the permission error:
          try:
            shutil.copyfile(path_to_watch + fn, destination + fn)
          except IOError:
            pass
 
# to only copy certain extensions:
 
            ext = os.path.splittext(fn)[1] # get the extension
            if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
              shutil.copyfile(path_to_watch + fn, destination + fn)

Open in new window

0
 
maheshr2k3Author Commented:
Hi Ramrom,

This is the way the code was modified--please verify where is it wrong as the monitoring folder ...............so and so comes but the actual copying does not happen..

print "Monitoring %s for new files" % path_to_watch
try:
  old_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
  while 1:
    result = win32event.WaitForSingleObject (change_handle, delay)
  #
  # If the WaitFor... returned because of a notification (as
  #  opposed to timing out or some error) then look for the
  #  changes in the directory contents.
  #
  if result == win32con.WAIT_OBJECT_0:
    new_path_contents = dict ([(f, None) for f in os.listdir (path_to_watch)])
    added = [f for f in new_path_contents if not f in old_path_contents]
    deleted = [f for f in old_path_contents if not f in new_path_contents]
    if added:
      for fn in added:
        print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
        try:
          ext = os.path.splittext(fn)[1] # get the extension
          if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
            shutil.copyfile(path_to_watch + fn, destination + fn)
            old_path_contents = new_path_contents
            win32file.FindNextChangeNotification (change_handle)
        except IOError:
          pass
finally:
 win32file.FindCloseChangeNotification (change_handle)

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
Sorry - I thought the indentation would convey where to add the code. Here is an "improved" version with the tries and tests in the correct place, and some other "improvements" most notable is the use of sets instead of dicts.

I have tested this.


# retain code above this point
print "Monitoring %s for new files" % path_to_watch
try:
  old_path_contents = set(os.listdir(path_to_watch))
  while True:
    result = win32event.WaitForSingleObject(change_handle, delay)
    #
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    #
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = set(os.listdir(path_to_watch))
      added = new_path_contents - old_path_contents
      for fn in added:
        ext = os.path.splitext(fn)[1] # get the extension
        if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
          try:
            shutil.copyfile(path_to_watch + fn, destination + fn)
            print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
          except IOError:
            pass
      old_path_contents = new_path_contents
      win32file.FindNextChangeNotification(change_handle)
finally:
  win32file.FindCloseChangeNotification(change_handle)

Open in new window

0
 
maheshr2k3Author Commented:
Hi Ramrom,

Many Regards for your suggestion-- i have a small query about the delay = 5000 statement--what does this mean in the code?

1. to check the folder every 5 seconds or
2. there will be a delay of 5 secs from the time of creation of the file in the folder and the actual copying to another location?

The reason being that the files which are downloaded from the website/internet take some time say anything ranging from 3 to 15 secs to fully copy to the temp folder of the system, if the script runs before that the half downloaded files are copied to the destination folder..is it possible to put a check or delay of 3 to 15 secs from the creation time of the file and then copy it to another location.

If this question is not out of scope of the original question, kindly suggest a remedy for the otherwise perfect code.

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
Sorry I overlooked that concern.

The 5000 means neither of those guesses.

WaitForSingleObject waits for a folder change. As soon as a change is detected it returns. The delay (5000) causes WaitForSingleObject to return if no change is detected within 5 seconds. Did you read the comments in the code?
"The WaitFor... will time out every xx seconds allowing for keyboard interrupts to terminate the loop."

To handle the need to wait for the file to be completely downloaded we need a different delay. The simplest one follows.

If this simple wait 20 seconds is not adequate, we could check the size of new files every xx seconds; when the size does not change then assume the download is complete.
# retain code above this point
print "Monitoring %s for new files" % path_to_watch
import time
try:
  old_path_contents = set(os.listdir(path_to_watch))
  while True:
    result = win32event.WaitForSingleObject(change_handle, delay)
    #
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    #
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = set(os.listdir(path_to_watch))
      added = new_path_contents - old_path_contents
      if added:
        time.sleep(20) # wait 20 seconds before copying
        for fn in added:
          ext = os.path.splitext(fn)[1] # get the extension
          if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
            try:
              shutil.copyfile(path_to_watch + fn, destination + fn)
              print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
            except IOError:
              pass
      old_path_contents = new_path_contents
      win32file.FindNextChangeNotification(change_handle)
finally:
  win32file.FindCloseChangeNotification(change_handle)

Open in new window

0
 
BillDLCommented:
Hi Mahesh

I am curious to know if you have changed the location of the Internet Explorer cache from the standard Temporary Internet Files folder to the user's Temp folder.  When I open pages in Internet Explorer and play wav, mp3 and wma files, or open *.rtf files, these files are cached in my Temporary Internet Files folder and NOT under my %TEMP% folder.

I didn't make a mistake when I commented earlier.  I assumed that YOU had made the mistake, but your response has me puzzled.
0
 
maheshr2k3Author Commented:
Hi BillDL,

You have not done any mistake in your first comment at all and really explained with painstaking effort the entire procedure of how to build the logic of the code when dealing with copying files from the temporary internet files folder. And neither have i changed the location of the temporary internet files folder to temp. The primary reason being that the "particular website which is in question" uses the %Temp%$ folder instead of the %temporary internet files" folder to download the voice files and word files. So i was just concentrating on the userprofile\Temp folder and not the other folders.

I do hope this explanation answers your concerns..
0
 
maheshr2k3Author Commented:
Hi Ramrom,

The wait before copy statement added the ultimate functionality to the code in discussion and the files are getting copied fully now with no broken parts.

Before finally closing the question , couple of quick queries on--

1. how to run this code as a background process or service at windows startup with no User Interface or dos windows?
2. how to do a map to a network drive in python code

The above two are essential as it will enable the automatic copy / updation of files downloaded in the temp folder of couple of computers which work on the website onto a shared network drive.

Best Regards,
Mahesh
0
 
BillDLCommented:
Thank you for that Mahesh.  It did have me puzzled, but I now understand the reasons.  I'm glad you are getting such excellent help from a real coding expert.  The only thing I knew about Python is that it is a large snake found in warm countries that crushes its prey to death and swallows it whole ;-)  It does seem to be quite a versatile language though, and I am watching each comment with interest.
0
 
ramromconsultant Commented:
BillDL: Python the language's name was taken from Monty Python's Flying Circus!

"real coding expert" - I like that. I guess it applies to me. Blush!

I've been programming since 1961! I "evolved" as it were through all the stages of machine language, assembler, spaghetti code, structured programming, structured design, structured analysis, modularity, data-flow, GUI, database, OOP. Not to mention Google which led me to Tim Robert's "pattern" for waiting for folder events. My first language of choice was PL/I, later replaced by APL. Python and APL stay at the top of my list.
0
 
ramromconsultant Commented:
1. how to run this code as a background process or service at windows startup with no User Interface or dos windows?

To run as a process use pythonw.exe instead of python.exe.

You can launch the program thru the startup folder.

C:\Documents and Settings\Administrator\Start Menu\Programs\Startup

Administrator may need to be replaced with another user name. Right-click Start -> Explore will start you on the right path.

Or you can use the Schedule Service to start the program.

2. how to do a map to a network drive in python code

This is a separate topic, out of scope of the original question. Please complete this question and start a new one.
0
 
BillDLCommented:
Hee, hee.  Well, I've been around since the year after you started programming.

Hey, I thought I was getting old when I think back to the time as a young lad when I was dumped into a control room and had to do endless vehicle ID and criminal conviction checks using some old green-screen terminals and had to get the syntax correct.  All I recall of that is comands like:
STX# VU <vehicle number>
and then further commands were necessary to send it to print.
The old Telex machine as the size of a Volkswagen Beetle's front-trunk, and the keys were like the pedals of same vehicle.  My fingers had biceps and triceps then!!  I had to type in a sequence of field codes and textual comments in strict sequence from triplicate pro-forma reports that were punched into thin "ticker tape" live.  It was a noisy environment and stressful getting it done in one pass with no errors.  Once done I reinserted the tape and transmitted the reports to one of Scotland's first Government-owned Dot Matrix printers at HQ.  I recall going to see that printer at HQ, and people were still in awe at the speed and relative silence.  They could actually have it sitting in the Control Centre and still hear themselves think rather than having it in the condo-sized "room" containing massive banks of reel-to-reel tape spools constantly whirling back and forwards seeking data.

Amazing changes, even in such a short space of time.
Sorry about the off-topic digression Mahesh ;-)
0
 
ramromconsultant Commented:
I should clarify: "run as a process" means no dos window or user interface.
0
 
maheshr2k3Author Commented:
Greetings Ramrom,

The code running as a process using pythonw.exe runs fine, but the problem now is with the time.sleep.

Let me explain this way-- the time.sleep works perfectly when the file size is lesser than 2MB. But if the filesize is approx 3 to 5MB i have to use time.sleep = 60 for it to get copied as the website in question is pretty slow sometimes, then the other files which were downloaded in the meantime are not copied due to their non-existence as they would have been overwritten with the newer file in the temp folder as the code is in waiting status for this present file.

In your earlier post, you had mentioned ( If this simple wait 20 seconds is not adequate, we could check the size of new files every xx seconds; when the size does not change then assume the download is complete.)

your suggestion is the ultimate solution to this problem , as the code takes care of the copying as soon as it deems that the file has been completely downloaded and proceeds to the next file immediately without waiting for a "fixed time" as in the time.sleep statement.

Kindly suggest the changes which has to be done in the most recent code which is functional for the need.

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
OK - will think "out loud" for a moment:
We need to maintain a collection of files-in-progress.
When a new file is detected we add it to this collection.
In the while loop, which is executed every delay milliseconds, we examine the files in the files-in-progress collection to see if their size has changed.
When we find one whose size has not changed then we copy it and remove it from the files-in-progress collection.
We use a dictionary for the collection. filename is key, value is size.
Delay should be long enough to ensure that a file that is growing will change in size from one cycle of the loop to the next.

# retain prior code
print "Monitoring %s for new files" % path_to_watch
import time
filesInProgress = {}
try:
  old_path_contents = set(os.listdir(path_to_watch))
  while True:
    result = win32event.WaitForSingleObject(change_handle, delay)
    # If the WaitFor... returned because of a notification (as
    #  opposed to timing out or some error) then look for the
    #  changes in the directory contents.
    if result == win32con.WAIT_OBJECT_0:
      new_path_contents = set(os.listdir(path_to_watch))
      added = new_path_contents - old_path_contents
      for fn in added:
        ext = os.path.splitext(fn)[1] # get the extension
        if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
          filesInProgress[fn] = 0
          print "Monitoring %s%s" % (path_to_watch, fn)
      old_path_contents = new_path_contents
    for fn, oldSize in filesInProgress.items():
      newSize = os.path.getsize(path_to_watch + fn)
      if oldSize == newSize:
        del filesInProgress[fn]
        try:
          shutil.copyfile(path_to_watch + fn, destination + fn)
          print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
        except IOError:
          pass
      else:
        filesInProgress[fn] = newSize
    win32file.FindNextChangeNotification(change_handle)
finally:
  win32file.FindCloseChangeNotification(change_handle)

Open in new window

0
 
maheshr2k3Author Commented:
Hi Ramrom,

The sequence of events you guessed is perfect..

When the code is executed it terminates with an error as below--

C:\Python25>python.exe monitort1.py
Monitoring w:\ for new files
Traceback (most recent call last):
  File "monitort1.py", line 63, in <module>
    win32file.FindNextChangeNotification(change_handle)
pywintypes.error: (56, 'FindNextChangeNotification', 'The network BIOS command limit has been reached.')

C:\Python25>

Did a google search and found out that the MaxCmds registry values has to be changed as in  
http://support.microsoft.com/kb/810886

Changed the registry values to 65535

Increased the delay to 40000 and the code is presently running, Will post back in a few minutes . Kindly suggest the optimal value of Delay = ?

Best Regards,
Mahesh
0
 
maheshr2k3Author Commented:
Hi Ramrom,

The code executed for some time (about 30 minutes) when the Delay was set to 40000 and later terminated. Kindly advise if there is an alternative to the FindNextChangeNotification logic. Will be back tomorrow ..till then bye and take care

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
We could get rid of the FindNextChangeNotification logic and just re-evaluate the folder contents every so often.

Here is a complete replacement program except the path_to_watch and destination assignments.

Delay is now in seconds rather than milliseconds.
delay = 40 # seconds
 
import os
import shutil
import time
print "Monitoring %s for new files" % path_to_watch
filesInProgress = {}
old_path_contents = set(os.listdir(path_to_watch))
while True:
  time.sleep(delay)
  new_path_contents = set(os.listdir(path_to_watch))
  added = new_path_contents - old_path_contents
  for fn in added:
    ext = os.path.splitext(fn)[1] # get the extension
    if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
      filesInProgress[fn] = 0
      print "Monitoring %s%s" % (path_to_watch, fn)
  old_path_contents = new_path_contents
  for fn, oldSize in filesInProgress.items():
    newSize = os.path.getsize(path_to_watch + fn)
    if oldSize == newSize:
      del filesInProgress[fn]
      try:
        shutil.copyfile(path_to_watch + fn, destination + fn)
        print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
      except IOError:
        pass
    else:
      filesInProgress[fn] = newSize

Open in new window

0
 
maheshr2k3Author Commented:
Hi Ramrom,

Firstly the time.sleep problem is resolved.

The revised code runs fine for sometime and stops with the following output--

Monitoring w:\ for new files
Monitoring w:\~WRD0001.doc
Traceback (most recent call last):
  File "C:\Program Files\Python25\monitortlat.py", line 22, in
    newSize = os.path.getsize(path_to_watch + fn)
  File "C:\Program Files\Python25\lib\ntpath.py", line 228, in getsize
    return os.stat(filename).st_size
WindowsError: [Error 2] The system cannot find the file specified: 'w:\\~WRD0001.doc'

I think if a statement excluding files such as ~WRD*.doc this problem can be overridden. But this suggestion is left to your wise discretion.

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
The obvious guess is that the file was created, we began monitoring, then it was deleted before we could copy it.

The leading ~ sounds like a temporary file created by MS Word. Could that be the case. Or is it a file that we should have copied before it got deleted?
0
 
maheshr2k3Author Commented:
Hi Ramrom,

Your guess is perfect..it is a MS word temporary document which is of no use.There is no need to copy this ~WRD*.doc file at all.

Best Regards,
Mahesh
0
 
ramromconsultant Commented:
I hope you are learning Python as part of this exercise. Could you make a guess as to what you could add to the program to filter out this kind of file?

Or would it suffice to test for the existence of a file before attempting to get its size, and if it is no longer there remove it from the filesInProgress container? See the os.path module for a function that will help you.

If these hints are inadequate let me know.
0
 
maheshr2k3Author Commented:
Hi Ramrom,

Making an sincere effort to understand the coding part and reading on a couple of websites--

The second part for excluding files is somewhat like -- filter action="drop" type="wildcard" pattern="~WRD*.*"

Currently i am just trying to understand where to put in this line and testing it out.

Will post back later..


Best Regards,
Mahesh
0
 
ramromconsultant Commented:
OK - I did not realize it would be a large task for you.

Here is a revision to do my 2nd idea; ignore any file that goes away.

I added

    if os.path.exists(path_to_watch + fn):

to see if the monitored file is still there. If not I remove it from the filesInProgress collection.
delay = 40 # seconds
 
import os
import shutil
import time
print "Monitoring %s for new files" % path_to_watch
filesInProgress = {}
old_path_contents = set(os.listdir(path_to_watch))
while True:
  time.sleep(delay)
  new_path_contents = set(os.listdir(path_to_watch))
  added = new_path_contents - old_path_contents
  for fn in added:
    ext = os.path.splitext(fn)[1] # get the extension
    if ext in ('.doc', '.rtf', '.wav', '.vox', '.dss'):
      filesInProgress[fn] = 0
      print "Monitoring %s%s" % (path_to_watch, fn)
  old_path_contents = new_path_contents
  for fn, oldSize in filesInProgress.items():
    if os.path.exists(path_to_watch + fn):
      newSize = os.path.getsize(path_to_watch + fn)
      if oldSize == newSize:
        del filesInProgress[fn]
        try:
          shutil.copyfile(path_to_watch + fn, destination + fn)
          print "Copied %s from %s to %s" % (fn, path_to_watch, destination)
        except IOError:
          pass
      else:
        filesInProgress[fn] = newSize
    else: # file went away; stop monitoring it
      del filesInProgress[fn]
      print "Stop monitoring %s%s" % (path_to_watch, fn)

Open in new window

0
 
maheshr2k3Author Commented:
Hi Ramrom,

this final code works perfectly without any errors. later i will try to incorporate the exclude statement to your previous code if at all this generates any errors. This will also help me to do some dedicated work towards learning python as i have found out some good websites where there is information on running python code as a windows service etc etc.

My next task is to monitor and copy the files in the "Temporary Internet Files" folder..Kindly give some pointers / websites / information where i can read , understand the functionality involved in working with system folders.

Best Regards,
Mahesh
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 16
  • 15
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now