Download file and delete other file(s) in the folder

Advertisement

matt3m
Joined:
Posts:
10
Location:
UK

Download file and delete other file(s) in the folder

Hi,

A few years ago martin helped me with downloading files to my computer and deleting them on my server and today I am wondering if the following is possible.

When my script downloads a file is it possible to have it remove anything else that is within that specific folder it downloads to, for example on my computer I might have D:\Work Files\document.docx and on the server a newer version of this will exist but it could have been saved as a different name or something like document-edit.docx is there a way for the script to download the file and then delete the file that was in that specific folder previously, the extension may not always be .docx, it could be a number of extensions including video files, images, excel spreadsheets.

Hopefully I have explained myself correctly as I tend to not explain things very well.

If you need any more info, let me know.

Thanks :)

Reply with quote

Advertisement

matt3m
Joined:
Posts:
10
Location:
UK

Hi martin,

The script I am currently using is

option batch abort
option confirm off
open sftp://username:password@serveraddress.com -hostkey="ssh-rsa 2048 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx username@serveraddress"
get -delete "server/Work Files/*" "D:\Work Files"

exit

In the Work Files folder I have various other folders with work in and I would only want the files in each specific folder to be delete so if I were to download clientdocument.docx into D:\Work Files\Client1 it would delete only the files in this folder and not the rest in D:\Work Files - is that possible?

Thanks

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,605
Location:
Prague, Czechia

matt3m wrote:

In the Work Files folder I have various other folders with work in and I would only want the files in each specific folder to be delete so if I were to download clientdocument.docx into D:\Work Files\Client1 it would delete only the files in this folder and not the rest in D:\Work Files - is that possible?
That's too unclear to me. If you use synchronize local -delete ..., it would download all modified and new files. And delete all local files which are not present on the server. No matter what (sub)directories are the files stored in.

Reply with quote

matt3m
Joined:
Posts:
10
Location:
UK

Hi martin,

Oh right, I wouldn't want to sync the files as I would want the files to delete on the server upon downloading.

In D:\Work Files I have loads of folders inside with various client work for example...

Client 1
Client 2
Client 3

If I connect to my server I would like a file that is in server\Work Files\Client 1 (server) to download to D:\Work Files\Client 1 (local) but delete any other files in the D:\Work Files\Client 1 folder (local) and also delete the files in server\Work Files (server)

Hope that makes better sense.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,605
Location:
Prague, Czechia

It's not possible with simple scripting. It's doable (and rather easy) with .NET assembly and e.g. PowerShell script.
https://winscp.net/eng/docs/library_powershell
Just retrieve the list of folders on the server with Session.ListDirectories, iterate the list, delete all local files in respective local subfolders, and download all remote files with Session.GetFilesToDirectory.
https://winscp.net/eng/docs/library_session_listdirectory
https://winscp.net/eng/docs/library_session_getfilestodirectory

Reply with quote

matt3m
Joined:
Posts:
10
Location:
UK

Hi martin,

Thanks, I will see what I can but I'm not familiar with .NET or PowerShell so I may give this one a miss and just do it manually if I am unable to get it to work.

Thanks for your help :)

Matt

Reply with quote

Advertisement

You can post new topics in this forum