I am trying to write a little Batch-Script which imports a modified file into a specific Firefox Profile Folder.
The Problem is that every Firefox Profilefolder has a differently generated Name
(for example: C:\Users\Username\AppData\Roaming\Mozilla\Firefox\Profiles\ab1c2def.default
)
Since I wanna make the Script work on multiple PC's , I can't hardcode the path, so I wanna be able to read the last part of the Path and use it as a variable so I can change my path to the desired folder to delete and add files in it.
Here is the code I begun with:
REM turning echo off
@echo off
REM changing directory to \Profiles Folder, behind it is the variable Folder.
cd %appdata%\Mozilla\Firefox\Profiles
REM I wanna read the last Folder into a variable.
set userprofilefolder = Somehow read the folders behind \Profiles
REM now I wanna change my directory into that one folder.
cd %appdata%\Mozilla\Firefox\Profiles\%userprofilefolder%
REM Here I am deleting the old "prefs.js" file in the Folder.
del prefs.js
REM now I change my Folderpath to where my modified "prefs.js" file lays.
cd path\where\my\prefs.js\file\lies
REM now I copy my fresh prefs.js" file into my Folder.
copy "prefs.js" "%appdata%\Mozilla\Firefox\Profiles\%userprofilefolder%"
REM Here I exit my .bat file.
exit
So how do I do that?
REM Turn echo off.
@echo off
REM Change directory to Firefox folder, this is where the profiles.ini file exist.
cd /d "%appdata%\Mozilla\Firefox" || exit /b 1
REM Read profiles.ini and call :profiledir with the value of each path key found.
for /f "tokens=1* delims==" %%A in (profiles.ini) do (
if /i "%%~A" == "path" call :profiledir "%%~B"
)
exit /b
:profiledir
REM Set local environment. Any settings i.e cd will be temporary.
setlocal
REM Check if ".default" in path to continue.
echo "%~1"| find ".default" || exit /b 1
REM Change to the profile dir.
cd /d "%~1" || exit /b 1
REM Copy prefs.js to current dir.
echo copy /y "path\to\my\prefs.js" "prefs.js"
exit /b
The file named profiles.ini
contains the paths to the profile folders.
The file is in ini format and you can find the path value of each path key.
You can have many profile folders so I use a find ".default"
to continue only if the folder contains that string.
Note:
pushd
and popd
may need to be used
instead of cd
.Description of operation
REM Change directory to Firefox folder, this is where the profiles.ini file exist.
cd /d "%appdata%\Mozilla\Firefox" || exit /b 1
This changes current directory. ||
is run command on right side
if left side command failed. If failed, will exit with errorlevel 1.
Note: Use cd /?
and exit /?
for syntax help.
REM Read profiles.ini and call :profiledir with the value of each path key found.
for /f "tokens=1* delims==" %%A in (profiles.ini) do (
if /i "%%~A" == "path" call :profiledir "%%~B"
)
This for
loop reads the profiles.ini file. Delimiters set are =
which
an ini file uses to delimit the key from the value. Token 1
will be the
key placed in %%A
while the token *
will be the value stored in %%B
.
If the key equals the key named path
, then call the label named
:profiledir
with 1st argument of value of path which should be a path
to a profile folder.
Note: Use for /?
and call /?
for syntax help.
:profiledir
This is the called label. A call to a label can pass arguments which
makes it convenient for passing something like i.e. a path. Arguments
received are in the format of %1
, %2
... %9
. The tilde i.e. %~1
removes outer double quotes. Called labels return back to the caller
when finished.
REM Check if ".default" in path to continue.
echo "%~1"| find ".default" || exit /b 1
"%~1"
is the 1st argument quoted. It is echoed to the find
command and if
.default
is found, then it will continue. The |
is known as a pipe where
stdout from the echo
is passed to the stdin of find
.
Note: Use find /?
for syntax help.
REM Change to the profile dir.
cd /d "%~1" || exit /b 1
Same as main code cd
except it is set local so only lasts with in
the called label.
REM Copy prefs.js to current dir.
echo copy /y "path\to\my\prefs.js" "prefs.js"
The /y
argument of copy
allows overwrite without confirmation so the
del
is obsolete.
Note: Use copy /?
for syntax help.
Finally: Remove the echo
in front of the copy
command if satisfied that it shows the copy command is OK.