4

I am currently working on a fairly large powershell script. However, I got stuck at one part. The issue is the following.

I have various reports with the same file name, they just have a different time stamp at the end. Within the report, I have a field displaying the date from when to when the report is from.

---> 2/1/2015 5:00:00AM to 3/1/2015 5:00:00AM <--- This is what it looks like.

This field is randomly placed on the Excel Sheet. Pretty much in the range of A5 to Z16. What I would like the script to do is:

Read the file / Check the range of cells for the dates, if the date is found and it matches my search criteria, close the sheet and move it to a different folder / If date does not match, close and check next XLS file

This is what I got so far:

$File = "C:\test.XLS" 
$SheetName = "Sheet1"
# Setup Excel, open $File and set the the first worksheet
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Workbook = $Excel.workbooks.open($file)
$Worksheets = $Workbooks.worksheets
$WorkSheet = $WorkBook.sheets.item($SheetName)
$SearchString = "AM" #just for test purposes since it is in every report
$Range = $Worksheet.Range("A1:Z1").EntireColumn
$Search = $Range.find($SearchString)
3
  • Welcome to SO. Is there anything you have tried up until this point that we can build of?. Components of this question surely have been covered else where. Basic search for example can be found daniellange.wordpress.com/2009/12/18/…. Try a few things and let us know specifically where you get stuck and how we can help. Also is that text ALWAYS the same or does it change. Commented May 12, 2015 at 21:19
  • I know to do this you will have to have the same bit version of Excel and Powershell installed, and you will need to have the following, Microsoft Access Database Engine 2010 Redistributable, microsoft.com/en-us/download/details.aspx?id=13255, and then I believe you can call it just like any other file and search it Commented May 12, 2015 at 21:30
  • Yes the text is always going to be the same and the reports are always the same as well. Basically, what is happening is one report has yestedays date, the other one from the first of last month until the current month, and the last one is from the beginning of the year until the beginning of the current month. I have already figured out how to identify them, I just can't get the search function to work and don't know how to initiate a move command. So in other words, I don't know how to find something and if found, move file to destination location. Commented May 12, 2015 at 21:33

1 Answer 1

6

If you want it to search the entire column for A to Z you would specify the range:

$Range = $Worksheet.Range("A:Z")

Then you should be able to execute a $Range.Find($SearchText) and if the text is found it will spit back the first cell it finds it in, otherwise it returns nothing. So start Excel like you did, then do a ForEach loop, and inside that open a workbook, search for your text, if it is found close it, move it, stop the loop. If it is not found close the workbook, and move to the next file. The following worked just fine for me:

$Destination = 'C:\Temp\Backup'
$SearchText = '3/23/2015  10:12:19 AM'
$Excel = New-Object -ComObject Excel.Application

$Files = Get-ChildItem "$env:USERPROFILE\Documents\*.xlsx" | Select -Expand FullName
$counter = 1
ForEach($File in $Files){
    Write-Progress -Activity "Checking: $file" -Status "File $counter of $($files.count)" -PercentComplete ($counter*100/$files.count)
    $Workbook = $Excel.Workbooks.Open($File)
    If($Workbook.Sheets.Item(1).Range("A:Z").Find($SearchText)){
        $Workbook.Close($false)
        Move-Item -Path $File -Destination $Destination
        "Moved $file to $destination"
        break
    }
    $workbook.close($false)
    $counter++
}

I even got ambitious enough to add a progress bar in there so you can see how many files it has to potentially look at, how many it's done, and what file it's looking at right then.

Now this does all assume that you know exactly what the string is going to be (at least a partial) in that cell. If you're wrong, then it doesn't work. Checking for ambiguous things takes much longer, since you can't use Excel's matching function and have to have PowerShell check each cell in the range one at a time.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thanks a lot. It is actually moving the file to a different location. Now to top it of, how would I look for a combination of words within an excel document? Lets say it still scans the same way as above in the code, just move the file when a certain date "1/1/2015" and the word "financial" is in the document. Both words are not in the same cell and not next to each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.