Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO ADD DATE TO FILE NAME FROM BATCH FILE!

Status
Not open for further replies.

selena1

Programmer
Apr 7, 2003
69
I have an Oracle database on Windows 2000, and I want to make exports every week. I would like
to automate that job using Windows Sheduled Tasks and batch file.

But, I don't know how to work with date variables in batch files. For example I want that my
export command in batch file looks something like this:

export ..... file=C:\export_dd_mm_yyyy\export_dd_mm_yyyy log=C:\export_dd_mm_yyyy\export_log_dd_mm_yyyy ,

where dd_mm_yyyy is date when export was made (system date).
Also, I want to create directory with name like export_dd_mm_yyyy.

Can anybody help me with this?

p.s. Some example would be great.

Thanks!
 
Well, if you can use vbscript instead of a bat file you could do it like this:

Dim DestServer

DestServer = "\\Server\C$\Logs\"


'Time to create the vars that hold the date and time
sDate = DatePart("m", Now) & "-"
sDate = sDate & DatePart("d", Now) & "-"
sDate = sDate & DatePart("yyyy", Now) & ""

sTime = DatePart("h", Now) & DatePart("n", Now)

set oFSO = CreateObject("Scripting.FileSystemObject")


'If correct folder doesn't exist, make it
If Not oFSO.FolderExists(DestServer & sDate) then

'This section will create the target folder
set oFolder = oFSO.CreateFolder(DestServer & sDate )
End If

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Here is the old fashioned .bat way:

@echo off
for /f "tokens=2 delims= " %%A in ('date/T') do (
set date=%%A)
for /f "tokens=1-3 delims=/" %%A in ('echo %date%') do (
set m=%%A
set d=%%B
set y=%%C)


mkdir c:\export_%d%_%m%_%y%
export ...file=c:\export_%d%_%m%_%y%\export_%d%_%m%_%y%
log..........
 
Thank you both.

I tried both solution and they work fine. But I'm not sure how to export from vbscript I'll use .bat file.
 
Writing to a file in vbscript is really easy.

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("c:\test.txt", ForWriting)

report = "Text to be written to file"
ts.write report

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Wallst32 --
Thanks for the code. How would I modify it so that I can retrieve the day of the week into a variable I can the use in an IF-THEN-ELSE to control execution ??
I am looking for something along the lines of
>Get dayofweek
> if dayofweek="Mon" then....

or
>if dayofweek=2 then.....

Thanks !
 
Well, if you can export the file with the same name (example: export.txt) then there is a very simple way to change the name in a one-liner:


REN EXPORT.TXT "%DATE%.TXT"

The trick is to set your date seperator so it uses the period (.) or the dash (-) instead of the slash (/) in your regional setting and then you can use the task scheduler to run right after your backup to rename the file.



"In space, nobody can hear you click..."
 
Some more simple code will give it to you.


dayofweek = DatePart("W", Date)
wscript.echo dayofweek

Run this in a VBS and it will return a number.

Sunday=1
Monday=2
Wednesday=3
etc.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top