Catalin
Posts: 7794
Joined: Wed Jun 13, 2018 7:49 am

When Include/Exclude filters aren't enough - How-To "sync" only folders matching a pattern

Hello,

I've got the following scenario from one of our customers: he wanted to sync some folders from disk, but not the entire folder. So basically he wanted to exclude some folders (like empty folders, for example).

Synchronized Folders are great when you know exactly what you're pulling in, but sometimes the source folder on disk contains a mix of folders you want and folders you don't, and the ones you don't want aren't a fixed, known set. Maybe it's a build output folder that also contains unrelated cache or localization folders, or some other empty folders.

The natural first instinct is to use the Exclude filter with a pattern like "exclude everything that isn't named X". Unfortunately, that's not something the filter engine supports (at least for now).

Here is a detailed breakdown of both options:

Include filters only apply to files. They're not taken into account for folders at all.

On the other hand, folders can be excluded, but there's no negation, so you can't write something like "exclude all folders except MyFolder".

Note: there's also a second thing worth knowing if you're scripting this: synchronized folders are refreshed at build time, not just when you press Refresh in the GUI. So if a source folder is sometimes missing, don't rely on manually removing content from the project as a workaround, since the next refresh (including the one that happens during build) will just bring it back based on what's currently on disk.

The solution

If your build is already automated (for example, a script that prepares a customer-specific source folder before running the .aip), the cleanest fix is to move the filtering logic into that script, using our PowerShell Automation support, instead of trying to express it through Sync Folder filters.

Here's the general idea:

1. Scan the source folder from your script.
2. Filter for only the subfolders you actually want, using whatever logic makes sense for your case (name contains a keyword, matches a list, etc).
3. Add only those folders to the project directly, using AddFolderContentS.

Here is a PowerShell script that uses our COM object to achieve the above:

Code: Select all

$advinst = New-Object -ComObject AdvancedInstaller
$project = $advinst.LoadProject("C:\Path\To\Your Application.aip")

# Folder that contains a mix of wanted and unwanted subfolders
$SourceRoot = "C:\Path\To\sync"

# Target base folder in the Advanced Installer project
# appdir = Application Folder
$TargetBase = "appdir"

# Keep only the folders you actually want, for example anything containing "MyFolder"
$wantedFolders = Get-ChildItem -Path $SourceRoot -Directory |
    Where-Object { $_.Name -like "*MyFolder*" }

foreach ($folder in $wantedFolders) {
    $targetFolder = "$TargetBase\$($folder.Name)"

    Write-Host "Adding folder '$($folder.FullName)' to '$targetFolder'"

    # Adds the full content of the matched folder
    $project.FilesComponent.AddFolderContentS($targetFolder, $folder.FullName)
}

$project.Save()
Hope you'll find this useful! :)

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”