How do I create a batch file that copys and deletes files off a pi into a pc?

2

1

I have a script on a Pi3 that creates files in:

home/pi/data/*csv

I am trying to write a bash script that uses PuTTY to copy and erase those files into a folder on my computer.

This is what I have so far:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

set "data='C:\Users\Recorded Data\%fullstamp%'"

echo Making Directory at: %data%
md %data%

cd C:\Programfiles
pscp @pi:/home/pi/data/*.csv %data%
pause

I saw that I have to use PSFTP to delete the files, but I also need to add a script to log into the pi? I'm just kinda lost right now.

jret bullion

Posted 2020-02-12T16:15:48.750

Reputation: 21

Answers

4

Do something like this instead of pscp:

(
    echo get /home/pi/data/*.csv %data%
    echo rm /home/pi/data/*.csv
    echo exit
) | psftp @pi 

Though this is not transitionally safe. If any file is created after get starts, but before rm, it will be deleted without being downloaded.


If you use WinSCP instead, you can use its get -delete command for transactionaly-safe solution:

winscp.com /ini=nul /command ^
    "open sftp://username:password@pi/ -hostkey=""...""" ^
    "get -delete /home/pi/data/*.csv %data%" ^
    "exit"

WinSCP GUI can generate a batch file template like this one for you.

(I'm the author of WinSCP)


See also the same question on Stack Overflow: psftp.exe get files from the server and delete.

Martin Prikryl

Posted 2020-02-12T16:15:48.750

Reputation: 13 764