VBScript - Date and Time Constraints for Running a File - datetime

Ok I am new with writing VBScript and I want to write a string of code that plays a file (WAV format) only on a certain day and only between specific times. After piecing together multiple fragments of code I found on the internet I was left with the following:
Dim myDateString
Dim thing1
thing1 = 0
myDateString = Date()
If myDateString < "13/08/13" Then
thing1 = 1
end if
if thing1 = 1 then
If myDateString > "15/08/13" Then
thing1 = 2
end if
end if
if thing1 = 2 then
hournow = hour(Time())
If hour(Time()) >= 9 And Hour(Now()) < 22 Then
set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
WshShell.Run "wmplayer """ & music & """",0,True
Else
wscript.quit 1
End If
Else
wscript.quit 1
End If
Ok so I had set this for the date I ran this on, within the hour I was in. But
it didn't work. I expected the VBS to start playing MYSOUND.wav but it didn't. When running the file
there were no errors though, so I was wondering what I did wrong!
I running Windows 7
If anyone could tell me what I did wrong, and how to fix it that would be great.
Double points if anyone could post a corrected version of the code!
Thanks to any answers!

First, indent your code and give your variables meaningful names!
Then, your date comparison doesn't work because you're trying to compare strings as if they were dates. This usually won't work (depending on your "system locale"): you need to use date type variables and an actual date comparison function (DateDiff in VBScript).
(EDIT: as Ansgar Wiechers pointed out, you don't need to use DateDiff to compare dates in VBScript, "DateStart <= Now And Now <= DateEnd" will do just fine)
Try this:
Dim DateStart, DateEnd, WshShell, music
DateStart = DateSerial(2013, 8, 13)
DateEnd = DateSerial(2013, 8, 15)
If DateDiff("D", DateStart, Now) >= 0 And DateDiff("D", Now, DateEnd) >= 0 Then
If Hour(Now) >= 9 And Hour(Now) < 22 Then
'*** delete after debugging ***
MsgBox "play sound"
Set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
'*** 2nd parameter : 0 hides wmplayer, 1 shows it ***
WshShell.Run "wmplayer """ & music & """", 1, True
Else
'*** delete after debugging ***
MsgBox "Not the right time"
End If
Else
'*** delete after debugging ***
MsgBox "Not the right day"
End If
Also, if you want to debug a small script like this, you can call MsgBox to do a simple tracking of what's actually executed (in your example, replacing your "WScript.Quit 1" by MsgBox would show you that the date is not properly compared.

Related

How to get r to read 'date taken' of a JPG file

I could use some help performing a database correction, in regards to date and time of pictures were taken.
Essentially, the research we perform entails taking many pictures and entering the picture information into a database (we automated this with Microsoft Access). However, I performed a random check of our database and found that several dates and times of the photos were incorrect, and I am attempting to correct this via R as I was unable to correct it via Access.
What I need to do is to is write a script that reads these data, and compiles a list of the date taken information for all photos (there are several thousand). So far the best thing that I've found is
file.info(list.files(
"E:/Whatcom Creek Project/Data/Seal photos/Discovery/Catalog/Phoca vitulina",
recursive = T))
However this returns a list of NA NA for all information. Also, if I manually select one image to run file.info on, it doesn't return date taken (see the picture for the data I am attempting to retrieve)
If anyone has any suggestions I am all ears. Thanks in advance!!
-Ian
enter image description here
maybe still of any use?
i saw this message. I solved this a long time ago (2011) with a very basic VB6 code. The job is still working, but it is not applicable to all '.jpg files'. It depends with what camera the picture has been taken, but mostly pictures taken from a smartphone and classic camera's are returning the date the picture has been taken (not usable for whatsapp images or edited images).
The code is here below, and it is very basic code (but i hope it still can help or give idea's, and ... any new idea's are welcome too) :
'Reading of "Date Picture Taken" from .jpg file
'-----------------------------------------------
Debug.Print ">>>>>==== Start Read of .jpg-file ===== "; Date; " ===== "; Time; " ====<<<<<"
X = 0
DatePictureTaken = ""
TimePictureTaken = ""
FOUNDdatum = False
SWdatum = False
TELdatum = 0
Foto = FOLDER & "\" & tabFileName(FotoNr)
Open Foto For Binary Access Read As #1
'get startposition of the field "date picture taken"
Do
X = X + 1
Get #1, X, MyByte ': Debug.Print Chr(MyByte);
DoEvents
If Chr(MyByte) = ":" Then
'Debug.Print
SWdatum = True
Get #1, X + 3, MyByte
'Debug.Print "x+03="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+03="; Chr(MyByte)
Get #1, X + 9, MyByte
'Debug.Print "x+09="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+09="; Chr(MyByte)
Get #1, X + 12, MyByte
'Debug.Print "x+12="; Chr(MyByte)
If Chr(MyByte) <> ":" Then SWdatum = False 'Debug.Print "x+12="; Chr(MyByte)
'if a ':' is on the 3 locations (found above) then it is a date!
If SWdatum _
Then
TELdatum = TELdatum + 1
End If
'the 3e date is the date the picture has been taken
If TELdatum = 3 _
Then
FOUNDdatum = True
X = X - 4
Exit Do
End If
X = X + 12
End If
Loop Until EOF(1) 'Or X = 32765
If FOUNDdatum = False Then Close 1: End
BPdatum = X
EPdatum = X + 9
BPuur = X + 11
EPuur = X + 15
For X = BPdatum To EPdatum
Get #1, X, MyByte
'Debug.Print Chr(MyByte)
If Chr(MyByte) <> ":" _
Then
' DatePictureTaken = DatePictureTaken & "/"
'Else
DatePictureTaken = DatePictureTaken & Chr(MyByte)
End If
Next X
For X = BPuur To EPuur
Get #1, X, MyByte
'Debug.Print Chr(MyByte)
If Chr(MyByte) <> ":" _
Then
' DatePictureTaken = DatePictureTaken & "/"
'Else
TimePictureTaken = TimePictureTaken & Chr(MyByte)
End If
Next X
'Debug.Print "Date Picture Taken = "; DATUM
tbxDatum.Text = DatePictureTaken
tbxUur.Text = TimePictureTaken
Close 1
End Sub

EPOC date to Normal system date in VBS

I have read many post but nothing really helped.
My question looks similar but bit different.
I have to change EPOC time to system time format.
data type = datetime
value= 20160630165419.634204+060
desired output
data type = datetime
value= 30/06/2016 16:54:19
as the EPOC value has digits after dot and the datatype is datetime , which makes it difficult to divide it by 10 in a loop and get ingteral value.
Please suggest solution for given input format only.
You can use this function WMIDateStringToDate for converting your date :
WScript.echo WMIDateStringToDate("20160227235343.000000+060")
WScript.echo WMIDateStringToDate("20160630165419.634204+060")
'************************************************************
Function WMIDateStringToDate(Mydate)
WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
& " " & Mid (Mydate, 9, 2) & ":" & _
Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************
Step 1: use DateSerial() and TimeSerial() on numbers (CInt()) extracted from your input (Mid()) to get a variant of subtype Date
Step 2: use SetLocale() and Replace() to format/stringify the Date
>> SetLocale "de-de"
>> sX = "20160630165419.634204+060"
>> dtD = DateSerial(CInt(Mid(sX, 1, 4)), CInt(Mid(sX, 5, 2)), CInt(Mid(sX, 7, 2)))
>> dtT = TimeSerial(CInt(Mid(sX, 9, 2)), CInt(Mid(sX, 11, 2)), CInt(Mid(sX, 13,2)))
>> dtX = dtD + dtT
>> WScript.Echo dtX, TypeName(dtX)
>> WScript.Echo Replace(dtX, ".", "/")
>>
30.06.2016 16:54:19 Date
30/06/2016 16:54:19
Danger, Will Robinson! Look at this test script:
Function WMIDateStringToDate(Mydate)
WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
& " " & Mid (Mydate, 9, 2) & ":" & _
Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************
For Each sLocale In Split("de-de en-us")
SetLocale sLocale
For Each sDate In Split("20160227235343.000000+060 20160203235343.000000+060")
On Error Resume Next
dtX = WMIDateStringToDate(sDate)
If Err.Number Then dtX = Err.Description
On Error GoTo 0
WScript.Echo GetLocale(), sD, sDate, dtX
Next
Next
and its output:
cscript 38249865.vbs
1031 20160227235343.000000+060 27.02.2016 23:53:43
1031 20160203235343.000000+060 02.03.2016 23:53:43
1033 20160227235343.000000+060 27.02.2016 23:53:43
1033 20160203235343.000000+060 03.02.2016 23:53:43
to see the reason for:
The sample scripts are not supported under any Microsoft standard
support program or service. The sample scripts are provided AS IS
without warranty of any kind. Microsoft further disclaims all implied
warranties including, without limitation, any implied warranties of
merchantability or of fitness for a particular purpose. The entire
risk arising out of the use or performance of the sample scripts and
documentation remains with you. In no event shall Microsoft, its
authors, or anyone else involved in the creation, production, or
delivery of the scripts be liable for any damages whatsoever
(including, without limitation, damages for loss of business profits,
business interruption, loss of business information, or other
pecuniary loss) arising out of the use of or inability to use the
sample scripts or documentation, even if Microsoft has been advised of
the possibility of such damages.
(found here)

IsDate validates wrong format

I have these lines
SetLocale(3081)
Response.Write "<p>TEST: " & Date() & " | " & isDate("3/22/2014") & " --> " & GetLocale() & "</p>"
which outputs
TEST: 3/07/2014 | True --> 3081
now correct me if i'm wrong but isn't there only 12 months in a year?, according to IsDate the date i've passed, which should be wrong because i have put in 22 as the month, is valid despite the local settings saying otherwise.
i want to validate the date to be the correct format to insert into the database and if it isn't give a more friendly error, "3/22/2014" will output "Error converting data type varchar to date." when i try to inster it into the database because it's getting by the IsDate check
What have i done wrong here?
Yes, this is indeed a valid date.
Why? Because VBScript is smart/generous/stupid (choose your favorite) enough to treat numbers as dates. And "3/22/2014" can be parsed as formula: 3 / 22 / 2014 = 6.770786313983931e-5
Now take this number and convert to date:
Dim myNumber, myDate
myNumber = 6.770786313983931e-5
myDate = CDate(myNumber)
The variable myDate will be perfectly valid date, which is December 30th 1899, 00:00:06
So bottom line: the value is a date, just not what you expect. You've done nothing wrong, but to really check if a string is valid date you will have to check it yourself, no out of the box methods.

Classic ASP, IIS 8.5, CINT CLNG not working

Reinstalled my dev machine with win 8.1
Enabled ASP, installed my legacy websites that I have to maintain, installed sites as an application in IIS, enabled parent paths, send errors to browser on, full detail set on in error pages. All is working well, except when I work with numbers
I'm working in a system that captures financial data inputted by users, so often a number will be: 100.0 or
100.000 or
100 or
100,00
so I have a function that (I've used on loads of websites in the past) that will standardise the irregularities into a constant format, such as 100.00 (ie: proper decimal, always using a dot, etc)
This line now fails on win 8.1 IIS 8.5: If Cdbl(myString) = Cdbl(0) Then
Function ProperDecimal(s_String)
Dim CharPlace, DotChar, iLoop, strCurrentChar, myString
myString = TRIM(s_String)
If ISNULL(myString) OR myString = "" Then myString = 0
myString = REPLACE(myString,",",".")
'Find where the comma or dot is, ie: 100.00 is in position 3 (from the right)
CharPlace = 1
DotChar = 0
For iLoop = Len(Replace(myString, " ", "")) to 1 Step -1
strCurrentChar = mid(Replace(myString, " ", ""), iLoop, 1)
If strCurrentChar = "." OR strCurrentChar = "," Then
DotChar = CharPlace
Exit For
End If
CharPlace = CharPlace + 1
Next
'If string is zero, leave it at zero, we dont need 0.00
If Cdbl(myString) = Cdbl(0) Then
'ignore it, no decimal places needed
ProperDecimal = myString
Else
'Where is the DOT
Select Case DotChar
Case 0 'eg: 100 will be converted to 100.00
ProperDecimal = (myString & ".00")
Case 1 'eg: 100. will be converted to 100.00
ProperDecimal = (myString & "00")
Case 2 'eg: 100.0 will be converted to 100.00
ProperDecimal = (myString & "0")
Case 3 'eg: 100.00 will remain
ProperDecimal = (myString)
Case Else '(4, 5, 6, 7, 8, etc) 'eg: 100.001
ProperDecimal = ProperDecimal(Round(myString,2))
End Select
End If
End Function
Call ProperDecimal("112.05")
This lead me to try other methods other than CDbl
Response.write(isNumeric(s_String)) 'works, returns false because this is a string
Response.write(CINT(myString))
Response.write(CLNG(myString))
Response.write(CDBL(myString))
Returns this:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CINT'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CLNG'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CDBL'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CINT'
If I then change the If statement to convert the 0 to a string, it works:
If myString = CSTR(0) Then
But I really dont want to be comparing strings to strings especially with numbers
This is now the second project this is happening on. The previous project I had to change the decimal comma to a fullstop (112,05 had to be 112.05) so I thought it had something to do with regional settings and the comma being listed as decimal character. But am getting different results now, its driving me up the wall.. plz assist if you can. Ta
CInt on the server works fine (2008 R2), on the clients pc (windows Xp and windows 7) but on my windows 8 machine, this is an issue.
I made my own two functions which I use now instead of CInt and CDbl. You can make more if needed. Pretty simple and tested,
EDIT:
Changed the function names toe shorter versions. Easier to replace and to use. Also does not clash with Javascripts parseInt if you do a search
Function pInt(Str)
Dim val : val = 0
'==Test Comma=='
On Error Resume Next
val = CInt(Replace(Str, ".", ","))
'==Test Period=='
On Error Resume Next
val = CInt(Replace(Str, ",", "."))
On Error Goto 0
pInt = val
End Function
Function pDbl(Str)
Dim val : val = 0
'==Test Comma=='
On Error Resume Next
val = CDbl(Replace(Str, ".", ","))
'==Test Period=='
On Error Resume Next
val = CDbl(Replace(Str, ",", "."))
On Error Goto 0
pDbl = val
End Function
I am not a pro in ClassicASP/VBScript but this works. I like my C# ASP.net

ASP formatting date

Hello there I'm trying to get a date in ASP to show up in a particular format (yyyymmdd). This is what I've tried so far but no luck. Any help is appreciated. Thanks
<tr>
<td><b>Call Date</b></td>
<% for i = -6 to 0 %>
<td align=center>
X
</td>
<% Next %>
</tr>
You can make use of the following functions:
Year(Now) '' Year in 4 digits
Month(Now) '' Month without leading zero
Day(Now) '' Day of the month without leading zero
DateAdd("d", <numofdays>, Now) '' add a number of days to your date
Read more about these (and other date functions) functions here.
If you need to add a leading zero:
function addLeadingZero(value)
addLeadingZero = value
if value < 10 then
addLeadingZero = "0" & value
end if
end function
An example of your case would be:
Dim today, myDate
today = Now
for i = -6 to 0
myDate = DateAdd("d", i, today)
response.write "X"
next
Sorry to dig this up, but it might be of help to some people. Rather than the "If<10 then add leading zero" logic, I often use the right command and always add a leading zero...
response.write "X"
..This way, you don't need a separate function, and it can be done on one line. I can't speak for the efficiency of it, but it seems logical.
ASP gets the date from the OS not from the Database, a common error, but it is solved by use:
<%
' Date dd/mm/yyyy
Session.lcid=2057 '= UK English
%>
I hope it helps people.
You can try. 100% tested!
<%
mm = Month(now())
dd = Day(now())
yy = Year(now())
IF len(mm) = 1 THEN
mm = "0" & mm
END IF
IF len(dd) = 1 THEN
dd = "0" & dd
END IF
response.write(yy & "/" & mm & "/" & dd)
%>
<%= DatePart("yyyy", Now) & "/" & DatePart("m", Now) & "/" & DatePart("d", Now) %>
Also refer
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
http://www.mikesdotnetting.com/Article/22/Date-formatting-in-VBScript
Thanks
Deepu
Mid(date(), 7,4) & "-" & Mid(date(), 4,2) & "-" & Left(date(), 2)

Resources