Winscp script get -delete without deleting empty folders



Winscp script get -delete without deleting empty folders

This question has been answered with powershell, but i am working with good old windows batch script.

So i have an SFTP server, which could have one or more folders. Each folder could have one or more files. Goal is to get it to my local directory and delete FILES only from the SFTP server. So two important things here - when i get files to local, i want to copy whole directory tree from SFTP. When i delete things from SFTP, i want only files to be deleted, leave folders as it is (even if it is empty).

Lets say this is directory tree on SFTP


Now i create a folder locally, call it "mainLocalFolder"

If i do get -delete from mainFolder/ to mainLocalFolder/, i will be able to copy whole directory tree, but it deletes folder1 and folder2 from SFTP, which i dont want. I am thinking of two possible solutions, but lack the scripting ability (as i started development few weeks ago and trying to learn on the job), so any help would be appreciated -

Solution1 -
Once i connect to SFTP, is something like this possible?
For each subdirectory in mainfolder,
      variable currentSubDir = name of current subdirectory
      create folder named currentSubDir in mainLocalFolder
      perform get -delete from currentSubDir to mainLocalFolder/currentSubdir

So basically i want to iterate over each subfolder, create similar subfolder in local, and do "get -delete" for each subdirectory, so that it's internal contents get deleted, but folder itself stays

I believe subfolders are deleted because they are empty. So can we put a dummy file in each subfolder, to prevent deletion?
For each subdirectory in mainfolder,
      put dummy.txt in each folder
perform get -delete filemask=*.csv from mainFolder to localmainFolder

Both solutions involve iterating over unknown number of subdirectories in SFTP mainFolder. I don't know how to do that.

Reply with quote



You can post new topics in this forum