Link to home
Start Free TrialLog in
Avatar of ibgadmin
ibgadminFlag for United States of America

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,ShipmentInformationCollectiondate,ShipmentInformationActualWeight,ShipmentInformationLeadTrackingNumber,ShipmentInformationServiceType,ThirdPartyUPSAccountNumber
Avatar of ibgadmin
ibgadmin
Flag of United States of America image

ASKER

Forgot to provide a few actual rows of data....

PackageReference1,ShipmentInformationCollectiondate,ShipmentInformationActualWeight,ShipmentInformationLeadTrackingNumber,ShipmentInformationServiceType,ThirdPartyUPSAccountNumber
"15231622","11/6/2013","1.00","581089305388","3","444444444"
"15242251","11/6/2013","1.00","581089305399","92","555555555"
"15282587","11/16/2013","1.00","581089305403","92","555555555"
"15278493","11/18/2013","1.00","581089305414","92","444444444"
Avatar of Saqib Husain
Try

Sub Macro1()
    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
End Sub
hello - is this vbs or powershell, etc.?
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.
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 

Open in new window

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
I get the error below...
C:\Users\jdunn\Desktop\TestFolder>REM ******************************************
***********************************

C:\Users\jdunn\Desktop\TestFolder>REM *                           remove lt 3 da
ys                                   *

C:\Users\jdunn\Desktop\TestFolder>REM *
            *

C:\Users\jdunn\Desktop\TestFolder>REM ******************************************
***********************************

C:\Users\jdunn\Desktop\TestFolder>cscript Lt3days.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Users\jdunn\Desktop\TestFolder\Lt3days.vbs(2, 10) Microsoft VBScript compilat
ion error: Expected end of statement


C:\Users\jdunn\Desktop\TestFolder>PAUSE
Press any key to continue . . .
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
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
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial