Removing all content except for a particular folder

Advertisement

Uploads
Joined:
Posts:
7

Removing all content except for a particular folder

Hi all,

1) I want to remove and then re-upload new versions of everything on an SFTP server excluding a certain directory but I am running into trouble. Hoping someone can help...


2) Broken down by steps:

Step A)
I have an SFTP server with content that looks like this:

SFTP Server Content BEGINS ----------------------
/folder-1/
/folder-without-write-access/
/folder-3/
/file-1
/file-2
/file-3
SFTP Server Content ENDS ----------------------

What I want to do is remove all of the content from the SFTP server except for the single folder that I do not have write access to and therefore cannot remove. The content would then look like this:

SFTP Server Content BEGINS ----------------------
/folder-without-write-access/
SFTP Server Content ENDS ----------------------

Step B)
Then I want to upload new copies of everything except for the single folder that I do not have write access to and therefore can't overwrite. The content would then look like this:

SFTP Server Content BEGINS ----------------------
/folder-1/
/folder-without-write-access/
/folder-3/
/file-1
/file-2
/file-3
SFTP Server Content ENDS ----------------------


3) I want to accomplish this using the "winscp.com /script=SCRIPTNAME" command where the SCRIPTNAME file contains a list of commands for WinSCP to execute. (I have used this method before)

I normally issue a "rm *" command in the SCRIPTNAME file, but unfortunately I can't seem to find a way to get the "rm" command to remove everything except for ignoring a particular folder. If I do run "rm *" then I get an error (and WinSCP stops removing content) because WinSCP cannot remove that folder since I don't have write access to it.

I would just write individual "rm/rmdir" commands for each folder/file, but the number of folders/files and their actual names change except for the /folder-without-write-access/ folder whose name will stay the same.

I want to avoid:
- I want to avoid using the "WinSCP .NET assembly and COM library".
- I want to avoid having to generate the SCRIPTNAME file on the fly.
- I want to avoid relying on the synchronize command without first deleting the content. ie I definitely want to delete the content first.

Does anyone know how to fix this issue while still using the "winscp.com /script=SCRIPTNAME" method?

Thanks!

Reply with quote

Advertisement

unix-bomber
Joined:
Posts:
6
Location:
Virginia

Re: Removing all content except for a particular folder

Use a directory filemask

On page https://winscp.net/eng/docs/file_mask
"For example following mask includes all JPG and GIF images, but excludes those starting with 2010 and 2011: *.jpg; *.gif | 2010*; 2011*."

On same page,
Directory Mask
To use the mask for directories, append a slash to the end, e.g. images/. The mask */ matches any directory.

Potential Example:
-filemask=|excludeddir/

*Note
If it's by choice, you're missing out on some aaaawesome functionality by not using the .net language, glhf

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: Removing all content except for a particular folder

Uploads wrote:

I want to avoid:
- I want to avoid using the "WinSCP .NET assembly and COM library".
- I want to avoid having to generate the SCRIPTNAME file on the fly.
- I want to avoid relying on the synchronize command without first deleting the content. ie I definitely want to delete the content first.
Then you have ruled out all the options to implement this.

Reply with quote

Uploads
Joined:
Posts:
7

Re: Removing all content except for a particular folder

Hi unix-bomber,

Thanks for the response :)

1) I already tried to use the file mask / directory mask (https://winscp.net/eng/docs/file_mask) features but was unable to get them to work. This is what it seems like from what I read and tried:

a) the "synchronize" command seems to take fully fledged "masks"

b) the "rm" command (which is what I am trying to get to work) seems to take "wildcards" (as per https://winscp.net/eng/docs/scriptcommand_rm) but does not seem to take fully fledged "masks" even though the word "wildcards" on that page does link to the page about masks.

c)
Potential Example:
-filemask=|excludeddir/

Unfortunately the only place I have seen "-filemask" documented is for the "synchronize" command. (I want to "rm" the content first)

Am I wrong in thinking "-filemask" does not work for "rm"?

c) Here are examples of what I have already tried:

- 1st command I tried: rm *|folder-without-write-access/
Result:
Selecting files using a path ending with slash is ambiguous. Remove the slash to select the folder. Append * mask to select all files in the folder.
Error listing directory '*|folder-without-write-access'.
No such file or directory.
Error code: 2
Error message from server: No such file
(A)bort, (R)etry, (S)kip: Abort


- 2nd command I tried: rm *|folder-without-write-access
Result:
No file matching '*|folder-without-write-access' found.


- 3rd command I tried: rm |folder-without-write-access
Result:
Can't get attributes of file '|folder-without-write-access'.
No such file or directory.
Error code: 2
Error message from server: No such file

2) Have you actually gotten the "rm" command to work with fully fledged masks? (ie more than just a wildcard) If so, can you please send me an example of an "rm" command containing a fully fledged mask that worked for you?

3)
*Note
If it's by choice, you're missing out on some aaaawesome functionality by not using the .net language, glhf

I know, the "WinSCP .NET assembly and COM library" stuff looks very powerful, but I am trying to avoid having to go the full on programming route.

Thanks again

Reply with quote

Uploads
Joined:
Posts:
7

Re: Removing all content except for a particular folder

Hi martin,

Then you have ruled out all the options to implement this.
Are you saying that the "rm" command does not accept fully fledged masks?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: Removing all content except for a particular folder

Uploads wrote:

Are you saying that the "rm" command does not accept fully fledged masks?
It does not support exclude masks.
It was not explicitly documented, I've added that now:
https://winscp.net/eng/docs/file_mask#include_exclude
Though exclude mask is actually just a special case of "combining" masks, for which this restriction was documented.

Reply with quote

Uploads
Joined:
Posts:
7

Hi Martin,

Thank you for the follow-up response. When you say that you documented it here https://winscp.net/eng/docs/file_mask#include_exclude are you talking about this part:
"Mask can combine include and exclude mask separated by pipe (|). You cannot use this in source parameters of script commands and .NET assembly methods.1"

Isn't that incorrect? I am successfully using combined masks in the script commands that I send to WinSCP via a script text file. Maybe I am just misunderstanding what you mean by "source parameters".

Either way, thank you for the help.

Reply with quote

Uploads
Joined:
Posts:
7

Re: Removing all content except for a particular folder

Hi all,

I was able to find a work around. I am posting this for anyone in the future who has the same problem:

Instead of using the rm command which does not support masks (but does support wildcards) you can use a synchronize command to remove all of the files on the remote server.

Command 1: synchronize remote -delete -filemask="|folder-without-write-access/" "C:/intentionally-empty-directory"
This command removes the content from the remote server. folder-without-write-access is the folder that you want to avoid trying to overwrite/remove and C:/intentionally-empty-directory is an intentionally empty folder that you need to create locally so that when WinSCP runs the synchronize command it synchronizes an empty folder, ie it removes the content from the server (except for the folder-without-write-access folder) as desired.

Command 2:
synchronize remote -filemask="|folder-without-write-access/" "C:/directory-containing-content-to-deploy"
This command deploys your new content to the remote server. folder-without-write-access is the folder that you want to avoid trying to overwrite and C:/directory-containing-content-to-deploy is a folder that contains the content that you want to deploy to the server. The "-filemask" here is not necessarily needed, but it will avoid the script failing if you end up with a folder named folder-without-write-access inside of C:/directory-containing-content-to-deploy by accident, etc.

Reply with quote

Advertisement

You can post new topics in this forum