BAT Files

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

BAT Files

Post by BobH »

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!

:cheers: :chocciebar: :thankyou:
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
HansV
Administrator
Posts: 72233
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: BAT Files

Post by HansV »

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.
Regards,
Hans

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

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.
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

Duplicate post. Sorry!
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
Jay Freedman
Microsoft MVP
Posts: 1150
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: BAT Files

Post by Jay Freedman »

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:

Code: Select all

robocopy "%AppData%\Mozilla\Firefox\Profiles" "D:\Firefox Profiles" /S
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.

User avatar
John Gray
PlatinumLounger
Posts: 5003
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT Files

Post by John Gray »

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! :sad:

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

Think how stupid the average person is, then realise that half of the population are stupider than that.

User avatar
stuck
UraniumLounger
Posts: 6698
Joined: 25 Jan 2010, 09:09
Location: up North (but it's not that grim)

Re: BAT Files

Post by stuck »

BobH wrote:
21 Sep 2021, 20:41
...I want to copy my Firefox Profile folder daily and write it to another disk drive...
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

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12456
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: BAT Files

Post by ChrisGreaves »

Jay Freedman wrote:
22 Sep 2021, 00:09
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:

Code: Select all

RoboCopy "%AppData%\Mozilla\Firefox\Profiles" "D:\Firefox Profiles" /S
I endorse this simple approach, but would expand it, thereby Keeping It Simple while Making It Complex:-

Code: Select all

RoboCopy "%AppData%\Roaming" "D:\AppData" /S
In this manner ALL of your local settings data is backed up, not just your Firefox profiles.
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.
If you are going through hell, keep going!

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

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.
:scratch:
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
HansV
Administrator
Posts: 72233
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: BAT Files

Post by HansV »

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.
Regards,
Hans

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

Thanks, Hans!!
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

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:
robocopy result.PNG
What did I do wrong?
You do not have the required permissions to view the files attached to this post.
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
Jay Freedman
Microsoft MVP
Posts: 1150
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: BAT Files

Post by Jay Freedman »

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

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12456
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: BAT Files

Post by ChrisGreaves »

BobH wrote:
22 Sep 2021, 18:58
What did I do wrong?
You followed Chris's instruction to the letter.
Lucky for you, I spotted this post and have run the command properly AND successfully
Untitled.png

Code: Select all

robocopy %appdata% t:\appdata /s
As Jay has not yet pointed out, you should always read Chris's advice and then wait one post for the inevitable correction :slapwrist:
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.
If you are going through hell, keep going!

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

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?
robocopy success.PNG
You do not have the required permissions to view the files attached to this post.
Bob's yer Uncle!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
John Gray
PlatinumLounger
Posts: 5003
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT Files

Post by John Gray »

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...
John Gray

Think how stupid the average person is, then realise that half of the population are stupider than that.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12456
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: BAT Files

Post by ChrisGreaves »

BobH wrote:
22 Sep 2021, 23:23
... There was one error. Is there some way that I can find out what that was?
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 :cheers:
Chrids
If you are going through hell, keep going!

User avatar
BobH
UraniumLounger
Posts: 8360
Joined: 13 Feb 2010, 01:27
Location: Temple - Deep in the Heart of Texas

Re: BAT Files

Post by BobH »

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!
(1/2)(1+√5)
Intel Core i5, 3570K, 3.40 GHz, 16 GB RAM, ECS Z77 H2-A3 Mobo, Windows 7 >HPE 64-bit, MS Office 2016

User avatar
John Gray
PlatinumLounger
Posts: 5003
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT Files

Post by John Gray »

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

Think how stupid the average person is, then realise that half of the population are stupider than that.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12456
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: BAT Files

Post by ChrisGreaves »

BobH wrote:
23 Sep 2021, 18:40
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?
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
but I modified it by prefacing it with a call to Cmd.Exe

Code: Select all

C:\Windows\System32\cmd.exe /c T:\BATLap\Sync2021.bat
With the shortcut modified (with the call via cmd.exe) I can now right-click the shortcut and "Pin to Taskbar"

I hope this helps
Cheers
Chris
If you are going through hell, keep going!