Append text to file in changing directories

September 17, 2010 at 11:05:14
Specs: Windows XP
I'm trying to put together a batch file to append a few lines of text (listed below) to the Firefox user profile prefs.js files for multiple accounts on a machine.

appendfile user_pref("capability.policy.policynames", "localfilelinks");
appendfile user_pref("capability.policy.localfilelinks.sites", "http://example.com");
appendfile user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");

These lines should append to user.js which is located in the user profile application data folder. For each profile Mozilla has seen fit to restructure the directory name. I.E. for John Doe it would be

"C:\Documents and Settings\[b]John.Doe[/b]\Application Data\Mozilla\Firefox\Profiles\[b]1234ABCD[/b].default\user.js"

but for Jane Doe it could be

"C:\Documents and Settings\[b]Jane.Doe[/b]\Application Data\Mozilla\Firefox\Profiles\[b]4321DCBA[/b].default\user.js".
This makes it pretty tricky for me.

Someone over at (http://forum.bigfix.com/viewtopic.php?id=4797 ) has already figured this out, but I have been unable to convert their code into a working batch file. Surely this can be done?


See More: Append text to file in changing directories

Report •

#1
September 17, 2010 at 12:26:02
I have been unable to convert their code into a working batch file.
Probably because whoever wrote the batch portion doesn't know how to write batch files.

Just run a search in %appdata%\Mozilla\Firefox\Profiles for user.js.
Quick, Dirty, & Untested example:

for /f "delims=" %%a in ('dir /s/b/a "%appdata%\Mozilla\Firefox\Profiles\user.js"') do call :AddLines "%%a"
goto :eof

:AddLines
find "user_pref(""capability.policy.policynames"", ""localfilelinks"");" %1 >NUL || echo user_pref("capability.policy.policynames", "localfilelinks"); >>%1
find "user_pref(""capability.policy.localfilelinks.sites"", ""http://example.com"");" %1 >NUL || echo user_pref("capability.policy.localfilelinks.sites", "http://example.com"); >>%1
find "user_pref(""capability.policy.localfilelinks.checkloaduri.enabled"", ""allAccess"");" %1 >NUL || echo user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess"); >>%1


Report •

#2
September 17, 2010 at 13:34:00
I'm not sure that the script you wrote would work because after Profiles is a randomly generated string with .default at the end set up as a folder. i.e. "%appdata%\Mozilla\Firefox\Profiles\1234dcba.default\user.js"

I did manage to get it working although I don't know what some of the lines do and I'm sure it could easily be optimized.

I wrangled with it and managed to use the following:

[code]
if exist del "C:\Program Files\Mozilla Firefox\defaults\pref\user.js"

echo user_pref("capability.policy.policynames", "localfilelinks"); > user.js
echo user_pref("capability.policy.localfilelinks.sites", "http://localwiki"); >> user.js
echo user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess"); >> user.js

move user.js "C:\Program Files\Mozilla Firefox\defaults\pref\"

for /F "tokens=1 delims=\" %%a in ('dir "C:\Documents and Settings\" /b') do call :Sub1 %%a %%b
goto :End

:Sub1
if %2 NEQ b (call :Sub2 %1 %2) else (call :Sub4 %1)
goto :End

:Sub2
for /F "tokens=1 delims=\" %%b in ('dir "C:\Documents and Settings\%1 %2\Application Data\Mozilla\Firefox\Profiles\" /b') do call :Sub3 %1 %2 %%b
goto :End

:Sub3
copy "C:\Program Files\Mozilla Firefox\defaults\pref\user.js" "C:\Documents and Settings\%1 %2\Application Data\Mozilla\Firefox\Profiles\%3\"
goto :End

:Sub4
for /F "tokens=1 delims=\" %%b in ('dir "C:\Documents and Settings\%1\Application Data\Mozilla\Firefox\Profiles\" /b') do call :Sub5 %1 %%b
goto :End

:Sub5
copy "C:\Program Files\Mozilla Firefox\defaults\pref\user.js" "C:\Documents and Settings\%1\Application Data\Mozilla\Firefox\Profiles\%2\"
goto :End

:End
[/code]

By the way, thank you for the quick response! :)


Report •

#3
September 17, 2010 at 13:39:38
I'm not sure that the script you wrote would work because after Profiles is a randomly generated string with .default at the end set up as a folder
Trust me; there's a reason why I can get away with untested code. (Or don't, and try the line
dir /s/b/a "%appdata%\Mozilla\Firefox\Profiles\user.js"
yourself.)

EDIT: Also, that script might bomb/just do nothing on Vista/Win7. I haven't tested it, though.


Report •

Related Solutions

#4
September 18, 2010 at 07:52:31
My Firefox doesn't have any user.js, but plenty of wacky folder names. LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#5
September 20, 2010 at 05:50:59
When I tried the untested code I got "File Not Found". I assume this is because user.js does not exist and has to be created in "...\Profiles\1234abdc.default\user.js" but I don't know.

No worries though. The problem has been solved. :)


Report •

Ask Question