0
votes

Background:

I am trying to migrate the Firefox profiles from the local machines to the roaming profile directory of the user. Some users already have the profile located in their home drive, the majority does not.

I already tried to do this with the CCK Tool from Mike Kaply but unfortunately it's not migrating the Firefox profile directory to the roaming profile of the user, but instead just points to the roaming profile directory where no Firefox profile is located.

So, to work around this, I decided to create a batch, which looks in every user directory under the directory "home" for the directory "FirefoxProfile". If this folder does not exist, it would create it (hidden folder). Furthermore the script would loop through every local profile, search in every Firefox profile folder for the latest places.sqlite file (the bookmarks file of Firefox), copy it to the users FirefoxProfile folder under home and lastly copy the file profiles.ini to the users Firefox folder under AppData\Roaming\Mozilla.

If the folder FirefoxProfile under the user’s home directory does exist, the script would do the same steps as if the folder does not exist, but not create the folder FirefoxProfile.

The script I wrote does not run as expected. I tried to rewrite it (rearranged the quotes and brackets, reformat the code), but it always comes down to the following problems: The script deletes the parameter "/D" in the FOR loop. Also from there on the script doesn't recognize the "%%" variables correctly, instead it deletes the leading "%" character.

Furthermore something is wrong with the FOR /F loop. The output in the flie 3.txt is:

DIR /S /B /O:-D testuser1.V2\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite home\testuser1\FireFoxProfile\Default

But it should give back something like this:

testuser1.V2\AppData\Roaming\Mozilla\Firefox\Profiles\lh15n9tq.default\places.sqlite home\testuser1\FireFoxProfile\Default

I would highly appreciate any help. Thanks in advance.

The script:

setlocal enabledelayedexpansion
SET HomeProfile=""
SET WSProfile=""
SET FFProfile=""

pushd \\srv-xyz\users
cd home
FOR /D %%A in (*) DO (
SET "HomeProfile=%%A"
if not exist !HomeProfile!\FireFoxProfile (
echo does not exist >> C:\tmp\1.txt
echo !HomeProfile!\FireFoxProfile\Default >> C:\tmp\2.txt
cd profile_WS
FOR /D %%B in (!HomeProfile!.V2) DO (
SET "WSProfile=%%B"
FOR /F "usebackq delims=" %%C IN ('DIR !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite /S /B /O:-D') DO (
SET "FFProfile=%%C"
echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default >> C:\tmp\3.txt
echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox >> C:\tmp\4.txt
))
) ELSE (
echo !HomeProfile!\FireFoxProfile\Default >> C:\tmp\5.txt
FOR /D %%B IN (!HomeProfile!.V2) DO (
SET "WSProfile=%%B"
FOR /F "usebackq delims=" %%C IN ('DIR !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite /S /B /O:-D') DO (
SET "FFProfile=%%C"
echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default >> C:\tmp\6.txt
echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox >> C:\tmp\7.txt)
))
)
popd
PAUSE

The scripts state:

To test the script, I replaced the actual code and made the script write its output to text files.

The Problem:

For every user the script fails like follows

V:\home>(
SET "HomeProfile=testuser1"  
 if not exist !HomeProfile!\FireFoxProfile (
echo does not exist  1>>C:\tmp\1.txt  
 echo !HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\2.txt  
 cd profile_WS  
 FOR / %B in (!HomeProfile!.V2) DO (
SET "WSProfile=%B"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 
) 
)  ELSE (
echo !HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\5.txt  
 FOR / %B IN (!HomeProfile!.V2) DO (
SET "WSProfile=%B"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\6.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\7.txt 
) 
) 
) 
) 

V:\home>(
SET "WSProfile=testuser1.V2"  
 FOR /F "usebackq delims" %C IN ('DIR /S /B /O:-D !WSProfile!\AppData\Roaming\Mozilla\Firefox\Profiles\places.sqlite') DO (
SET "FFProfile=%C"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 
) 

V:\home>(
SET "FFProfile=DIR"  
 echo !FFProfile! home\!HomeProfile!\FireFoxProfile\Default  1>>C:\tmp\3.txt  
 echo C:\tmp\profiles.ini !WSProfile!\AppData\Roaming\Mozilla\Firefox  1>>C:\tmp\4.txt 
) 

What caught my eye starts at these lines:

cd profile_WS  
 FOR / %B in (!HomeProfile!.V2) DO (
SET "WSProfile=%B" 
1
Clearly, the FOR / %B is a problem. What do you see in the console output where the wrong output is produced? - lit
Are you are aware that each user can have several Firefox profiles and those can be located almost anywhere? Just because a bookmarks file was most recently modified would not be an indication of the users most often used profile. (I know that the majority may just be using the single randomly named *.default location but I wouldn't call that a robust assumption). - Compo
Hello @Lit, thank you for your answers. The code under the titel "The Problem:" is the exact output of the script just for a single user folder, in this case testuser1. The output is the same for every user directory. - BSL
Hello @Compo, thank you for your answers. Our envoirement is strictly resticted, the users have no ability to create another profile in another location, this is why I know for sure, that they use the standard *.default profile. Thank you for that input nevertheless. - BSL
If the users are prevented from creating more than one profile it doesn't make any sense then to recursively check for the most recently modified bookmarks file, they will only have one under the *.default path. Also are you aware that the actual paths to their profile directories are written in file in a known location? You can just retrieve/parse them from there. - Compo

1 Answers

0
votes

If you take a look at the first of the following line:

FOR /D %%B in (!HomeProfile!.V2) DO (

That should resolve to %%A.V2 so you are effectively just setting WSProfile to \\srv-xyz\users\home\profile_WS\testuser1.V2 and could probably, since your current directory should at that point be \\srv-xyz\users\home\profile_WS, just use Set "WSProfile=%CD%\%%A.V2".

There appears to be a difference between the steps you are taking depending upon the existence of the FirefoxProfile folder under the user’s home directory. If it doesn't exist you are changing directory to profile_WS and never returning from there which will create issues in the rest of your script. If it does exist you are staying in home.

Edit

I have decided to post this despite it not being a solution to the OP's question as much of it may prove useful to them and all to many others.

This attempts to find all of the active Firefox profiles for all of the standard local users. It saves those profile paths to variables in the format %UsersName[i]% where i can be an increasing integer for each users profile found. (Those variables are also printed to the screen for information purposes).

@Echo Off
For /F "UseBackQ Tokens=1* Delims==" %%A In (
    `WMIc Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`
) Do For /F "Delims=" %%C In ("%%B") Do Call :Users "%%C"
If Defined i If Not "%i%"=="0" (SetLocal EnableDelayedExpansion
    For /L %%A In (1,1,%i%) Do Echo( %%!UN![%%A]%% = !%UN%[%%A]!
    EndLocal
    Echo( Press any key to exit...
    Timeout -1 1>Nul)
Exit/B

:Users
Set "PF=%~1\AppData\Roaming\Mozilla\Firefox\"
If Not Exist "%PF%" GoTo :EOF
Set "UN=%~nx1"
Set "i=0"
For /F "Tokens=1* Delims==" %%A In (
    'FindStr/R "^Path=.*" "%PF%Profiles.ini"') Do (Set "FP=%%B"
    Call :Profs "%%FP:/=\%%")
GoTo :EOF

:Profs
Set/A "i+=1"
If Exist "%PF%%~1\" (Set "%UN%[%i%]=%PF%%~1") Else Set "%UN%[%i%]=%~1"

The script has not been tested on multi user systems nor on users with multiple profiles, I have written it as a concept only.