VBScript to format the file and date <filename><date>.csv [duplicate] - vbscript

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
Hi I need help to set the file name as .csv
currently we could set the file name as 2_10_2018user.csv with the below codes
Set CPY = CreateObject("Scripting.FileSystemObject")
CPY.CopyFile "\\users\user.csv", "\\home\group\" & Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True
Set CPY = nothing
'MsgBox("Done")

You cannot use the ANGLE brackets in a file/folder name it is not permitted.
In fact, The following characters are NOT permitted:
/ \ < > ? * | : "
The bit which you state concerns you:
Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True"
Can be broken down into the following individual commands:
FormatDateTime(Date(),2)
This is telling the system to format the system date (date today) as a SHORT DATE
The options for the second parameter are:
0 = vbGeneralDate (date and time)
1 = vbLongDate (long date)
2 = vbShortDate (short date)
3 = vbLongTime (long time)
4 = vbShortTime (short time)
So now we have the date format,
Next is the replace action :
Replace(FormatDateTime(Date(),2),"","-") & "users.csv",True"
where we replace the date delimiters in this case "" (null string) with a hyphen "-" (-)
So this part of the filename changes from 010119 to 01-01-19 (for example 1st Jan 2019)
and "users.csv" is attached to the end of it all
Finally the TRUE comment tells the system to OVERWRITE the file if it already exists!
If you want the filename before the date then simply swap the bits over:
CPY.CopyFile "\\users\user.csv", "\\home\shared\group\" & "users-" & Replace(FormatDateTime(Date(),2),"","-") & ".csv",True

Related

Applescript to sort files into folder based on British financial year

I'm looking for way to move files based on UK financial year (runs from 6th April -5th April).
Files are named in pattern as
2014-08-26_Asda_lunch.pdf
2016-03-20_Tesco_sationary.pdf
The File needs to be moved to folders which are named, and so on
FY 2014-15
FY 2015-16
Just wondering if applescript/ shell script or automator action would help to achieve this. Also interface with hazel wud be even better
Thanks in advance
I have tried to modify the script
My first aim is get month right, then wud try dates;
the Output for Script
File 2019-07-26_Tesco_stationary -> FY 2020 ( expected FY 2019-20)
File 2019-03-15_Sainsbury -> FY 2019 ( expected FY 2018-19)
Please advise, also any pointers to add date in sorting wud be helpful
Thank you
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}
tell application "Finder"
set filename to name of theFile
end tell
set expenseYear to (first text item of filename) as number
set expenseMonth to (second text item of filename) as number
set expenseDate to (third text item of filename) as number
-- Get the last two characters of the Year
set AppleScript's text item delimiters to savedDelimiters
set lastTwoCharactersOfYear to (characters 3 thru 4 of (expenseYear as text))
set RoundedExpYear to (lastTwoCharactersOfYear as text) as number
if expenseMonth ≥ 4 then
set LongString to expenseYear
set ShortString to RoundedExpYear + 1
else
set LongString to expenseYear - 1
set ShortString to RoundedExpYear
end if
set returnText to "FY" & " " & LongString & "-" & ShortString
There are many ways to parse dates but since your format is always the same (yyyy-mm-dd_xxxx) I used the easiest way. In script below the handler GetFY returns directly the format you're looking for "FY YYYY-YYYY" when you give parameter your file name:
set Fname to "2014-03-05_xxxx" -- value to test
set myfolder to GetFy(Fname)
log "myfolder=" & myfolder
on GetFy(Fname) -- return FY-(FY+1) based on Fname as YYYY-MM-DD_xxxxxx
set myear to (text 1 thru 4 of Fname) as integer
set mmonth to (text 6 thru 7 of Fname) as integer
set mday to (text 9 thru 10 of Fname) as integer
if mmonth < 4 then set Fy to myear - 1
if mmonth = 4 then set Fy to myear - ((mday ≤ 5) as integer)
if mmonth > 4 then set Fy to myear
return "FY " & Fy & "-" & (Fy + 1)
end GetFy

VBS Object Required Error manipulating strings

I am trying to create a simple script that prompts the user for their birthday, formatted as 10.02.20, then takes that string and turns it into text, such as October 2nd 2020. I have the following code:
Dim bd, message, title ' define variables
title = "What is your birthday?" ' set variable
message = "Format like so: 12.15.07; 3.03.05" ' set variable
bd = InputBox(message, title) ' prompt user
Dim year
year = bd.Substring(6, 2)
Dim month
month = bd.Substring(0, 2)
Dim day
day = bd.Substring(3, 2)
msgbox bd ' for testing
call msgbox(year + month + day) 'also testing
And I am getting an error after the prompt, ...\Desktop\test.vbs(8, 1) Microsoft VBScript runtime error: Object required: '12.23.03' and I am not sure what it means, Object Required.
Any fixes or suggestions would be very much appreciated.
You cannot use Substring in VBScript. You can use Mid instead:
Dim year
year = Mid(bd, 7, 2)
The first character in a string is at position 1, not 0, so I adjusted your parameter from 6 to 7.
Also, to concatenate strings, although + works, you can also use &:
call msgbox(year & month & day)
You can use this function in vbscript : FormatDateTime(date,format)
Parameter Description
date Required.
Any valid date expression (like Date() or Now())
format Optional.
A value that specifies the date/time format to use can take the following values:
0 = vbGeneralDate - Default. Returns date: mm/dd/yyyy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yyyy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
Dim Title,Input
Title = "format date"
Input = InputBox("Enter your date of Birthday !",Title,"10.02.20")
Input = FormatDateTime(Replace(Input,".","/"),1)
MsgBox Input

How can I reformat Date Time output into ISO 8601 [duplicate]

This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 3 years ago.
Using the code below it outputs the following value: 04/10/2019 which is due to the server being set to UK date time settings. However google is reading this date as 10 April 19. So I need to convert it to ISO8601 formate, ie 10/04/2019
Dim FutureDate, TodaysDate
FutureDate = FormatDateTime(Now(),1)
TodaysDate = DateAdd("m", 0, FutureDate)
FutureDate = DateAdd("d", +100, TodaysDate)
"priceValidUntil": "<%= FutureDate %>",
I don't remember ASP supporting this, you can change the format between long date, short date, ... but not to ISO8601
I used something like this to do it (string split and concat):
d = split(FutureDate,"/")
future_iso = d(1) & "/" & d(0) & "/" & d(2)

Remove last 4 characters using VB script from folder name [duplicate]

This question already has answers here:
How Do I Use VBScript to Strip the First n Characters of a String?
(4 answers)
Closed 4 years ago.
I am new to VB script and trying to create a script in which i am looping in a folder and printing all the subfolder names in a output text file.
All the subfolders have same 4 characters at the -tst , i want to remove this last 4 character and print rest of the name.
For example - original name - Test_name-tst
expected output name - Test_name .
'Creates new log file and write in it
Set FSO = CreateObject("Scripting.FileSystemObject")
outFile="D:\Test\test_output.LOG"
Set objFile = FSO.CreateTextFile(outFile,True)
objFile.Write "START_" & vbCrLf
strFolder ="C:\apps\Test\"
'Get a reference to the folder you want to search
Set FLD = FSO.GetFolder(strFolder).Subfolders
'Loop through the folder and get the file names
For Each Fil In FLD
If InStr(Fil.Name , "-prj") Then
objFile.Write Fil.Name & vbCrLf
End If
Next
Thank you.
Thank you all. Left and len worked for me.
var1 = Left(Fil.Name, Len(Fil.Name) - 4)
Reverse the name, cut the first four characters with Mid() and reverse the string again:
StrReverse(Mid(StrReverse(Fil.Name), 5))

visual basic difference in dates between two lines in text file

I am new to vb express and looking for a way to read two lines in a text file get the difference between then and loop it till the end its a simple clock in clock out system which store each persons clock on and off time in a text file like so
03/11/2014 09:55:02
03/11/2014 14:55:02
03/11/2014 16:55:02
03/11/2014 19:55:02
04/11/2014 09:00:02
04/11/2014 13:00:00
I know I use the DateDiff to get the time but I only want them to work out the difference between line 1 and 2 then 3 and 4 and add them all up is it possible to do that without over complicating things?
I guys I have worked it out I have done this by reading the text filed line by line in a loop at the moment I have not put any validation in to show people who have forgot but the basics are there
Dim FILE_NAME As String = "times\08.txt"
Dim start As DateTime
Dim finish As DateTime
Dim total
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
start = objReader.ReadLine() & vbNewLine
finish = objReader.ReadLine() & vbNewLine
duration = DateDiff(DateInterval.Minute, start, finish)
total = duration + total
Loop
Label2.Text = total

Resources