Batch Commands To Name Output File With Current Date

2008-09-20 Initial Post

Here is a sample batch file that names the output file based on the current date. If the file was run on 09-20-2008, the output file name would be LDIFDE_Export_All_2008-09-20.ldf.

[batch]
@Echo off
Echo Running this command exports all AD objects in forest into a file named LDIFDE_Export_All_YYYY-MM-DD.ldf.
Rem Tested on Windows Server 2003 SP2
Rem Change the server name after the -s parameter (server must be a GC).
Rem Change the domain DN after the -d parameter.
Rem Sat 09/20/2008 < This is a sample of the date format. It includes the day also, and has 14 characters total, 1 - 14.
Rem 12345678901234 Character count (starts at 1)
Rem %date:~10,4% Go to the 10th character and take 4 characters after that for the year.
Rem %date:~4,2% Go to the 4th character and take 2 characters after that for the month.
Rem %date:~7,2% Go to the 7th character and take 2 chacters after that for the date.
Rem Date formatting based on info from http://www.webmasterworld.com/forum107/362.htm.
Set strDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
ldifde -f "G:\Backups\LDIFDE_Exports\LDIFDE_Export_All_%strDate%.ldf" -s server-dc-1 -t 3268 -d "DC=ad-domain,DC=dom" -p subtree
Echo Export completed
Pause
[/batch]

If you also want to get the day:

[batch]
Set strDay=%date:~0,3%
echo %strDay%
[/batch]

Leave a Reply

*