Seiten

Posts mit dem Label python script to get file or folder status in windows werden angezeigt. Alle Posts anzeigen
Posts mit dem Label python script to get file or folder status in windows werden angezeigt. Alle Posts anzeigen

Mittwoch, 21. August 2013

Simple Batch-Script using the CLI of Dropbox

Today I wanted to write a small batch script for Windows, which has to check if dropbox is still syncing or a specific folder isn't synced yet. If the specific folder is synced, then the batch file will start an other programm which needs consitent files for running.

First I have to download the latest 64bit version of Python 2.x.x . You have to download the version 2 because the script bases on the syntax of version 2.

In Addition I had to download one packages:

I found a code on dropboxwiki that retunrs the status of a file or a folder within the dropbox folder.
I run the Python Compiler GUI and copied the code into the windows, saved it as a *py data and checked if it returns the status of the dropbox folder. Summarized, after two days of tries and errors it works fine


Code by Steve H.
(http://www.dropboxwiki.com/dropbox-addons/python-script-to-get-file-or-folder-status-in-windows)
import win32pipe, win32ts, win32api, pywintypes, struct, sys
def dropbox_path_status(pathname):
    return ['dropbox not running',
            'not in dropbox',
            'up to date',
            'syncronising',
            'sync problem'][dropbox_path_status_code(pathname)+1]
def dropbox_path_status_code(pathname):
    processid = win32api.GetCurrentProcessId()
    threadid = win32api.GetCurrentThreadId()
    request_type = 1
    wtf = 0x3048302
    pipename = r'\\.\PIPE\DropboxPipe_' + \
               str(win32ts.ProcessIdToSessionId(processid))
    request = (struct.pack('LLLL', wtf, processid, threadid, request_type) + \
              pathname.encode('utf-16le') + (chr(0)*540))[0:540]
    try:
        response = win32pipe.CallNamedPipe(pipename, request, 16382, 1000)
    except pywintypes.error, err:
        if err[0] == 2:
            return -1
        else:
            raise
    else:
        return int(response[4:-1])
if __name__ == "__main__":
    if len(sys.argv) > 1:
        print 'checking status of', sys.argv[1]
        print dropbox_path_status(sys.argv[1])
    else:
        print >> sys.stderr, 'pathname required'

Note:  The code had two smaller mistakes in line 30 and 34. The compiler couldn't unterstand " > ". This stands for the character " > ".



Finally I wrote a batch file which repeatly determines the status of the dropbox folder and wait until synchronization has been completed.

Following I show you my code with some comments.


@echo off ::Zählervariable i wird auf 1 gesetzt SET i=1 :LOOP echo %time% : Start "C:\Program Files (x86)\Python26\python.exe" test.py "C:\Users\NAME\My Dropbox\Dateien\Thunderbird-Profil" > ausgabe.txt ::Sucht nach dem String "up to date" exakter Wortlaut und ohne Beachtung von Groß-/ Kleinschreibung FINDSTR "up to date" ausgabe.txt ::Errorlevel gibt bei Erfolg 0 zurück und wenn nicht dann alles was größer ist als 0 ::IF unterwucht immer ob errorlevel >= ist; nicht ausschließlich = IF errorlevel 1 ( echo %time% : Fehler ::Zaehlervariable i wird um eins erhöht /A bedeutet dass eien Rechenoperation ausgeführt wird SET /A i=i+1 ::GTR bedeutet "größer" IF %i% GTR 11 GOTO END ping -n 10 127.0.0.1>nul GOTO LOOP ) ELSE ( echo %time% :kein Fehler echo %time% : Thunderbird wird gestartet start "Thunderbird" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" exit ) :END echo %time% : Timeout after %i% tries. pause

First the Python Interpreter executes the *py data from above and checks if the dropbox folder is synchronized or not. The expression the function gives back is stored in a temporarly data.
After that the programm tries to find the string "up to date". If the string was found (errorlevel < 1), then a third program starts an this scripts end. Otherwiese a counter variable increases until it reaches the value of eleven. Then the prograam waits until the script sends ten pings to localhost as a workaround function to interrupt the programm. If the string wasn't found the assumption is made that the synchronization was succesfull and a second program starts. Finally if after eleven rounds no complete synchronization was found, the programm ends and shows a timeout error.



With kind regards