Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: C# Directory Masking

Use the Session.EnumerateRemoteFiles method to retrieve list of the files and call the Session.MoveFile for each.

https://winscp.net/eng/docs/library_session_enumerateremotefiles

Note that the Session.MoveFile officially does not support masks. So your current code may break in future versions of WinSCP anyway.
Aldor

C# Directory Masking

Hi all,

I am currently putting together a small tool to move files into a folder and then transfer them to my local machine. I have the transfer etc all working perfectly, but I am struggling with the move section.

Scenario: Source location has many folders and loose files... I intend to collect the loose files, put them into a new folder and then transfer the new folder. BUT - I only want to move the "Loose" files into a new folder and ignore any other folders in the source location.

e.g. I have a source with:

Folder 1
Folder 2
Folder 3
loose file 1
loose file 2
loose file 3
loose file 4

I want to create a "Folder 4" and then move the 4 loose files into the folder, then transfer.

so it becomes:
Folder 1
Folder 2
Folder 3
Folder 4

I then transfer the content of Folder 4 to my local machine. As i said, my transfer works fine - no issues here, the creation of folder 4 works fine, no issues there either. My issue is the moving of loose files into the "Folder 4" - it keeps trying to move the folders too. I have tried putting "*" at the start of the source folder, but it crashes.

as a note - every time I run this, there will be different folders, so i cant hard code what to ignore.

my code so far for the move:

session.Open(sessionOptions);


      string localPath = txtBoxLocalDest.Text;
      string remotePath = txtBoxRemoteLoc.Text;
      string movePath = remotePath + "/*";
      string remoteFilePath = remotePath + "/" + txtBoxNewFolderName.Text;

      // Create remote subdirectory, if it does not exist yet
      if (!session.FileExists(remoteFilePath))
      {
          session.CreateDirectory(remoteFilePath);
      }
      //move loose files into new subdirectory
      session.MoveFile(movePath, remoteFilePath);


thank you for any support provided.