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
Solved

Looking for a Script

Posted on 2014-01-30
10
263 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

829 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