How to iterate through a directory looking for a specific file that can be contained in multiple folders and then copying it to another location BATCH

Viewed 27

I am working on a batch script to backup chrome/edge browser bookmarks for every profile a user has in addition to the default profile. There can be any number of profile folders inside of %USERPROFILE%\AppData\Local\Google\Chrome\User Data\ and I need to visit each profile folder and then look for a file named Bookmarks. Then I copy the Bookmarks file into another directory inside of a folder corresponding to the profile folder it was found in. EX: %USERPROFILE%\AppData\Local\Google\Chrome\User Data\Profile 11\Bookmarks gets copied to a folder %userbookmarkspath%\Chrome\Profile 11\Bookmarks and so on.

My main issue is how exactly I do my iteration through the subfolders in a way that I still have easy access to the name of the subfolder (profile 11 etc). Along with this I'm not sure if its possible to use wildcards for finding all the different profile folders like "Profile *".

This is my current attempt, but I can't seem to get my conditionals right when it comes to getting it to go inside of Profile or folders or the Default folder.

title backup Chrome bookmarks in Windows 

REM Path for the user's network file share
SET userprivatedrivepath=\\ad.weber.edu\fs

REM Path for bookmark backup folder in user's network file share
REM Need to change to bookmark_backups > computer name > chrome or edge folder > put each profile html in its own folder not just default
SET userbookmarkspath=%userprivatedrivepath%\Redirect\%USERNAME%\bookmark_backups\%computername%

REM bookmark locations on user's computer
SET bookmark_chrome=%USERPROFILE%\AppData\Local\Google\Chrome\User Data\
SET bookmark_edge=%USERPROFILE%\AppData\Local\Microsoft\Edge\User Data\

REM bookmark backup path for different browsers
SET userbookmarkspathchrome=%userbookmarkspath%\Chrome
SET userbookmarkspathedge=%userbookmarkspath%\Edge

REM create folder to backup a computer's bookmarks to
IF NOT EXIST "%userbookmarkspath%" (
    mkdir "%userbookmarkspath%"
)

REM create chrome folder for chrome bookmark profiles
IF NOT EXIST "%userbookmarkspathchrome%" (
    mkdir "%userbookmarkspathchrome%"
)

REM create edge folder for edge bookmark profiles
IF NOT EXIST "%userbookmarkspathedge%" (
    mkdir "%userbookmarkspathedge%"
)


REM iterate through the chrome/edge bookmark locations - 
REM if a default or Profile #### is found go into it and get the html file and copy that html file into a directory with the same profile name in the corresponding browser folder


FOR /R %%D IN (%bookmark_chrome%) DO (
    IF( "%%D" == "Profile *")(
        REM look through %% d directory for Bookmark file
        FOR /R IN (%%s) DO (
            IF "%%s" == "Bookmarks"(
                REM setup path for the folder we're going to save in chrome bookmarks
                SET bookmarkbackuppath=%userbookmarkspathchrome%\%%D\

                REM create the folder that will hold the Bookmark file for this profile
                IF NOT EXIST "%bookmarkbackuppath%" (
                    mkdir "%bookmarkbackuppath%"
                )

                REM backup the bookmarks file in a corresponding profile folder within userbookmarkspath
                ROBOCOPY "%%s" "%bookmarkbackuppath%" Bookmarks /COPY:DAT /DCOPY:T /R:1 /W:30 /NP
            )
        )
    )
)






REM backup bookmarks
REM ROBOCOPY "%bookmark_chrome%" "%userbookmarkspathchrome%" Bookmarks /COPY:DAT /DCOPY:T /R:1 /W:30 /NP
REM ROBOCOPY "%bookmark_edge%" "%userbookmarkspathedge%" Bookmarks /COPY:DAT /DCOPY:T /R:1 /W:30 /NP

REM save a script that opens the users chrome folder
echo explorer %USERPROFILE%\AppData\Local\Google\Chrome\User Data\Default\ > %userbookmarkspath%\openChromeFolder.bat```
0 Answers
Related