Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

General Discussion in English
Post Reply
IMTheNachoMan
Posts: 86
Joined: Sun Nov 20, 2022 5:11 am

Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by IMTheNachoMan »

I swear I posted about this before but I can't find it now.

Is it possible to dynamically exclude a folder if the folder has a "special" file in it?

Say I am doing a two way sync of C:\Users\me\Documents and \\server\documents.

Now say I create/have a folder like C:\Users\me\Documents\some\path\ and I want to exclude it. I could update the profile but then I'd have to constantly update the profile every time I have a new folder I want to exclude.

I know I could name the folder special like C:\Users\me\Documents\some\special folder\ and then configure my profile to always exclude special folder but I don't want to have to use a specific folder every time.

Instead, I'm wondering if I can create a file like C:\Users\me\Documents\some\path\syncovery.exclude and that would tell Syncovery to exclude the entire folder C:\Users\me\Documents\some\path\.

That way, every time I have a folder I want to exclude, I could just create a file in that folder like syncovery.exclude.

tobias
Posts: 1670
Joined: Tue Mar 31, 2020 7:37 pm

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by tobias »

Hello,
yes, please see the second OnIncludeItem Sample Script on the PascalScript page:
https://www.syncovery.com/pascalscript/

You need to edit it a little, like this:

Code: Select all

function OnIncludeItem(const FileName, RelativePath: UnicodeString;
        const isRightSide, isFolder:Boolean;
        const FileSize:Int64; const FileAttr:LongWord;
        const Connector: Opaque):Boolean;
begin
  Result:=isRightSide or not isFolder or
    not ConnFileExists(Connector,ConcatPath(ConcatPath(LeftBasePath,RelativePath,Connector),FileName,Connector)+
              '\syncovery.exclude');
  end;
PascalScripts are pasted into the profile via the PascalScript... checkmark on the Job tab sheet.

IMTheNachoMan
Posts: 86
Joined: Sun Nov 20, 2022 5:11 am

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by IMTheNachoMan »

tobias wrote:
Thu Nov 23, 2023 8:20 am
Hello,
yes, please see the second OnIncludeItem Sample Script on the PascalScript page:
https://www.syncovery.com/pascalscript/

You need to edit it a little, like this:

Code: Select all

function OnIncludeItem(const FileName, RelativePath: UnicodeString;
        const isRightSide, isFolder:Boolean;
        const FileSize:Int64; const FileAttr:LongWord;
        const Connector: Opaque):Boolean;
begin
  Result:=isRightSide or not isFolder and
    not ConnFileExists(Connector,ConcatPath(ConcatPath(LeftBasePath,RelativePath,Connector),FileName,Connector)+
              '\syncovery.exclude');
  end;
PascalScripts are pasted into the profile via the PascalScript... checkmark on the Job tab sheet.
A few questions on this script:
  • In the OnIncludeItem function, what does Connector represent? I assume it is a forward or backward slash depending on the OS?
  • What does the ConnFileExists function do? I'm trying to make sense of the line ConnFileExists call but not able to. You're concatenating folder paths but then you concatenate FileName so I'm not sure how that works since we're looking at directories.
  • Will this exclude sub-folders at any level when a parent folder has syncovery.exclude? Using my example, if left is C:\Users\me\Documents and I have the file C:\Users\me\Documents\some\path\syncovery.exclude, will it exclude C:\Users\me\Documents\some\path\?
  • If I get rid of the isRightSide check, will it work for right side exclusions too? Meaning, exclude a folder if the right has the file? So \\server\documents\some\path\syncovery.exclude will exclude \\server\documents\some\path\.

IMTheNachoMan
Posts: 86
Joined: Sun Nov 20, 2022 5:11 am

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by IMTheNachoMan »

tobias wrote:
Thu Nov 23, 2023 8:20 am
Hello,
yes, please see the second OnIncludeItem Sample Script on the PascalScript page:
https://www.syncovery.com/pascalscript/

You need to edit it a little, like this:

Code: Select all

function OnIncludeItem(const FileName, RelativePath: UnicodeString;
        const isRightSide, isFolder:Boolean;
        const FileSize:Int64; const FileAttr:LongWord;
        const Connector: Opaque):Boolean;
begin
  Result:=isRightSide or not isFolder and
    not ConnFileExists(Connector,ConcatPath(ConcatPath(LeftBasePath,RelativePath,Connector),FileName,Connector)+
              '\syncovery.exclude');
  end;
PascalScripts are pasted into the profile via the PascalScript... checkmark on the Job tab sheet.
And something is wrong. I added this script to a profile and did a preview and it's not showing everything it should to transfer. I didn't create any of the exclude files anywhere yet.

tobias
Posts: 1670
Joined: Tue Mar 31, 2020 7:37 pm

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by tobias »

Try this:

Code: Select all

function OnIncludeItem(const FileName, RelativePath: UnicodeString;
        const isRightSide, isFolder:Boolean;
        const FileSize:Int64; const FileAttr:LongWord;
        const Connector: Opaque):Boolean;
begin
  Result:=isRightSide or not isFolder or
    not ConnFileExists(Connector,ConcatPath(ConcatPath(LeftBasePath,RelativePath,Connector),FileName,Connector)+
              '\syncovery.exclude');
  end;

tobias
Posts: 1670
Joined: Tue Mar 31, 2020 7:37 pm

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by tobias »

Hi,
there's actually a better script on the PascalScript page that uses a different hook:

Code: Select all

function OnScanFolder(const FolderLevel: Integer;
        const RelativePath, LeftCompletePath, RightCompletePath: UnicodeString;
        const LeftExists,RightExists:Boolean;
        const LeftConnector, RightConnector: Opaque):Boolean;
begin
  Result:=RightExists or not ConnFileExists(LeftConnector,LeftCompletePath+'\syncovery.exclude');
  end;


This script assumes you will have the exclusions on the source side only, and that the source side is the left side. It will exclude the folder only if it does not exist on the other side yet. It could be modified depending on your exact situation. Two-way syncs would require special care and a different script.

Replies to your questions:

Connector is the code object or class that provides connectivity to the storage. It could be a local file system connector or a cloud connector.

ConnFileExists determines if a file exists, using the appropriate connector for the storage.

The paths have to be concatenated and at the end the filename of your signal file has to be added too.

Yes it will exclude all subfolders too.

This script also assumes you will have the exclusions on the source side only, and that the source side is the left side.

Depending on the exact situation, you could remove the "isRightSide or" words or maybe replace them with "not isRightSide or", if you want to have the exclusion signal files only on the right side.

IMTheNachoMan
Posts: 86
Joined: Sun Nov 20, 2022 5:11 am

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by IMTheNachoMan »

This is great. I played around with this and I think I got what I needed. Can you please help me confirm?

It looks like both OnIncludeItem and OnScanFolder will not drill down into a folder if the functions return false. Is that accurate?

I tweaked the script because I want to do two way syncing with the syncovery.exclude file.

Code: Select all

function OnScanFolder(
        const FolderLevel: Integer;
        const RelativePath, LeftCompletePath, RightCompletePath: UnicodeString;
        const LeftExists, RightExists:Boolean;
        const LeftConnector, RightConnector: Opaque
    ):Boolean;
begin
    Result := (LeftExists and not RightExists and not ConnFileExists(LeftConnector, LeftCompletePath + '/syncovery.exclude')) or (not LeftExists and RightExists and not ConnFileExists(RightConnector, RightCompletePath + '/syncovery.exclude')) or (not ConnFileExists(LeftConnector, LeftCompletePath + '/syncovery.exclude') and not ConnFileExists(RightConnector, RightCompletePath + '/syncovery.exclude'))
end;
The logic I am using is: return true if any of these is true:
  • left exists, right does not, and there is no exclude file on the left
  • left does not exist, right does exists, and there is no exclude file on the right
  • at this point we know both left and right exist, so true if there is no exclude file on the left and right
Thoughts?

tobias
Posts: 1670
Joined: Tue Mar 31, 2020 7:37 pm

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by tobias »

Hello,
yes, the script is correct. However if the left and right sides are normal Windows paths, please use the backslash rather than slash.

For example:
LeftCompletePath + '\syncovery.exclude'

IMTheNachoMan
Posts: 86
Joined: Sun Nov 20, 2022 5:11 am

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by IMTheNachoMan »

tobias wrote:
Tue Nov 28, 2023 11:07 pm
Hello,
yes, the script is correct. However if the left and right sides are normal Windows paths, please use the backslash rather than slash.

For example:
LeftCompletePath + '\syncovery.exclude'
Thanks. Yeah, I was testing it on my Linux machine. On my Windows machine I will use forward slash.

Is there some global variable with the right slash for the OS the script is running on? That way the same script would work on any OS. Something like:

Code: Select all

Result := RightExists or not ConnFileExists(LeftConnector, LeftCompletePath + PATH_SEPERATOR +  'syncovery.exclude');

tobias
Posts: 1670
Joined: Tue Mar 31, 2020 7:37 pm

Re: Is it possible to do dynamic exclusions of folders based on existance of "special" file in a folder?

Post by tobias »

Hello,
yes, you can use
PathDelim
to get the system's default path delimiter.

But on Windows, the delimiters for the left and right sides can be different if one side uses FTP or another Internet Protocol.

Therefore, it is recommended to use the functions:
LeftDelim
and
RightDelim

Post Reply