Как составить скрипт удаления отосланных winscp файлов
Перейти к содержимому

Как составить скрипт удаления отосланных winscp файлов

  • автор:

Как составить скрипт удаления отосланных winscp файлов

I have to pick (remove) the files with file mask FileName_A_* and FileName_B_* from SFTP location and place them in an sharedrive.

I tried using WinSCP. I have created an HourlyFile.txt file with below code and placed it under C:\Program Files (x86)\WinSCP . Another batch file HourlyFile.bat to execute the script from HourlyFile.txt

I tried with below options to delete the file but got the error message «Unknown command». Also the above code is copying subfolder from /outbound/test/ , which it should not.

WinSCP — delete file after successful transfer (put/get)

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.

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.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 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. 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:

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 and scripting commands.

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

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

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.

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.

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 (website and instructions).

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. 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.

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

My New Knowledge

Python, IaC, algo trading, programming, server administration, Linux, PHP, CakePHP, PostgreSQL, MySQL, Joomla .

Wednesday, December 22, 2010

winscp delete after successful transfer (put/get)

I’ve been using WinScp and a script to compress files at maximum compressión, name the file as name_YYMMDD-HHMM.rar, and send them to a remote server; then I wanted to delete local files after a successful transfer. Here’s my batch file (it works on WinXP). :

;FOR /F «tokens=1-4 delims=/ » %%I IN (‘DATE /t’) DO SET mydate=%%K%%J%%I
;
;FOR /F «eol=; tokens=1-2 delims=: » %%a IN (‘TIME /t’) DO SET mytime=%%a%%b
;
;ECHO The value is «%mydate% %mytime%»
;
;set varname=myname-%mydate%-%mytime%.rar
;
;rar m -m5 % varname % «FolderWithFilesToCompress\*.jpg»
;echo % varname %

start /min winscp.exe /console /command «option batch on» «open lab@64.150.188.62» «option confirm off» «put -delete % varname %» «exit»

To call the batch file automatically in a minimized mode, I call from window’s Task Scheduler this vbs script (the last 0 means minimized):

Set WinScriptHost = CreateObject(«WScript.Shell»)
WinScriptHost.Run Chr(34) & «C:\path\to\batch\mybatch.bat» & Chr(34), 0
Set WinScriptHost = Nothing

And here the documentarion on WinScp

WinSCP Free SFTP, FTP and SCP client for Windows

Script Commands

  • General Syntax
  • call
  • cd
  • chmod
  • close
  • exit
  • get
  • help
  • keepuptodate
  • lcd
  • lls
  • ln
  • lpwd
  • ls
  • mkdir
  • mv
  • open
  • option
  • put
  • pwd
  • rm
  • rmdir
  • session
  • synchronize

In its scripting functionality, WinSCP supports set of commands described below.

You can see the very same help for the commands as shown here, if you type command help directly in console.

General Syntax

Command parameters that include space(s) have to be enclosed in double-quotes. To use double-quote literally, double it:

You can use environment variables in the commands, with syntax %NAME% 1):

You can reference script arguments (passed on command-line using parameter /parameter ) using syntax %N% , where N is ordinal number of argument1):

Note that WinSCP treats filenames in case sensitive manner. So even if your server treats filenames in case insensitive manner, make sure you specify case properly2).

If current session does not allow execution of arbitrary remote command separate shell session will be automatically opened.

The command must not require user input.

Changes remote working directory for active session.

If directory is not specified, changes to home directory.

chmod

Changes permissions of one or more remote files.

mode can be specified as three or four-digit octal number.

Filename can be replaced with wildcard to select multiple files.

close

Closes session specified by its number. When session is not specified, closes currently selected session.

Closes all sessions and terminates the program.

Downloads one or more files from remote directory to local directory.

Downloads one or more files from remote directory to local directory. If only one parameter is specified downloads the file to local working directory. If more parameters are specified, all except the last one specify set of files to download. The last parameter specifies target local directory and optionally operation mask to store file(s) under different name. Destination directory must end with backslash. Filename can be replaced with wildcard to select multiple files. To download more files to current working directory use .\ as the last parameter.

Switch Description
— delete Delete source remote file(s) after transfer .
-resume Automatically resume transfer if possible (SFTP and FTP protocols only). Cannot be combined with -append .
-append Append source file to the end of target file (SFTP protocol only). Cannot be combined with -resume .
-preservetime Preserve timestamp
-nopreservetime Do not preserve timestamp
-speed= Limit transfer speed

Effective options: transfer , confirm , exclude , include , reconnecttime

See also synchronize if you need to transfer modified or non-existing files only.

Displays help for script commands.

Displays list of commands when no parameters are specified. Displays help for each command when some are specified.

keepuptodate

Watches for changes in local directory and reflects them on remote one.

When directories are not specified, current working directories are synchronized. To stop watching for changes press Ctrl-C .

Note: Overwrite confirmations are always off for the command.

Switch Description
— delete Delete obsolete files
-permissions= Set permissions (SFTP and SCP protocols only)
-nopermissions Keep default permissions
-speed= Limit transfer speed

Effective options: transfer , exclude , include , reconnecttime

Changes local working directory for all sessions.

Lists the contents of local directory.

If directory is not specified, lists working directory. When wildcard is specified, it is treated as set of files to list. Otherwise, all files are listed.

Prints current local working directory (valid for all sessions).

Lists the contents of specified directory.

Lists the contents of specified remote directory. If directory is not specified, lists working directory. When wildcard 3) is specified, it is treated as set of files to list. Otherwise, all files are listed.

mkdir

Moves or renames one or more remote files.

Destination directory or newname or both must be specified. Destination directory must end with slash. Operation mask can be used instead of new name. Filename can be replaced with wildcard to select multiple files.

Establishes new connection.

Establishes connection to given host. Use name of the stored session (to open session, stored in folder, use path syntax “folder/session”). You can also specify host , username , port and protocol directly.

Switch Description
-privatekey= Private key path
-timeout= Server response timeout
-hostkey=“ ” Specifies fingerprint of expected SSH host key (or several alternative fingerprints separated by semicolon). It makes WinSCP automatically accept hostkey with the fingerprint. As the hostkey fingerprint contains spaces you need to enclose it in quotes. SFTP and SCP protocols only.
-certificate=“ ” Specifies fingerprint of expected SSL/TLS sertificate (or several fingerprints separated by semicolon). It makes WinSCP automatically accept certificate with the fingerprint. FTPS protocol only.
-passive Passive mode (FTP protocol only)
-implicit Implicit TLS /SSL (FTPS protocol only)
-explicitssl Explicit SSL (FTPS protocol only)
-explicittls Explicit TLS (FTPS protocol only)

option

Shows or sets value of script options.

If no parameters are specified, lists all script options and their values. When one parameter is specified only, shows value of the option. When two parameters are specified sets value of the option. Initial values of some options are taken from application configuration, however modifing the options does not change the application configuration.

Option Values and description
echo on|off
Toggles echoing of command being executed.
Commands affected: all
batch on|off|abort|continue
Toggles batch mode (all prompts are automatically replied negatively). When on , it is recommended to set confirm to off to allow overwrites. With abort script is aborted when any error occurs. With continue all errors are ignored.
Commands affected: nearly all
confirm on|off
Toggles confirmations (overwrite, etc.).
Commands affected: get , put
transfer binary|ascii|automatic
Transfer mode: binary, ascii (text), automatic (by extension).
Commands affected: get , put , synchronize , keepuptodate
exclude
include
clear | [; . ]
Sets exclusion or inclusion masks (only one can be set at time).
Commands affected: get , put , synchronize , keepuptodate
reconnecttime off |
Sets time limit in seconds to try reconnecting broken sessions.
Commands affected: get , put , synchronize , keepuptodate

Aliases: ascii (for option transfer ascii ), binary (for option transfer binary )

Note that resetting the same option overwrites previous values, it does not append.

Uploads one or more files from local directory to remote directory.

If only one parameter is specified uploads the file to remote working directory. If more parameters are specified, all except the last one specify set of files to upload. The last parameter specifies target remote directory and optionally operation mask to store file(s) under different name. Destination directory must end with slash. Filename can be replaced with Windows wildcard3) to select multiple files. To upload more files to current working directory use ./ as the last parameter.

Switch Description
— delete Delete source local file(s) after transfer .
-resume Automatically resume transfer if possible (SFTP and FTP protocols only). Cannot be combined with -append .
-append Append source file to the end of target file (SFTP protocol only). Cannot be combined with -resume .
-preservetime Preserve timestamp
-nopreservetime Do not preserve timestamp
-permissions= Set permissions (SFTP and SCP protocols only)
-nopermissions Keep default permissions
-speed= Limit transfer speed

Effective options: transfer , confirm , exclude , include , reconnecttime

See also synchronize if you need to transfer modified files only.

Prints current remote working directory for active session.

Removes one or more remote files.

If remote recycle bin is configured, moves file to the bin instead of deleting it. Filename can be replaced with wildcard to select multiple files.

rmdir

Removes one or more remote directories.

If remote recycle bin is configured, moves directory to the bin instead of deleting it.

session

Manages opened sessions.

Makes session specified by its number active. When session number is not specified, lists connected sessions.

synchronize

When the first parameter is local , changes from remote directory are applied to local directory. When the first parameter is remote , changes from the local directory are applied to the remote directory. When the first parameter is both , both local and remote directories can be modified.

When directories are not specified, current working directories are synchronized.

Note: Overwrite confirmations are always off for the command.

Switch Description
— delete Delete obsolete files. Ignored for both .
-mirror Mirror mode (synchronize also older files). Ignored for both .
-criteria= Comparison criteria. Possible values are time , size , both and none . Ignored for both mode.
-permissions= Set permissions (SFTP and SCP protocols only)
-nopermissions Keep default permissions
-speed= Limit transfer speed

Effective options: transfer , exclude , include , reconnecttime

XML Log Elements: download (with local or both ), upload (with remote or both ), touch (with remote or both ), chmod (with remote or both and -permissions ), rm (with remote and — delete )

Быстрое удаление папок с большим количеством файлов в WinSCP

в WinSCP, если я выбираю каталог и щелкните правой кнопкой мыши, чтобы удалить, вместо того, чтобы просто удалить каталог, он удаляет все содержимое по одному. В зависимости от содержимого и подключения это может занять очень много времени.

есть ли какой-либо способ, в интерфейсе WinSCP, я могу просто удалить каталог так же быстро, как если бы я использовал команду вроде rm -rf ?

1 ответов

это основано на WinSCP «Полезные команды» запись страницы для «удалить» С некоторыми важными недостающими шагами.

эти шаги вам нужно сделать только один раз:

  1. щелкните правой кнопкой мыши тестовый каталог, чтобы удалить
  2. выбрать пользовательские команды > настройка
  3. Нажмите кнопку «Добавить»
  4. в разделе «Описание» дайте ему имя по вашему выбору, например «удалить»
  5. под «Пользовательская команда» у вас есть выбор:
    • вы хотите, чтобы появилось окно подтверждения? Если да, введите: test «!?Do you really want to delete ?no!» == «yes» && rm -rf !&
    • вы хотите, чтобы он просто мгновенно удалить? Если да, введите: rm -f -r «!»
  6. установите флажок «применить к каталогам»
  7. Нажмите «ОК». Теперь у вас есть опция «удалить» в разделе «пользовательские команды» каждый раз, когда вы щелкните правой кнопкой мыши файл или папку.

теперь вы можете удалить любой файл или сразу папку с этим новым пользовательские команды. Это может занять несколько секунд, чтобы подключиться в первый раз, когда вы делаете это в сеансе

Пакетное удаление файлов на FTP старше x дней

Нашел несколько решений для локального, но нужно одно для действия FTP:

У меня есть ежедневный пакетный сценарий для локального резервного копирования баз данных MySQL, но я также отправляю их по ftp на резервный сервер.

Локально я использую forfiles для удаления файлов старше 14 дней:

То же самое я хотел бы сделать на FTP, когда самые новые файлы резервных копий будут сброшены на FTP-сервер, работающий на Windows Server 2008 R2.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *