ibgadmin
asked on
Remove rows from csv file based on date column
I have a csv file with columns below. The second column is the only field that is an actual date. What we would like to do is remove all rows from the csv file where the date in that column is < 3 days from today whenever run. Don't have any special preference, but figured some type of bat file utilizing DOS commands, or VB script, powershell, etc. We've used things like 'FINDSTR' etc in the past for certain strings, but not a date so not sure where to start. Any help would greatly be appreciated.
Columns:
PackageReference1,Shipment Informatio nCollectio ndate,Ship mentInform ationActua lWeight,Sh ipmentInfo rmationLea dTrackingN umber,Ship mentInform ationServi ceType,Thi rdPartyUPS AccountNum ber
Columns:
PackageReference1,Shipment
Try
Sub Macro1()
Range("A2").AutoFilter
With ActiveSheet.AutoFilter.Ran ge
.AutoFilter Field:=2, Criteria1:=">" & Date - 3
Application.DisplayAlerts = False
On Error Resume Next
.Offset(1).Resize(.Rows.Co unt - 1).SpecialCells(xlCellType Visible).D elete
On Error GoTo 0
Application.DisplayAlerts = False
End With
Range("A2").AutoFilter
End Sub
Sub Macro1()
Range("A2").AutoFilter
With ActiveSheet.AutoFilter.Ran
.AutoFilter Field:=2, Criteria1:=">" & Date - 3
Application.DisplayAlerts = False
On Error Resume Next
.Offset(1).Resize(.Rows.Co
On Error GoTo 0
Application.DisplayAlerts = False
End With
Range("A2").AutoFilter
End Sub
ASKER
hello - is this vbs or powershell, etc.?
ASKER
I see now this looks to be MS Excel macro. How would I invoke that w/o actually running Excel? I need to automate this process through a bat file, etc. to read the same input file every day that deletes the records.
If I may jump in:
Yes, the code is visual basic, and can be run in Excel in seconds. If you double click on the file in Windows, it will open in Excel. you can assign the macro to a keystroke or a button, then let it fly. Click on save, and keep the same CSV format. Depending on size, I see this taking less than a minute or two in total.
Yes, the code is visual basic, and can be run in Excel in seconds. If you double click on the file in Windows, it will open in Excel. you can assign the macro to a keystroke or a button, then let it fly. Click on save, and keep the same CSV format. Depending on size, I see this taking less than a minute or two in total.
ASKER
I need this to run fully unattended like through a bat file of sorts. Is there some way to automate the input filename and code to run?
Try this macro. I have not tested it.
Sub Macro1()
dim fnam as string
dim fpath as string
dim wb as workbook
set fpath = "C:\............" 'Put something here
fnam= dir(fpath & "\*.csv")
do while fnam<>""
set wb = workbooks.open(fpath & "\" & fnam)
Range("A2").AutoFilter
With ActiveSheet.AutoFilter.Range
.AutoFilter Field:=2, Criteria1:=">" & Date - 3
Application.DisplayAlerts = False
On Error Resume Next
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Delete
On Error GoTo 0
Application.DisplayAlerts = False
End With
Range("A2").AutoFilter
wb.close true
fnam=dir
loop
End Sub
How is the csv file getting generated in the first place? Is it some sort of query extract?
If so can the query not be adjusted to not extract those dates so the csv file gets created with only the dates required.
Can you clarify what you mean by dates < 3 days from today?
Today + 3 days? Keep or lose.
Today - 3 days? Keep or lose.
Thanks
Rob H
If so can the query not be adjusted to not extract those dates so the csv file gets created with only the dates required.
Can you clarify what you mean by dates < 3 days from today?
Today + 3 days? Keep or lose.
Today - 3 days? Keep or lose.
Thanks
Rob H
ASKER
I get the error below...
C:\Users\jdunn\Desktop\Tes tFolder>RE M ************************** ********** ******
************************** *********
C:\Users\jdunn\Desktop\Tes tFolder>RE M * remove lt 3 da
ys *
C:\Users\jdunn\Desktop\Tes tFolder>RE M *
*
C:\Users\jdunn\Desktop\Tes tFolder>RE M ************************** ********** ******
************************** *********
C:\Users\jdunn\Desktop\Tes tFolder>cs cript Lt3days.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Users\jdunn\Desktop\Tes tFolder\Lt 3days.vbs( 2, 10) Microsoft VBScript compilat
ion error: Expected end of statement
C:\Users\jdunn\Desktop\Tes tFolder>PA USE
Press any key to continue . . .
C:\Users\jdunn\Desktop\Tes
**************************
C:\Users\jdunn\Desktop\Tes
ys *
C:\Users\jdunn\Desktop\Tes
*
C:\Users\jdunn\Desktop\Tes
**************************
C:\Users\jdunn\Desktop\Tes
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Users\jdunn\Desktop\Tes
ion error: Expected end of statement
C:\Users\jdunn\Desktop\Tes
Press any key to continue . . .
ASKER
the file is like a log file. i don't extract the records. i would like to lose all records lt todays date. hope that helps.
I do not know how and where you have got those errors. What I have provided is a VBA macro which you have to run from within excel. To run the macro
Open a new file in excel
Right-click on the sheet tab name
Select View code
From the menu Insert > Module
Paste the given code in the VBA window
Close the VBA window
Press Alt-F8
Select the macro name
Click on run
Open a new file in excel
Right-click on the sheet tab name
Select View code
From the menu Insert > Module
Paste the given code in the VBA window
Close the VBA window
Press Alt-F8
Select the macro name
Click on run
ASKER
an excel macro will not work as it will need to run unattended from a windows server. anyone have just vbs or powershell, etc. that could be used?
In that you need to ask this question in some other topic area. This is excel zone.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
PackageReference1,Shipment
"15231622","11/6/2013","1.
"15242251","11/6/2013","1.
"15282587","11/16/2013","1
"15278493","11/18/2013","1