Search code examples
batch-filedirectorypathsubdirectory

BATCH : set an incomplete path in a variable


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


Solution

  • 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: