Time in 24 hour format in VBScript - vbscript

I am trying to get the system time in 24 hour format using VBScript. I had gone through the documents and found nothing. I need to compare the system date and time with my data and need to check the differences.

WScript.Echo right("0" & hour(time),2) & ":" & right("0" & minute(time),2)
WScript.Echo FormatDateTime(time, vbShortTime)

VBScript's Date datatype is format-agnostic. You can calculate the difference between the system time and some other timestamp using the DateDiff function, as long as VBScript recognizes the format of the other timestamp. Example (calculating the difference in seconds):
>>> systime = Now
>>> WScript.Echo systime
26.11.2013 12:48:52
>>> WScript.Echo DateDiff("s", systime, "11/25/2013 23:16:52")
-48720
>>> WScript.Echo DateDiff("s", systime, "25.11.2013 23:16:52")
-48720

Related

VBScript to copy/move file by lowest value in filename

I'm fairly new to scripting and am in need of some help. I have come across a unique situation for a Non-Profit client of ours that requires us to compare two or more files in a specific folder and move the file with the lowest numerical value in the filename.
This organization runs a non-profit radio station which has content submitted from hundreds of volunteers that name their files (when they record more than one) with various numbers at the end that either represent the date or the order in which the files are to be aired.
Essentially I am looking to create a vbscript (because I think it can be done this way) that will run with windows task scheduler 30 minutes prior to the first air date of the content and move the file with the lowest value (if more than one file exists) to a folder where it will be automatically processed by the radio automation software.
Examples of files in a folder might look something like these:
Folder1: (in this instance, "news.mp3" is the lowest value)
news.mp3
news1.mp3
news2.mp3
Folder2:
entertainment24.mp3
entertainment26.mp3
Folder3:
localnews081420.mp3
localnews081520.mp3
Honestly, on this one, I'm not even sure where to start. I've found several scripts that can look at file date or a specific numerical or date format in the filename, but none that can parse numbers from a filename and move/copy a file based on the numerical value. I'm hoping there is someone out there smarter than me that can point me in the right direction. Thanks for looking at my problem!
One script I've been playing with (from the scripting guy) looks at specific years in a filename:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colFiles = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _
& “ResultClass = CIM_DataFile”)
Set objRegEx = CreateObject(“VBScript.RegExp”)
For Each objFile in colFiles
objRegEx.Global = True
objRegEx.Pattern = “\d{4}”
strSearchString = objFile.FileName
Set colMatches = objRegEx.Execute(strSearchString)
strYear = colMatches(0).Value
strNewFile = “C:\Test\” & strYear & “\” & objFile.FileName & _
“.” & objFile.Extension
objFile.Copy(strNewFile)
objFile.Delete
Next
...but I can't seem to make the leap to regular numbers and then take a lowest value...
You can use FileSystemObject to Work with Drives, Folders and Files.
Also i used GETNUM function to get number.
Try my way :
sFolder = "C:\Test\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile in oFSO.GetFolder(sFolder).Files
Number=GETNUM(objFile.Name)
strNewFile = sFolder & Number & "\" & objFile.Name
If NOT (oFSO.FolderExists(sFolder & Number)) Then
oFSO.CreateFolder(sFolder & Number)
End If
oFSO.MoveFile objFile, strNewFile
Next
Function GETNUM(Str)
For i=1 To Len(Str)
if IsNumeric(Mid(Str,i,1)) Then
Num=Num&Mid(Str,i,1)
End if
Next
GETNUM=Num
End Function
For understanding the used code and how they work, open these sites and read all pages very carefully.
MoveFile method
Vbs Script to check if a folder exist

How to save file in vbscript with previous date

I am trying to save excel file with previous date in vbscript.
Fill has been save but date format not like 5-Jun-2019.
Please help me..
MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now)-1, 2)
wbm.SaveAs "C:\report " & MyDateFormat & ".xlsx"
Expected Actual Result like:5-Jun-2019
date-1 gives yesterday "05.06.2019" in my case, this will not let you encounter day = 00, rest should be done by formatting

How does the DateAdd function in ASP Classic work with the "ww" weeks option?

I have to replicate functionality in C# from an old asp classic codebase.
The codebase seems to assume that the year can have up to 54 weeks and uses the DateAdd method with the "ww" option to figure out the "week ID" for purposes of categorising some files.
All of the online documentation I have found seems to ignore this question and a straightforward reading of it suggests that adding weeks is the same as adding (weeks * 7) days, however, I don't want to break backwards compatibility. I think that the old code has an off by one error but I don't want to assume it. It's possible for a year to have its days in 54 different weeks but I don't think that doing a DateAdd with a value greater than 52 ever makes sense.
My question is, how does the
DateAdd("ww", x, date)
work?
Is it the equivalent of
date.AddDays(7 * x)
in C#?
Are there edge cases where the first of January falls on (for example) a Saturday and DateAdd("ww", 53, date) could return a valid date in the same year?
I can tell you that DateAdd("ww", i, date) is equivalent to DateAdd("d", 7*i, date); therefore, provided that both functions in C# and vbScript have the same meaning, they must be equivalent.
To ensure DateAdd("ww", i, date) is equivalent to DateAdd("d", 7*i, date) I used the script bellow, it cycles 2 years and for each day, it cycles for 104 weeks if adding days or adding weeks mismatch somehow, it raises an error.
date0 = Date()
For i = 0 to 2 * 365
date1 = DateAdd("d", i, date0)
For j = 0 to 2 * 53
date2 = DateAdd("d", j * 7, date1)
date3 = DateAdd("WW", j, date1)
assert = DateDiff("s", date2, date3)
If assert <> 0 Then
errMsg = _
"OPS addDays And addWeeks unmatched" &_
vbCrLf &_
CStr(date1) & vbTab &_
CStr(date2) & vbTab &_
CStr(date3) & vbTab &_
assert & vbTab
Call Err.Raise(vbObjectError + 10, "Test DateAdd(""ww"", i, date)", errMsg)
End If
Next
Next
So I guess you can use date.AddDays(7 * x) to replace DateAdd("ww", x, date) with no concerns in mismatching dates.

Having the day value of date start with 0

I am editing a VBScript so it will take yesterdays date add a string to the front off it and search for that file before moving it to a folder.
sDate = day(date)-1
sName= "Blaa" & "_" & sDate
Using the above bits of code I would get a result - Blaa_10 or Blaa_9
The issue is the files, I want it to search for when under 10 would be named as Blaa_09.
Is there anyway I can format the value day so when it is under 10 it starts with 0? Think I could write an If statement to do this but was hoping there is another way.
The canonical way to left-pad a day of month with a zero in VBScript looks like this:
Right("0" & Day(Date), 2)
Just went with
sDay = day(date) -1
IF sDay < 10 Then
sDate = "0"& sDay & monthname(month(DateAdd("m",-1,Date)), True) & year(date)
WScript.Echo "Date = " & sDate
This worked fine, if anyone has an easier way please feel free to share.

Getting month and year date format in vbscript

Can someone please tell me how can we get data in the month and date format.In the below code it shows me the year with the same date one year ago in the format 6/18/2012 ..but I just need the month/year.
LastMonth = DateAdd("m",-12,Date)
Thanks
Use the specialized functions Month(), Year(), ... to get at the private parts of a (variable of type) Date:
>> dt = DateAdd("m",-12,Date)
>> WScript.Echo TypeName(dt), CStr(dt), Month(dt) & "/" & Year(dt)
>>
Date 6/18/2012 6/2012

Resources