2

I have two users local to an office that need to scan documents then have them automatically upload to their remote Terminal Server. I can set the scanning software to save the files to a certain folder, but I would like to automatically upload them to the TS so they don't have to leave their session to upload the files. What is a good folder watcher for xp that I can use to automatically upload these files?

The server is Win 2000 and I am not sure how this is generally done on windows. I understand I can use WinSCP as a scriptable ftp client, but I don't know what tool is usually used for watching a folder for changes. Intuition says Powershell, but I don't know. My Python isn't up to snuff and I don't want to install it on their computers, but that might be a last option.

---This is what I found looking around.--- This timely thread from today shows a good utility on Debian Linux (one hyper link for new users)/questions/50127/how-to-automatically-run-a-script-when-the-contents-of-a-directory-changes-in-lin And this thread is the closest I found on serverFault, but goes the wrong way. Some kind of Auto-downloader from an FTP Site?

(META ps Is there a way to delete tags. There is one tag for 'uploads' that should probably be 'upload'. No need for plurals.)

Radix
  • 33
  • 1
  • 5

3 Answers3

5

You can use WinSCP to perform this kind of automatic upload. It's normally used with SFTP or SCP but it supports plain FTP as well (your server may actually be capable of SFTP or SCP) and this can be automated with their automation scripting:

WinSCP Automation Guide

The specific command you're looking for is keepuptodate:

http://winscp.net/eng/docs/script_commands

Martin Prikryl
  • 7,756
  • 2
  • 39
  • 73
Dave Forgac
  • 3,546
  • 7
  • 37
  • 48
  • So, what you're telling me is RTFM :) thanks for holding your tongue. Maybe I should have read the whole WinSCP site before I began looking for a watching program. I'll figure this out tomorrow and give you the check now. Thank you very much. – Radix Aug 05 '09 at 03:46
1

The problem with keepuptodate is, that it perform whole synchronisation, which is slow. With a little bit of scripting it is possible to upload only changed files after they are modified.

You can use FolderMon as a folder watcher. Then process it's output by Gawk and of course use WinSCP for uploading. The whole process can be controlled by simple batch script:

REM Local directory (recursive). REM %CD% - current working directory. Must end with '\'. set "localPath=%CD%\"

REM Remote directory, must end with '/'
set "remotePath=/some/remote/directory"

REM Name of the stored session in WinSCP
set "session=stored@session.com"

REM --------------------------------------------------------

REM Escape local path - replace '\' with '\\'.
set "escapedLocalPath=%localPath:\=\\%"

foldermon -lastwrite -subdirs %localPath% | ^
gawk -f autoupload.awk -v "localPath=%escapedLocalPath%" -v "remotePath=%remotePath%" -v "session=%session%" | ^
winscp.com /console | ^
gawk "{ if (!/#aaaaaaaaaaaaaaaaa/) print; }"

And of course you need that gawk script (autoupload.awk): BEGIN { # Variable to fix bug (feature?) in foldermon. # When file is saved, foldermon reports two changes in the same second. lastLocalFile = "" lastTime = ""

    # Init and connect
    print "#remotePath=", remotePath
    print "#localPath=", localPath
    print "#session=", session

    print "option batch abort"
    print "option confirm off"
    print "open " session
    print "option transfer binary"
    #print "cd " remotePath

    # Flush AWK buffer.
    for (i=0; i<41; i++)
        print "#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
}

{
    if (match ($0, /^([:0-9]*) File modified: (.*)$/, matchRow)) {
        # File changed.

        time = matchRow[1]
        file = matchRow[2]
        localFile = localPath file

        # Don't upload same file twice immediately.
        if (localFile!=lastLocalFile || lastTime!=time) {
            lastLocalFile = localFile
            lastTime = time

            # Extract subdirectory from change report and convert '\' to '/'.
            match (file, /^((.*[\\])*).*$/, matchPath);
            path = matchPath[1]
            gsub(/\\/, "/", path)

            # Change remote dir and upload.
            #print "cd " remotePath path
            print "put", localFile, remotePath path

            # Flush AWK buffer.
            for (i=0; i<41; i++)
                print "#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        }
    }
}

END {
    # Disconnect.
    print "close"
    print "exit"
}

You can download zip file with everything included from here.

Strix
  • 111
  • 1
0

I was having some problems with the auto upload script provided by Strix and had to modify it slightly to work with Sublime Text 3 (unsure of Sublime Text 2 will work the same way).

If you were having problems with his script not working or uploading blanks (and thereby EVERYTHING) then replace the middle function of autoupload.awk with this modified version:

  {
    if (match ($0, /^([:0-9]*) File modified: (.*)$/, matchRow)) {
      # File changed.

        time = matchRow[1]
        file = matchRow[2]

      # Sublime Text 2/3 creates several files and appends some temporary characters after the original file name like this: myfile~RF34be688.TMP.
      # The original version of this program didn't handle this correctly and uploaded everything to the server
      # until you killed the script
      # This if statement below handles this by matching for only the lines that have the ~ (tilde chracter)

      if ( match (file,/~/) ) {

          tildePOS = match (matchRow[2],/~/)
          file = substr(matchRow[2], 1, tildePOS - 1)

          localFile = localPath file

          # Don't upload same file twice immediately.

          if (localFile!=lastLocalFile || lastTime!=time) {
            lastLocalFile = localFile
            lastTime = time

            # Extract subdirectory from change report and convert '\' to '/'.
            match (file, /^((.*[\\])*).*$/, matchPath);
            path = matchPath[1]
            gsub(/\\/, "/", path)

            # Change remote dir and upload.
            #print "cd " remotePath path
            print "put", localFile, remotePath path

            # Flush AWK buffer.
            for (i=0; i<41; i++)
              print "#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
          }
      }
    }
  }

Hopefully this helps someone out!

DudeBacca
  • 1
  • 1