Solved

Looking for a Script

Posted on 2014-01-30
10
265 Views
Last Modified: 2014-01-30
Greeting Experts,
      I am in need of a script to delete a set of rows in group of excel spread sheets until hits a condition (i.e. such as a keyword). What do I mean….  I receive several attachments once a week from our Antiviruses Management server with the states of daily infections. The excel files come in two parts. One is the list of malware, Trojans, and viruses names (along with no# of detections of each threat) and 2 is a Drill down detail list of each event.
What I am looking to do is dynamically strip out the list of Threats until I get the First cell of the Drill down list in to cell “A1” ( i.e. Event Category highlighted in yellow). None of the excel files have a fixed number of rows for both parts (Meaning the Number of Threat Events could be longer or shorts from one day to the next.  
I am looking for a script to scan theses excel files and strip out the unnecessary data and leave the drill down detailed information starting at Cell “A1” with the keyword of “EventCategory”   . I have attached example spreadsheet that give an example of the files I receive weekly..
ExampleSpreadsheet.xlsx
0
Comment
Question by:amstoots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39822079
Try this:

function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

$objExcel = new-object -comobject excel.application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Open("C:\temp\ExampleSpreadsheet.xlsx")
$objWorksheet = $objWorkbook.Worksheets.Item(1)

$i = 1
Do {
    If ($objWorksheet.Cells.Item($i, 1).Value() -ne "Event Category"  )
      {
            $objRange = $objWorksheet.Cells.Item($i, 1).EntireRow
            $objRange.Delete()
            $i-=1
      }
      $i += 1
}
While ($objWorksheet.Cells.Item($i,1).Value() -ne "Event Category" )

$objExcel.ActiveWorkbook.SaveAs("c:\temp\myfile.xlsx")
$objExcel.Workbooks.Close()
$objExcel.Quit()

Release-Ref($objRange)
Release-Ref($objWorksheet)
Release-Ref($objWorkbook)
Release-Ref($objExcel)
0
 

Author Comment

by:amstoots
ID: 39822124
The Script works perfect .... Is there a way to have it look at the past 7 days and do the same thing on each spreadsheet (in silent mode)... ?>
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39822207
Optimized code: (faster) and silent mode

function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

$objExcel = new-object -comobject excel.application
$objExcel.Visible = $false
$objWorkbook = $objExcel.Workbooks.Open("C:\temp\ExampleSpreadsheet.xlsx")
$objWorksheet = $objWorkbook.Worksheets.Item(1)

$i = 1
$cell=$objWorksheet.Cells.Item($i, 1)

Do {
    If ( $cell.value() -ne "Event Category"  )
      {
            $objRange = $cell.EntireRow
            $objRange.Delete()
            $i-=1
      }
      $i += 1
    $cell=$objWorksheet.Cells.Item($i, 1)
}
While ($cell.value() -ne "Event Category")

$objExcel.ActiveWorkbook.SaveAs("c:\temp\myfile4.xlsx")
$objExcel.Workbooks.Close()
$objExcel.Quit()

Release-Ref($objRange)
Release-Ref($objWorksheet)
Release-Ref($objWorkbook)
Release-Ref($objExcel)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39822221
you mean look the .xlsx files created 7 days ago?
0
 

Author Comment

by:amstoots
ID: 39822237
yes... Need to have it look at example.csv files for the past seven days... based on the creation date (Located  in the properties) of each document ...
0
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39822310
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $false

$Lookupdir="C:\temp"    
$days_backdate=$(Get-Date).AddDays(-7)    
 
$files = Get-Childitem $Lookupdir -Recurse | where-object {!($_.psiscontainer)} | where { $_.Creationtime -gt $days_backdate }
foreach ($file in $files)
{
$objWorkbook = $objExcel.Workbooks.Open($file.Fullname)
$objWorksheet = $objWorkbook.Worksheets.Item(1)

$i = 1
$cell=$objWorksheet.Cells.Item($i, 1)

Do {
    If ( $cell.value() -ne "Event Category"  )
      {
            $objRange = $cell.EntireRow
            $objRange.Delete()
            $i-=1
      }
      $i += 1
    $cell=$objWorksheet.Cells.Item($i, 1)
}
While ($cell.value() -ne "Event Category")

$objExcel.ActiveWorkbook.SaveAs("c:\temp\myfile4.xlsx")
$objExcel.Workbooks.Close()
$objExcel.Quit()
}

Release-Ref($objRange)
Release-Ref($objWorksheet)
Release-Ref($objWorkbook)
Release-Ref($objExcel)
0
 

Author Comment

by:amstoots
ID: 39822410
Is there a way to  save the file as it current file name instead of using "MyFile4.xlsx" file name.. Just remove the unnecessary rows and then save it w/o moving it to new excel document.


$objExcel.ActiveWorkbook.SaveAs("c:\temp\myfile4.xlsx")
$objExcel.Workbooks.Close()
$objExcel.Quit() 

Open in new window

0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39822489
$objExcel.displayalerts=$false
$objExcel.activeworkbook.saveas($file.fullname)
0
 

Author Comment

by:amstoots
ID: 39822831
That did the trick .. thank you....  Raheman Mohammed Abdul
0
 

Author Closing Comment

by:amstoots
ID: 39822832
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question