Dan
Posts: 4524
Joined: Wed Apr 24, 2013 3:51 pm

Deliver a file to all users profile programmatically

Hello,

If your installer needs to install some files under each user profile, among the self-healing and active setup mechanism, there is always the custom actions approach.

In this thread will exemplify how to configure the installer to deliver a file under each user's profile (both registered and next users) using a .bat file. the content of the bat file can be something as follow:

Code: Select all

FOR /f "tokens=*" %%G in ('dir /b /a:d-s-l "%SystemDrive%\Users"') DO (
  IF /I NOT "%%G"=="Public" (
    IF NOT EXIST "%SystemDrive%\Users\%%G\AppData\Roaming\MyCompany\demoFile.config" md "%SystemDrive%\Users\%%G\AppData\Roaming\MyCompany"
    xcopy.exe %1 "%SystemDrive%\Users\%%G\AppData\Roaming\MyCompany" /C /I /Q /R /K /Y
  )
)
Basically, the script loop through the folders in C:\Users excluding system folder. For each existing user, it will check the existence of the specified directory. If the sub-directory does not exist, it will be created and the config file will be copied at that location. For details, you can also check the xcopy article and How to launch a CMD or BAT file.

A very nice thing that the script does, is that it will copy the related config file under the Default profile. That means, any future created user on the machine will end having the config file under his profile. The Default user profile is used as a template when creating future users account on the machine.

Now, let's configure the project to implement this. For this case, I've created a demo installer that installs a shortcut to the Updater on the Desktop. It simply checks if a new version of Advanced Installer is available.

Since the config file will be copied by the .bat file, then you can add both bat file and config file as temporary files. You can read more about temp files in the Temporary Files Operations in the Files and Folders Page article.
temp files.PNG
temp files.PNG (11.43 KiB) Viewed 92263 times
To launch the .bat file, you can use the predefined launch file custom action:
ca props.PNG
ca props.PNG (43.91 KiB) Viewed 92263 times
Of course, you can use the VBScript or a PowerShell custom action to implement this functionality or create your custom action as a custom action written in C# or a custom action written in C++.

The sample project is attached to this thread, so if you are interested to take a look directly at my project, you are more than welcome to download the ZIP file.

Best regards,
Dan
Attachments
Deliver file to all users profille.zip
(55.16 KiB) Downloaded 2871 times
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
mrtn
Posts: 6
Joined: Tue Jun 02, 2020 1:33 pm

Re: Deliver a file to all users profile programmatically

Thanks! That's great:)But sadly I must be doing something wrong. The folder is copied perfectly, but the files are not copied.

My aim is to copy custom Office templates to the folder "My Documents\Anpassade Office-mallar", so I have modified the script. Maybe there are errors there?

FOR /f "tokens=*" %%G in ('dir /b /a:d-s-l "%SystemDrive%\Users"') DO (
IF /I NOT "%%G"=="Public" (
IF NOT EXIST "%SystemDrive%\Users\%%G\Documents\Anpassade Office-mallar\test.txt" md "%SystemDrive%\Users\%%G\Documents\Anpassade Office-mallar"
IF NOT EXIST "%SystemDrive%\Users\%%G\Documents\Anpassade Office-mallar\test2.txt" md "%SystemDrive%\Users\%%G\Documents\Anpassade Office-mallar"
xcopy.exe %1 "%SystemDrive%\Users\%%G\Documents\Anpassade Office-mallar" /C /I /Q /R /K /Y
)
)
Catalin
Posts: 6739
Joined: Wed Jun 13, 2018 7:49 am

Re: Deliver a file to all users profile programmatically

Hello,

I have answered your forum thread here:

viewtopic.php?f=2&t=45483&p=116727#p116727

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Catalin
Posts: 6739
Joined: Wed Jun 13, 2018 7:49 am

Re: Deliver a file to all users profile programmatically

Hello,

If you'd like a PowerShell script that does the same thing as the batch script from above, please have a look on the script below:

Code: Select all

$source = "C:\testFolder\"
$users = (get-childitem c:\users -exclude 'Public').Name

foreach ($user in $users){
copy-item -path $source -destination "c:\users\$user\AppData\Roaming" -force
}
The above script will copy the entire "testFolder" into the destination.

This is mostly useful because the approach from above uses xcopy, which can by default receive only one parameter - meaning we can copy only one file at a time with it.

Hope this helps! :)

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

Return to “Sample Projects”