WinSCP —  delete file after successful transfer (put/get)
Published: 2020-02-16 22:00:36

Or how to control remotely an Android cellphone to take hundreds of thousands of pictures automatically, write a script to transfer the pictures continuously to a computer, delete them from the cellphone and then transfer the pictures to the cloud.

    TL;DR
    - I use OpenCamera installed in an Android phone to take a picture every 3s (28K pictures a day, 200k a week...).
    - Every 30m I execute from Windows a Batch script that uses WinSCP to transfer files from the cellphone to the computer.
    - I describe how I use multiple open-source or free-to-use apps to automate the process.
    - I share a couple of snippets to automate file transfers with WinSCP script.

![Control remotely an Android cellphone to take hundreds of thousands of pictures automatically, write a script to transfer the pictures continuously to a computer and then to the cloud.](https://cdn-images-1.medium.com/max/2000/1*4cAdavovo3Sn5FnOs7K5Tg.png)*Control remotely an Android cellphone to take hundreds of thousands of pictures automatically, write a script to transfer the pictures continuously to a computer and then to the cloud.*

[WinSCP](https://winscp.net) is my go-to tool whenever I’m in Windows and need to do SFTP transfers to/from a remote server.

From its website:
> # WinSCP is a popular SFTP client and FTP client for Microsoft Windows! Copy file between a local computer and remote servers using FTP, FTPS, SCP, SFTP, WebDAV or S3 file transfer protocols.

Most importantly, you can script WinSCP to do automatic operations.

## The problem

My use case is that I have an Android cellphone (tried iPhone, impossible) taking pictures continuously for several days. After a couple of hours, the cellphone’s memory is full of pictures. The pictures need to be transferred to a computer and deleted from the cellphone, without stopping the camera from taking pictures, or touching the cellphone.

## The Solution

So I decided to do a WinSCP script to transfer the pictures from the cellphone to the computer.

I installed on the cellphone an [SSH Server app](https://play.google.com/store/apps/details?id=com.theolivetree.sshserver). Then I launched a Windows’ batch script that launched the WinSCP `SFTP` transfer script.

The Window’s batch script runs in an infinite loop. It transfers all the files from a folder in the cellphone to a folder on the computer. It deletes the file from the cellphone immediately after it has been transferred to the computer.

When all files have been transferred, it waits for 30 minutes and starts transferring files again.

To implement the waiting period in the script, I used the ping command, specifying a timeout and a number of 1 retries:

    ping 192.168.0.1 -n 1 -w 1800000

I copy below is copied the simple WinSCP script I use to transfer files from the cellphone's SSH Server to a local computer and delete after the transfer.

The first line of the script is automatically generated by WinSCP, and the rest are common [documented options](https://winscp.net/eng/docs/scriptcommand_option) and [scripting commands](https://winscp.net/eng/docs/scripting#commands).

Notice the -delete option, that deletes the file after a successful transfer.

    open sftp://sftpusername:sftppassword@<you server ip>:<port>/ 
    -hostkey="ssh-dss 1024 THE_HOST_KEY" -rawsettings CacheDirectories=0 CacheDirectoryChanges=0 PreserveDirectoryChanges=0

    option batch continue
    option confirm off
    option reconnecttime 10
    option failonnomatch on

    cd /sdcard/path/on/mobile
    lcd "C:\path\on\windows\computer\"

    get -delete -transfer=automatic *.jpg

    exit

I launch the WinSCP transfer script from a batch .bat file periodically, in an infinite loop, like so:

    [@echo](http://twitter.com/echo) off
     
    :retry
     
    WinSCP.COM /ini=nul /script="C:\path\to\winscp_transfer_script.txt"

    echo Waiting 30m (using PING command)
    echo off
    ping 192.168.0.1 -n 1 -w 1800000
    echo on
    goto retry

Notice that WinSCP.COM is in my Window’s `PATH` already.

## Remote control (cellphone and/or computer)

By the way, since I sometimes need to operate my cellphone without touching it, I do it using the excellent and free-for-personal-use app [Teamviewer](https://www.teamviewer.com).

I install this app both on the computer and the cellphone. That way I can control them remotely, without even being physically close to them.

## Cloud Storage

Initially, I set up a cloud-sync service on the computer so that the pictures that were copied to the computer would be automatically uploaded to the cloud. I use a cloud-syncing service, such as [MEGA](https://mega.nz/).

Automatically syncing with MEGA was good for a while, but as the number of pictures increased to tens and hundreds of thousands, it became too slow.

Nowadays I use an `awscli` one-liner to sync the folder to an `Amazon S3` bucket.


## Camera App

To take the pictures I use another wonderful open-sourced app, [OpenCamera](https://play.google.com/store/apps/details?id=net.sourceforge.opencamera&hl=en_US) ([website and instructions](https://opencamera.org.uk/help.html)).

OpenCamera can take pictures *ad-infinitum*, every N seconds, among many, many other wonderful settings.

## Restarting all apps automatically

With such a long-lasting process — days, weeks —, I faced the issue that for some unknown reason Open Camera would stop taking pictures and/or TeamViewer would disconnect.

I solved this using another wonderful app called [Automate](https://play.google.com/store/apps/details?id=com.llamalab.automate&hl=en_US). I use `Automate` to stop and restart all the apps on the cellphone. With `Automate` I can also start taking the pictures automatically by simulating a “tap” on `OpenCamera`’s shutter.

<hr />

This is enough information for now. I’d love to hear your comments and possible improvements to this process.