0

I'm trying to backup some information from a folder. I try to make it so my collegues can also work with it. The folder in with are the files to backup dosen't have the same name on each machine.

I work from a different folder.

What I can do is this in CMD:

cd %APPDATA%\Mozilla\Firefox\Profiles\*.default-esr

and I can navigate to C:\Users***\AppData\Roaming\Mozilla\Firefox\Profiles\k6lfpnug.default-esr

I tried to implement it in my batch file :

@echo off

set CURRENT_DIR=%~dp0
set FIREFOX_DIR=%APPDATA%\Mozilla\Firefox\Profiles\*.default-esr

set BAK_FIREFOX_DIR=%CURRENT_DIR%\Firefox-%TIMESTAMP%

mkdir %BAK_Firefox_DIR%

robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% places.sqlite

What I get :

ERREUR : paramètre non valide #1 : "C:\Users\***\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-esr"

What I expected is that :

set FIREFOX_DIR=%APPDATA%\Mozilla\Firefox\Profiles\*.default-esr
echo %FIREFOX_DIR%

gives me

C:\Users\***\AppData\Roaming\Mozilla\Firefox\Profiles\k6lfpnug.default-esr

Magoo
  • 77,302
  • 8
  • 62
  • 84
Gulivert
  • 17
  • 7
  • 1
    You could get the target directory/directories using a `For /F` loop. Example: ```For /F "Delims=" %%G In ('Dir "%FIREFOX_DIR%" /A:D /B 2^>NUL') Do ...``` where `%%G` will be the matching full directory path, and `%%~nxG` will be just the directory name itself. There are literally thousands of examples of this technique throughout the [[tag:batch-file]] tag on this site, and no excuse for you not to have found one or more of them before submitting your question. – Compo Dec 01 '22 at 15:05
  • There is also a variable called `%UserProfile%`… – aschipfl Dec 02 '22 at 09:31

2 Answers2

1

The backup task can be done with the following commented batch file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Search for the default Firefox profiles directory. If one of the two
rem possible wildcard patterns return a positive match, make a backup of
rem the file with annotations, bookmarks, favorite icons, input history,
rem keywords, and browsing history (a record of visited pages) in that
rem directory using current date and time in the format yyyy-MM-dd_hh_mm
rem (international date format + hour and minute) in the destination
rem directory name.
for /D %%# in ("%APPDATA%\Mozilla\Firefox\Profiles\*.default-esr" "%APPDATA%\Mozilla\Firefox\Profiles\*.default-release") do (
    for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do (
        %SystemRoot%\System32\robocopy.exe "%%#" "%~dp0Firefox-%%G-%%H-%%I_%%J_%%K" places.sqlite /NDL /NFL /NJH /NJS /R:1 /W:5 >nul
        goto EndBatch
    )
)
echo ERROR: Could not find the default Firefox profile folder.
echo(
pause
:EndBatch
endlocal

ROBOCOPY creates the entire directory tree to the destination directory itself on not already existing.

%~dp0 expands always to a directory path ending with a backslash. There should not be added one more \ on concatenating %~dp0 with a file/folder name or wildcard pattern.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • pause /?
  • rem /?
  • robocopy /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

thank you ! I also use the timestamp from WMIC.

the code now look like this :

@ECHO OFF

set CURRENT_DIR=%~dp0
set TASKBAR_DIR=%APPDATA%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
set QUICK_ACCES_DIR=%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations
for /D %%# in ("%APPDATA%\Mozilla\Firefox\Profiles\*.default-esr" "%APPDATA%\Mozilla\Firefox\Profiles\*.default-release") do (
    set FIREFOX_DIR=%%#
)
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do (set "DateTime=%%I")
set "TIMESTAMP=%DateTime:~0,8%_%DateTime:~8,2%-%DateTime:~10,2%"

set BAK_FIREFOX_DIR=%CURRENT_DIR%\Firefox-%TIMESTAMP%
mkdir %BAK_Firefox_DIR%
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% key4.db                /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% logins.json            /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% logins-backup.json     /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% places.sqlite          /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% favicons.sqlite        /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% formhistory.sqlite     /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% cookies.sqlite         /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% cert9.db               /NDL /NFL /NJH /NJS /R:1 /W:5
robocopy %FIREFOX_DIR% %BAK_Firefox_DIR% handlers.json          /NDL /NFL /NJH /NJS /R:1 /W:5

pause
Gulivert
  • 17
  • 7
  • If the batch file is stored into the directory `C:\Temp\Development & Test(!) 100%` and the user double clicks it, the second line and all further lines using `CURRENT_DIR` already fail. The definition of the environment variables `TASKBAR_DIR` and `QUICK_ACCES_DIR` fail on user account name is `Asterix & Obelix, Inc.` resulting in `%APPDATA%` expanding to `C:\Users\Asterix & Obelix, Inc.\AppData\Roaming`. Microsoft plans to remove `wmic.exe` from future versions of Windows. There is already output on running `wmic /?` in a command prompt window on Windows 11 22H that `wmic` is deprecated. – Mofi Dec 02 '22 at 17:52
  • I have seen many questions in the last years on Stack Overflow about not working scripts (of any kind) caused by corruption of the value of the environment variable `PATH` (__system__ and/or __user__ `PATH`). A fail safe batch file does not depend on `PATH` even for executables in directory `%SytemRoot%\System32`. I have never seen a corruption of the environment variable `SystemRoot`. It is advisable for that reason to specify all executables with well known path with the fully qualified file name. That results also in a minimum of file system accesses by `cmd` during batch file processing. – Mofi Dec 02 '22 at 17:56
  • Not working scripts designed for usage by others because of not enclosing file/folder names in double quotes is the number one annoyance of script users. Lots of questions on Stack Overflow asking for help on an installation issue or on the usage of a script written by somebody else than the user of a script or program package are caused by scripts executed on Windows not working with files and folders containing on the users´ computer a space or one of these characters ``&()[]{}^=;!'+,`~``. – Mofi Dec 02 '22 at 18:03
  • The usage help of __ROBOCOPY__ output on running `robocopy /?` in a command prompt window contains as fifth non-empty line: "Usage :: ROBOCOPY source destination **[file [file]...]** [options]". There can be multiple files specified. The nine `robocopy` command lines can be merged to the single command line: `%SystemRoot%\System32\robocopy.exe "%FIREFOX_DIR%" "%BAK_Firefox_DIR%" key4.db logins.json logins-backup.json places.sqlite favicons.sqlite formhistory.sqlite cookies.sqlite cert9.db handlers.json /NDL /NFL /NJH /NJS /R:1 /W:5 >nul` – Mofi Dec 02 '22 at 18:11