I created this script for use in my AutoBootVHD8.cmd file which is part of a hands on lab http://ITProGuru.com/HOL
Here is the resulting code..
@echo Backing up Boot Configuration Data
Rem Create FileName with datatime
:: this is Regional settings dependent so tweak this according your current settings
Echo %DATE% %Time%
for /f “tokens=1-8 delims=::./ ” %%A in (‘echo %DATE% %TIME%’) do set FileDateTime=%%D%%C%%B-%%E%%F%%G
ECHO FileDateTime IS %FileDateTime%
@echo %temp%AutoBootBackup-%filedatetime%.bak
Echo This is a test file > %temp%AutoBootBackupTemp-%FileDateTime%.bak
BCDEDIT /export %temp%AutoBootBackup-%FileDateTime%.bak
dir %temp%AutoBootBackup*
Now, I will explain what each line does
Command | Description |
@echo Backing up Boot Configuration Data | Display a comment on the screen so the user knows what we are doing |
Rem Create FileName with datatime | Code comment that just tells other programmers that are looking at the code what we are doing. |
:: this is Regional settings dependent so tweak this according your current settings | Another comment, just a note to programmers |
Echo %DATE% %Time% | This is used to DISPLAY the contents of what we will be parsing. You do not need to leave it in the code. I left it in so you could easily tweak for different regional settings or rearranging the values. The output of this is used to figure out what the different parts are in our loop Output: Thu 02/07/2013 10:53:06.12 |
for /f “tokens=1-8 delims=::./ ” %%A in (‘echo %DATE% %TIME%’) do set FileDateTime=%%D%%C%%B-%%E%%F%%G | This is where all the real work is done. We are looping through the output of the “%DATE% %Time%” output and breaking it down into different parts (called tokens).
in (‘echo %DATE% %TIME%’) tells the script to process the results of this statement for the text we are going to scan through. The output can be seen above. Tokens are defined by “tokens=1-8 delims=::./ ” 1-8 says return only the first through the 8th tokens. A token is a Part. tokens are defined by a character which is defined by the %%A which says starting with “A” return each token in the next ASCII character. So A=Token1, B=Token2, C=Token3, etc all the way up to the max tokens which in this statement is 8 (remember 1-8 tokens) In this case, we are using several “delims” (delimiters). delims are defined by delims=::./ “ Anytime one of these characters is found in the string we are processing a new token will be created from the text “Thu 02/07/2013 10:53:06.12” After the tokens are broken up they are stored in variables %%A through %%H So the last thing we have to do is create a new variable that uses our tokens to create the FileDateTime variable. This is done with the part of the statement “do set FileDateTime=%%D%%C%%B-%%E%%F%%G” In my case, I am not using the %%H variable and I rearranged the date tokens so it would display YYYYMMDD followed by the time HHMMSS and I store that into a new variable called FileDateTime |
ECHO FileDateTime IS %FileDateTime% | display the contents of our new variable FileDateTime
Output: FileDateTime IS 20130702-105306 |
@echo %temp%AutoBootBackup-%filedatetime%.bak | Now use this FileDateTime variable to display what we will be using for our actual filename. The %temp% is a variable that is the location of Temporary files Output: C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackup-20130702-105306.bak |
Echo This is a test file > %temp%AutoBootBackupTemp-%FileDateTime%.bak | This command just creates a temporary text file using the temp variable and the FileDateTime variable with a .bak extention. The contents of the text file is “This is a test file” and the name of the file is:
C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackupTemp-20130702-105306.bak |
BCDEDIT /export %temp%AutoBootBackup-%FileDateTime%.bak | You can delete this line. This is how I will ultimately be using the command. I am passing the output filename to the BCDEDIT /Export function. |
dir %temp%AutoBootBackup* | I am just showing a listing of the files in my temp folder that start with “AutoBootBackup” so I can see that the file was successfully created. |
Please NOTE: The only line you really need is the “for /f “tokens=1-8 delims=::./ ” %%A in (‘echo %DATE% %TIME%’) do set FileDateTime=%%D%%C%%B-%%E%%F%%G” This will set an environment variable for FileDateTime then you can use that however you like. All the other lines in the script are simply provided to show you examples of how you can use this technology.
output from running the script on my computer:
C:Boot>filedatetime
Backing up Boot Configuration Data
C:Boot>Rem Create FileName with datatime
C:Boot>Echo Thu 02/07/2013 10:53:06.12
Thu 02/07/2013 10:53:06.12
C:Boot>for /F “tokens=1-8 delims=::./ ” %A in (‘echo Thu 02/07/2013 10:53:06.12
‘) do set FileDateTime=%D%C%B-%E%F%G
C:Boot>set FileDateTime=20130702-105306
C:Boot>ECHO FileDateTime IS 20130702-105306
FileDateTime IS 20130702-105306
C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackup-20130702-105306.bak
C:Boot>Echo This is a test file 1>C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackupTemp-20130702-105306.bak
C:Boot>BCDEDIT /export C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackup-20130702-105306.bak
The operation completed successfully.
C:Boot>dir C:UsersDSTOLT~1.NORAppDataLocalTempAutoBootBackup*
Volume in drive C is OSDisk
Volume Serial Number is FE20-0485
Directory of C:UsersDSTOLT~1.NORAppDataLocalTemp
02/07/2013 10:28 AM 65,536 AutoBootBackup-20130702-102855.bak
02/07/2013 10:29 AM 65,536 AutoBootBackup-20130702-102905.bak
02/07/2013 10:47 AM 65,536 AutoBootBackup-20130702-104709.bak
02/07/2013 10:53 AM 65,536 AutoBootBackup-20130702-105306.bak
02/07/2013 10:47 AM 65,536 AutoBootBackup.bak
02/07/2013 10:47 AM 22 AutoBootBackupTemp-20130702-104709.bak
02/07/2013 10:53 AM 22 AutoBootBackupTemp-20130702-105306.bak
7 File(s) 327,724 bytes
0 Dir(s) 174,626,017,280 bytes free
C:Boot>
I do hope you found this helpful. If you did, send me a thank you note with the URL on Twitter @ITProGuru
You can use only the date or only the time by just deleting the parts you do not want from the
do set FileDateTime=%%D%%C%%B-%%E%%F%%G
part of the command
%%D=Year
%%C=Month
%%B=Day
– = Static text
%%E=Hour
%%F=Min
%%G-=Sec
%%H=hundreds of seconds which I did not use
%%A= Day of week which I did not use
If I add another command to the top of the script (@Echo Off) I would get the following (clean) output