BAT Files
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
BAT Files
It's been a donkey's age since I last wrote a batch (.bat) file. I've even forgotten where to begin. I remember that they are text files, basically; so a simple text editor (I use EditPad Lite) is needed.
I know OMG is a bat file maven as, perhaps, are others of you. Would one of you kind people please point me to a good tutorial to refresh my aging (and decrepit) memory? Please and thank you!
I know OMG is a bat file maven as, perhaps, are others of you. Would one of you kind people please point me to a good tutorial to refresh my aging (and decrepit) memory? Please and thank you!
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: BAT Files
John Gray will undoubtedly have recommendations for you, but here is one that looks good to me: Batch Script Tutorial.
If you're feeling adventurous, you might also look at the Powershell Tutorial on the same site.
If you're feeling adventurous, you might also look at the Powershell Tutorial on the same site.
Best wishes,
Hans
Hans
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
To correct an oversight . . .
I meant to include my reason for needing a batch file. I want to copy my Firefox Profile folder daily and write it to another disk drive. I have just spent 8 days trying - successfully - to recover and restore the bookmarks for my browser. Thank you Mozilla Forums and especially FredMcD and corel. There are probably things Mozilla that will do this but I seem to have difficulty with things Mozilla; so I thought a simple BAT file might be easier for me.
I meant to include my reason for needing a batch file. I want to copy my Firefox Profile folder daily and write it to another disk drive. I have just spent 8 days trying - successfully - to recover and restore the bookmarks for my browser. Thank you Mozilla Forums and especially FredMcD and corel. There are probably things Mozilla that will do this but I seem to have difficulty with things Mozilla; so I thought a simple BAT file might be easier for me.
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
Duplicate post. Sorry!
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- Microsoft MVP
- Posts: 1320
- Joined: 24 May 2013, 15:33
- Location: Warminster, PA
Re: BAT Files
For this specific job -- copying your Firefox profile from its operating location to a backup on another drive -- you need only one statement in a batch file that runs the Robocopy program with the appropriate arguments:
The source path is the same for everybody who installs Firefox, because the environment variable %AppData% expands on execution to the AppData folder in your Windows profile (including your name). https://support.mozilla.org/en-US/kb/pr ... -user-data
You can replace the "D:\Firefox Profiles" path with whatever location you want as the destination (and that folder doesn't have to exist first).
The Robocopy program has lots of other switches besides the /S (which means "copy subdirectories")https://social.technet.microsoft.com/wi ... rence.aspx so you can tailor the copying to just new/updated files, ones with specific attributes, etc.
If you want to see the summary when the copying ends, add the Pause statement to a second line in the batch file.
Code: Select all
robocopy "%AppData%\Mozilla\Firefox\Profiles" "D:\Firefox Profiles" /S
You can replace the "D:\Firefox Profiles" path with whatever location you want as the destination (and that folder doesn't have to exist first).
The Robocopy program has lots of other switches besides the /S (which means "copy subdirectories")https://social.technet.microsoft.com/wi ... rence.aspx so you can tailor the copying to just new/updated files, ones with specific attributes, etc.
If you want to see the summary when the copying ends, add the Pause statement to a second line in the batch file.
-
- PlatinumLounger
- Posts: 5413
- Joined: 24 Jan 2010, 08:33
- Location: A cathedral city in England
Re: BAT Files
Curiously I have a FirefoxProfileCopier.bat (below) which is probably considerably more complicated than you might want, but you can possibly pick out bits of interest! Like Jay's suggestion, it also includes Robocopy. The initial timestamp routine is UK date-and-time-format-specific, and would need changing for less logical parts of the world <evil grin>!
EDIT: on reading through the comments, I note that this BATch file came from one I wrote to copy PSTs, and I haven't changed them all to be appropriate for Firefox profile copying ... oops!
EDIT: on reading through the comments, I note that this BATch file came from one I wrote to copy PSTs, and I haven't changed them all to be appropriate for Firefox profile copying ... oops!
Code: Select all
@echo off
:: +----------------------+
:: I FirefoxProfileCopier I copy the Firefox Profile while Firefox is not active
:: +----------------------+
:: this BATch file may be task scheduled at regular intervals,
:: and/or run as a Logoff script
:: it checks whether or not Firefox is running
:: and copies it ONLY if Firefox is not running (just in case)
:: we keep five backup copies of the Firefox Profile, suffixed .#1 to .#5
:: the format is BlueYonder account.pst.#1 up to #5
:: the number has no function except to differentiate the filenames
:: for the latest copy, simply refer to the dates and times of the files
:: an interesting and unexpected side-effect is that if the PST file does
:: NOT change between backups, then the same backup file number is used
:: until there IS a change to the PST file!
setlocal
:: obtain timestamp as yyyymmdd_hhmm yyyy mm dd_ hh mm
for /f "tokens=1-5 delims=/;:." %%a in ("%date%;%time: =0%") do set "dt=%%c%%b%%a_%%d%%e"
:: cumulative file
set cumulog=%~dp0Logs\%computername%_%~n0_cumulative.log
:: if Firefox is running, we don't do a backup, and leave immediately
tasklist /fi "imagename eq firefox.exe" ^
| findstr /b /i /c:"firefox.exe" > nul && (
echo %dt% Firefox running; no profile copy performed >> %cumulog%
goto sorter
)
)
:: source folder and the (newest) profile name folder
set source=%APPDATA%\Mozilla\Firefox\Profiles
for /f %%a in ('dir /ad /od /b %source%') do set profilename=%%a
if not defined profilename (
echo %dt% unable to find a source Firefox profile >> %cumulog%
endlocal
exit /b 1
)
:: target folder - ensure it is set up
set target=D:\Backup\Firefox Profiles
md "%target%" 2>nul
:: set up the source folder name, which will have .#n added for the target folder name
set folder=Firefox Profile
:: Step 1 - examine any backups in the target directory
:: initially assume that there have been no backups, so set newest to 0
set newest=0
:: obtain the newest backup folder number, if any, from the target folder directory
:: the 2>nul avoids an error message from DIR if there is no backup file yet,
:: in which instance the contents of %newest% are not altered
for /f "tokens=*" %%a in ('dir "%target%\%folder%.#?" /b /od 2^>nul') do set newest=%%a
:: obtain the suffix number of the most recent backup file
:: if this is 5 [or above!] then revert to 1, else increment it
set sfx=%newest:~-1%
if %sfx% geq 5 (set sfx=1) else (set /a sfx+=1)
:: Step 2 : perform the copy
:: construct the robocopy parameters and the upper-level folder name
set source=%source%\%profilename%
set ulf=%target%\%folder%.#%sfx%
set target="%target%\%folder%.#%sfx%\%profilename%"
set parms=/copy:dat /mir /s /dcopy:t /np /ndl /nfl /r:0 /w:0
set log=%~dp0Logs\%computername%_%~n0.profile#%sfx%.log
robocopy %source% %target% %parms% > %log%
:: change timestamp of the upper-level folder to the current date and time
type nul > "%ulf%\$dummy$" & del "%ulf%\$dummy$"
:: one line is written to the cumulative log for this backup
echo %dt% Firefox profile copied to backup #%sfx% >> %cumulog%
:sorter
:: sort the cumulative log into reverse order
sort /r %cumulog% /o %cumulog%
endlocal
John Gray
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
-
- Panoramic Lounger
- Posts: 8185
- Joined: 25 Jan 2010, 09:09
- Location: retirement
Re: BAT Files
I use SyncBackFree:
https://www.2brightsparks.com/download- ... kfree.html
to do something similar. In that application I have a profile that mirrors, i.e. takes a copy my FF profile and writes it to another disk and overwrite any files that already exist in the destination location. NB if i wanted to retain previous versions of my FF profile, i.e. do a 'proper' backup, I could change the setting in SyncBackFree to do that. I could also automate the mirroring, by having SyncBackFree start automatically with windows and then make the mirror process run in the background.
Ken
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: BAT Files
I endorse this simple approach, but would expand it, thereby Keeping It Simple while Making It Complex:-Jay Freedman wrote: ↑22 Sep 2021, 00:09For this specific job -- copying your Firefox profile from its operating location to a backup on another drive -- you need only one statement in a batch file that runs the RoboCopy program with the appropriate arguments:Code: Select all
RoboCopy "%AppData%\Mozilla\Firefox\Profiles" "D:\Firefox Profiles" /S
Code: Select all
RoboCopy "%AppData%\Roaming" "D:\AppData" /S
Now if, like me, you are the kind of old codger who wouldn't know how to get data back into place, as long as you have a current backup copy of your AppData there is a strong chance that there is someone here who can tell you how.
But only if you have a backup copy.
Bit like having jumper cables in the trunk and not knowing how to use them. As long as you have them, someone else can start your engine.
Cheers
Chris
P.S. as a tutorial, look at Jay's (and other's) example and check study the parameters & switches used in the RoboCopy documentation "RoboCopy.doc".
C.
He who plants a seed, plants life.
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
What, exactly, if Robocopy? I find that I seem to have put it on this laptop but when I run it I can see nothing happen. Task Manager shows no instance of it. Control Panel > Programs shows nothing in the list of programs that can be uninstalled.
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: BAT Files
RoboCopy is a command line utility built into Windows, so you won't see it in the list of installed applications.
It does not have a windowed interface - you can call it by typing instructions in the Command Prompt window or in a batch file. Think of it as the Copy command on steroids.
A search in your favorite search engine for robocopy tutorial will yield lots of results.
It does not have a windowed interface - you can call it by typing instructions in the Command Prompt window or in a batch file. Think of it as the Copy command on steroids.
A search in your favorite search engine for robocopy tutorial will yield lots of results.
Best wishes,
Hans
Hans
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
Thanks, Hans!!
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
I've tried to follow instructions here but must be missing something.
First, I created a new partition on my hard drive with the letter designation P (P:). I allocated 5GB to the partition.
I created a folder called AppData so the path would be P:\Appdata.
I then copied Chris' short Robocopy batch file line (RoboCopy "%AppData%\Roaming" "D:\AppData" /S) and changed the drive letter to P.
I then copied the modified instruction, opened the command line interface by entering CMD in the Search window and got a command prompt into which I pasted the modified command line and pressed enter.
I got this result: What did I do wrong?
First, I created a new partition on my hard drive with the letter designation P (P:). I allocated 5GB to the partition.
I created a folder called AppData so the path would be P:\Appdata.
I then copied Chris' short Robocopy batch file line (RoboCopy "%AppData%\Roaming" "D:\AppData" /S) and changed the drive letter to P.
I then copied the modified instruction, opened the command line interface by entering CMD in the Search window and got a command prompt into which I pasted the modified command line and pressed enter.
I got this result: What did I do wrong?
You do not have the required permissions to view the files attached to this post.
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- Microsoft MVP
- Posts: 1320
- Joined: 24 May 2013, 15:33
- Location: Warminster, PA
Re: BAT Files
The %AppData% environment variable's value includes the \Roaming folder at the end, so following Chris's instruction got you two "\Roaming" folder names in the expansion. Just remove the "\Roaming" from the command you enter.
The alternative, which may be easier for you to figure out if you look at the batch file at a later date, is to use the explicit full path instead of the environment variable:
robocopy "C:\Users\rhhut\AppData\Roaming" "P:\AppData" /S
or if you want to include the Local and LocalLow folders as well as the Roaming folder, make it
robocopy "C:\Users\rhhut\AppData" "P:\AppData" /S
The alternative, which may be easier for you to figure out if you look at the batch file at a later date, is to use the explicit full path instead of the environment variable:
robocopy "C:\Users\rhhut\AppData\Roaming" "P:\AppData" /S
or if you want to include the Local and LocalLow folders as well as the Roaming folder, make it
robocopy "C:\Users\rhhut\AppData" "P:\AppData" /S
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: BAT Files
You followed Chris's instruction to the letter.
Lucky for you, I spotted this post and have run the command properly AND successfully
Code: Select all
robocopy %appdata% t:\appdata /s
Cheers
Chris
P.S. I see my several mistakes, amongst them that i copied the command line from one of my BATch files, where the % symbols are required.
Manually entering the command from the command line prompt does NOT require those percentage symbols :slap wrist again:
C
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
OK, I followed Jay's example with changes to fit my drive and directory definitions and got results shown below. There was one error. Is there some way that I can find out what that was?
You do not have the required permissions to view the files attached to this post.
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- PlatinumLounger
- Posts: 5413
- Joined: 24 Jan 2010, 08:33
- Location: A cathedral city in England
Re: BAT Files
To find the error you need to be able to read the entire log from Robocopy.
To produce the log in a file to be later read using Notepad (or similar) you need to add
> Robocopy_log.txt (don't omit the > character!)
to the end of the Robocopy command which you have working above.
Nothing from the running of the Robocopy command will now display on the Command Prompt window - it all goes to the log file.
When Robocopy terminates you will get the Command Prompt prefix again, when you should enter
notepad Robocopy_log.txt
and you will be able to see all the Robocopy output in a scrollable and FINDable form!
PS My Robocopy logs for copying the Firefox profile on top of a previous one vary in size from about 50 KB to about 90 KB...
To produce the log in a file to be later read using Notepad (or similar) you need to add
> Robocopy_log.txt (don't omit the > character!)
to the end of the Robocopy command which you have working above.
Nothing from the running of the Robocopy command will now display on the Command Prompt window - it all goes to the log file.
When Robocopy terminates you will get the Command Prompt prefix again, when you should enter
notepad Robocopy_log.txt
and you will be able to see all the Robocopy output in a scrollable and FINDable form!
PS My Robocopy logs for copying the Firefox profile on top of a previous one vary in size from about 50 KB to about 90 KB...
John Gray
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: BAT Files
What John Gray says.
Plus, I never look at my RoboCopy log files (well, not since I tried to wade through them years ago).
I'd bet money on the error being something like "I, RoboCopy, could not gain access to this file because I, RoboCopy, have restricted access to it".
That, or else it is some supremely privileged file owned by Windows; "~FontCache-System.dat" comes to mind.
Still and all, you two have sparked my interest and I shall make a Log File tonight and examine it after my nightly backup. That consists of at least Two RoboCopy calls.
But anyway, Bob, congratulations on getting this far.
Cheers
Chrids
He who plants a seed, plants life.
-
- UraniumLounger
- Posts: 9297
- Joined: 13 Feb 2010, 01:27
- Location: Deep in the Heart of Texas
Re: BAT Files
Now that I have a bat bile containing the instructions to back up my Firefox Profile, is it possible to create a shortcut with icon on the System Tray that I can use to execute it?
Bob's yer Uncle
Dell Intel Core i5 Laptop, 3570K,1.60 GHz, 8 GB RAM, Windows 11 64-bit, LibreOffice,and other bits and bobs
(1/2)(1+√5) |
-
- PlatinumLounger
- Posts: 5413
- Joined: 24 Jan 2010, 08:33
- Location: A cathedral city in England
Re: BAT Files
I only know of a way to make executable files (including .BAT files) to be desktop shortcuts rather than appearing on the system tray. I suspect the latter would require Windows API programming not available to a BATch file.
John Gray
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: BAT Files
Yes!
I have a batch file Sync2021.bat, that does all my RoboCopy stuff.
I created a shortcut to the batch file with right-click+Copy and then (in a folder) right-click+PasteShortcut.
At this stage you could execute the batch file by executing the batch file itself, or the newly-created shortcut.
The "Target" of my shortcut looked like this:-
Code: Select all
T:\BATLap\Sync2021.bat
Code: Select all
C:\Windows\System32\cmd.exe /c T:\BATLap\Sync2021.bat
I hope this helps
Cheers
Chris
He who plants a seed, plants life.