Find and copy the latest file - vb.net

Our program is going to save PDFs to a network folder. I will then be copying the PDFs to a web server. There becomes a problem when a user saves/archives the same report more than once.
Example:
DAGH4 (002).PDF - Date Modified = 3/16/2016 10:10 AM
DAGH4 (001).PDF - Date Modified = 3/16/2016 9:00 AM
DAGH4.PDF - Date Modified = 3/16/2016 8:00 AM
DANYL (001).PDF - Date Modified = 3/16/2016 10:50 AM
DANYL.PDF - Date Modified = 3/16/2016 9:50 AM
Is there a way I could copy the latest version? I only want DAGH4 (002).PDF and DANYL (001).PDF to be copied. Or is there a way to rename and save the latest version?
Another thought
Using this code I check if the file exists and allow the user to click an thumbnail to open the associated PDF.
Dim pdf_path = Server.MapPath(String.Format("../Reports/{0}.pdf",
SerialNumber))
lblSerial.Visible = System.IO.File.Exists(pdf_path)
HyperLinkSerial.Visible = System.IO.File.Exists(pdf_path)
HyperLinkSerial.Text = "Inspection"
HyperLinkSerial.NavigateUrl = "../Reports/" & SerialNumber & ".PDF"
However, I need to only find and show the DAGH4 (002).PDF with the modified date 3/16/2016 10:10 AM
Is that possible using VB .NET?

To find the latest copy of each file, you could use LINQ to group by serial number, sort by modification date, and select the first item for each group. Note that this assumes that your "SerialNumber" is just letters and numbers (what the regex matches):
Dim latest As IEnumerable(Of String) =
From f In New DirectoryInfo(Server.MapPath("../Reports")).EnumerateFiles("*.PDF")
Let serialNumber = Regex.Match(f.Name, "^\w+").Value
Order By serialNumber Ascending, f.LastWriteTime Descending
Group By serialNumber Into Group
Select Group.First().f.FullName
latest will have the full path/name for the latest (based on modification date) file for each serial number.

Related

Updating Due Date based on Invoice Date and Vendor

I am using DocuWare Workflow Designer (uses SQL) to fill in the due date based on the invoice date and vendor name. In the code below I am trying to have it add 15 days to the invoice date if the vendor is "Hueber - Jones & Coontz" otherwise have the Due Date equal the Invoice Date. The code errors out currently and makes the Due Date equal todays date. I am not able to bring up where in the code its failing. Everything in brackets is references to predefined variables in DocuWare.
SELECT [DW_INVOICE_DATE] FROM [AP] WHERE IF [DW_VENDOR_NAME] = 'HUEBER - JONES & COONTZ' [DW_DUE_DATE] = DATEADD(DD,15,[DW_INVOICE_DATE] ELSE [DW_DUE_DATE] = [DW_INVOICE_DATE]
Thanks in advance.
I don't know how DocuWare works, it it's just simple SQL this is the command:
UPDATE [AP]
SET [DW_DUE_DATE] = (CASE
WHEN [DW_VENDOR_NAME] = 'HUEBER - JONES & COONTZ' THEN DATEADD(DD,15,[DW_INVOICE_DATE])
ELSE [DW_INVOICE_DATE]
END)

Create Auto Repeated Records by Time

I just new in access, and I have to face up the DB everyday. In this case, I create a form to record scedule of planned maintenance (next I call it "PM") which has variaties divided of time by week, month and year. Every PM that has been created, it has an unique number of PM. The number is as follows : 162/PNT/PM/06/2020. The number was containing with : "unique number"/"PM's area"/"type of maintenance"/"month"/"year".
Then, is there a way to create records repeatly based on time or previous PM schedule with the same content?
For an example :
I create a monthly PM schedule which the number is 001/PNT/PM/01/2020 on 20-Jan-2020. When I press the save button (or copy schedule button), the record will copying (creating) to 20-Feb-2020 with changed PM's number to 002/PNT/PM/02/2020 automatically.
Thanks for anyone who wants to help me.
June is right. Use a table like this:
ID - Long
PNT - Short Text
PM - Short Text
StartDate - DateTime
and a query to assemble the code:
SELECT
Maintenance.ID,
Maintenance.PNT,
Maintenance.PM,
Maintenance.StartDate,
Format([ID],"000") & "/" & [PNT] & "/" & [PM] & "/" & Format([StartDate],"mm\/ddyy") AS Code
FROM
Maintenance;
Output:
Now, to create the next record, use:
NextID = ID + 1
NextDate = DateAdd("m", 1, StartDate)

Date format problems when passing information from Access to Excel

just for documentation and future readers, the problem was solved and the answer is in the discussion between me and #varocarbas
My Application is reading a few columns from access data base and joining those columns into an existing datatable. one of the columns in the database has dates, the column is configured as date and time column in the database and in the data column of the datatable. the problem is when i am extrcting it into excel ,some of the dates are behaving in the oposite direction (i think the term is "english date"). what that i mean by "behaving", is that excel recognize the monthes as the days and the days as the months, for example a date that should be 11 in september 2015 the excel formula month(11/09/2015) is showing that the month is november and not september.
also, the date values in the access column are from sharepoint list that is also configured as dateandtime.. i have a filling that this behaving maybe belongs to excel(?),
here is the importent parts of my code:
' declaring the dateandtime column
Dim colString As DataColumn = New DataColumn("changed")
colString.DataType = System.Type.GetType("System.DateTime")
UnitedTable.Columns.Add(colString)
Storing it from the database:
If Not (IsDBNull(UnitedTable.Rows(i).Item("spcall"))) Then
spcall = UnitedTable.Rows(i).Item("spacll")
rowNum = Searchbinary(spcall) ' check that spcall exist in Projects table via binary search
If rowNum > -1 Then
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("PmUserName").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("OpertionalStatus").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("CrmCallNum").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("OperationStart").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("Approved").ToString
UnitedTable.Rows(i).Item("somecolumn") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("GeneralStatus").ToString
UnitedTable.Rows(i).Item("changed") = db.ReadDataSet.Tables(0).Rows(rowNum).Item("Changed")
Else
' the spcall not exist add it to arraylist
SpCalls_NotExist.Add(spcall)
'UnitedTable.Rows(i).Delete()
End If
Then I am populating it to excel with nested loop.

Add Query with Month filter to an Access Database with Date/Time Field

I have an Access Database with many fields connected through a datagridview in my vb.net project. Two of these fields contain Date/Time Values. I want to create a query through the query builder that uses input from the user to find records that match the dates the user wants. This "where clause" works :
WHERE BETWEEN ? AND ?
This creates a toolstrip in which I can input 2 dates so that the query can fill the datagridview with the records.
What I want now is to make a query like the above only this time the user inputs the name of a month he wants (ex. February or 02 ). Is there any way to do that ?
EDIT: Tried using Plutonix's code (Sailing is the Column name) :
WHERE SAILING BETWEEN #01/31/yyyy# AND #03/01/yyyy#
and I got this error: "Cannot convert entry to valid datetime TO_DATE function might be required"
EDIT 2: I have created a combobox containing all 12 months and I have a commandbutton. I want to find a way so that if the user selects one of the 12 months from that combobox and clicks the commandbutton, to have the datagridview control (access database) show him only the records that go with that month based on their datetime(Short Date) fields. What code should I put in my commandbutton_click ?
Eventually I created a long query where I added a "Between ? and ?" in my where clause for every Year included in the database (2004-2015), created a huge if clause that gives the beginnings and endings of the month requested for every year and used all those strings to query the database.
If Month.Text = "JANUARY" Then
A04A = "01/01/04"
A04B = "31/01/04"
A05A = "01/01/05"
A05B = "31/01/05"
A06A = "01/01/06"
A06B = "31/01/06"
A07A = "01/01/07"
A07B = "31/01/07"
A08A = "01/01/08"
A08B = "31/01/08"
A09A = "01/01/09"
A09B = "31/01/09"
A10A = "01/01/10"
A10B = "31/01/10"
A11A = "01/01/11"
A11B = "31/01/11"
A12A = "01/01/12"
A12B = "31/01/12"
A13A = "01/01/13"
A13B = "31/01/13"
A14A = "01/01/14"
A14B = "31/01/14"
A15A = "01/01/15"
A15B = "31/01/15"
A16A = "01/01/16"
A16B = "31/01/16"
ElseIf etc etc

Need to pull only last date in table that stores change dates SQL / ODBC

Hope somebody can help me with this. I'm trying to pull a list of forthcoming titles (I work in publishing) via ODBC/ms query. I want (amongst other things) to show their internal status (approved, prepress etc.). The database stores the change dates for the status'. I seem to be getting one line per status per title. So if the title has changed status 6 times, I will get 6 lines. But I only want to show the latest status...
The date is in BL_PROJECT_TO_STATUS.STATUS_DATE (I've inserted a date criteria beneath, just to make it more visible).
How can this be done? I'm very new to ODBC and would appreciate it a lot.
SELECT DISTINCT
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL,
MEDIATYPE.DESCRIPTION, BL_PROJECT_STATUS.DESCRIPTION
FROM
FIRMA1.BL_PROJECT BL_PROJECT, FIRMA1.BL_PROJECT_STATUS BL_PROJECT_STATUS,
FIRMA1.BL_PROJECT_TO_STATUS BL_PROJECT_TO_STATUS, FIRMA1.MEDIATYPE MEDIATYPE
WHERE
BL_PROJECT.PROJECT_ID = BL_PROJECT_TO_STATUS.PROJECT_ID AND
BL_PROJECT_TO_STATUS.STATUS_ID = BL_PROJECT_STATUS.CODE AND
BL_PROJECT.MEDIATYPE = MEDIATYPE.ID AND
((BL_PROJECT.PROJECT_TYPE = 2) AND
(BL_PROJECT.EXP_PUB_DATE Between SYSDATE AND (SYSDATE+90)) AND
(BL_PROJECT_TO_STATUS.STATUS_DATE = {ts '2013-11-20 00:00:00'}))
ORDER BY
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL
Here is the general idea. You can adapt it with your table and field names.
select somefields
from sometables
join
(select something, max(datetimefield) maxdt
from table1
where whatever
group by something ) temp on table1.datetimefield = maxdt
etc