Powershell video length calculation - powershell

I have a calculation issue I cannot solve, any help appreciated! I receive video length of files in a more complex loop context using the following code:
$movs ="..\..\MOV"
$dura = New-Object -ComObject Shell.Application
$dura = Get-ChildItem -Path $movs -Recurse -Force | ForEach {
$Folder = $Shell.Namespace($_.DirectoryName)
$File = $Folder.ParseName($_.Name)
$Duration = $Folder.GetDetailsOf($File, 27)
[PSCustomObject]#{
vid-file= $_.Name -replace ".mov",""
duration = $Duration
}
}
Later on I match some IDs to $dura so that the result looks like this:
ID vid-file duration
1 move 00:01:08
1 run 00:01:12
1 fly 00:01:30
1 swim 00:01:08
1 sleep 00:02:20
2 move 00:01:08
2 swim 00:01:08
2 sleep 00:02:20
3 move 00:01:08
3 run 00:01:12
3 fly 00:01:30
3 swim 00:01:08
3 sleep 00:02:20
3 think 00:03:20
Now I need to calculate the starting points for each concatenated video case, i.e. I have to sum up the duration of the video for each part until the current position for every ID context and create a new column with it (every new ID starts at 00:00:00). The result would look like this:
ID vid-file duration videopart-start-at
1 move 00:01:08 00:00:00
1 run 00:01:12 00:01:08
1 fly 00:01:30 00:02:20
1 swim 00:01:08 00:03:50
1 sleep 00:02:20 00:04:58
2 move 00:01:08 00:00:00
2 swim 00:01:08 00:01:08
2 sleep 00:02:20 00:02:16
3 move 00:01:08 00:00:00
3 run 00:01:12 00:01:08
3 fly 00:01:30 00:02:20
3 swim 00:01:08 00:03:50
3 sleep 00:02:20 00:04:58
3 think 00:03:20 00:07:18
I think there could be some calculated object in the PSCustomObject but I can't figure it out..
[PSCustomObject]#{
vid-file= $_.Name -replace ".mov",""
duration = $Duration
videopart-start-at= $Duration | Measure-Object -Sum $Duration
}
Thanks, Daniel

I would think that there's an easier way of handling this - but I converted the time into seconds - then worked on the [TimeSpan] datatype.
$movs = 'c:\temp\sample' | Get-ChildItem -Recurse -Force -ErrorAction Stop
$dura = New-Object -ComObject Shell.Application
$result = Foreach ($mov in $movs) {
$Folder = $dura.Namespace($mov.DirectoryName)
$File = $Folder.ParseName($mov.Name)
$Duration = $Folder.GetDetailsOf($File, 27)
[PSCustomObject]#{
vidfile = $mov.Name -replace ".mov", ""
# Convert the string into an actual time data type
duration = $Duration
durationinseconds = ([TimeSpan]::Parse($Duration)).TotalSeconds
}
}
$i = 0
Foreach ($object in $result) {
# Skipping first and stopping on last (foreach will run out of objects to process)
if ($i -eq 0 -or $i -gt ($result.count)) {
# Adding one to counter
$i++
continue
}
$object.durationinseconds = $Object.durationinseconds + $result.durationinseconds[$i - 1]
$object.duration = [timespan]::fromseconds($object.durationinseconds)
("{0:hh\:mm\:ss}" -f $object.duration)
$i++
}

Thanks to Sebastian I found the following solution (I added the "startat" column in the pscustomobject, identical to durationinseconds):
$i = 0
Foreach ($object in $result) {
# skip lines gt result count
if ($i -gt ($result.count)) {
$i++
continue
}
# set start to 0 for first line
if ($i -eq 0) {
$object.startat = 0
}
# calculate start time for all following lines
if ($i -gt 0) {
$object.startat = $result.durationinseconds[$i - 1] + $result.startat[$i - 1]
}
# transform seconds to time value in duration var
$object.duration = [timespan]::fromseconds($object.startat)
# counter +1
$i++
}
$result

To calculate date/time differences, try something like this...
$current = Get-Date
$end = (Get-Date).AddHours(1)
$diff = New-TimeSpan -Start $current -End $end
"The time difference is: $diff"
# Results
<#
The time difference is: 01:00:00.0019997
#>
... then format as you need to.

Related

Efficient way to find and replace many strings in a large text file

The Text file contains a software output on a time domain analysis. 10800 seconds simulation and 50 nodes being considered. We have 540,000 strings to be replaced in 540 MB text file with 4.5 million lines.
Which is currently projected to take more than 4 days. Something is going wrong. Don't know what. Please suggest me a better efficient approach.
Below is the function which does the find and replace.
To replace the string the script goes through the original text file line by line at the same time it generates a duplicate file with replaced strings. So another 540 MB file with 4.5 million lines will be generated at the end of the script.
Function ReplaceStringsInTextFile
{
$OutputfilebyLine = New-Object -typename System.IO.StreamReader $inputFilePathFull
$uPreviousValue = 0
$time = 60
$u = 0; $LastStringWithoutFindResult = 0
$lineNumber = 0
while ($null -ne ($line = $OutputfilebyLine.ReadLine())) {
$lineNumber = $lineNumber + 1
if ($time -le $SimulationTimeSeconds) # time simulation start and end checks
{
# 10800 strings corresponds to one node
# there are 50 nodes.. Thus 540,000 values
# $StringsToFindFileContent contains strings to find 540,000 strings
# $StringsToReplaceFileContent contains strings to replace 540,000 strings
$StringToFindLineSplit = -split $StringsToFindFileContent[$time-60]
$StringToReplaceLineSplit = -split $StringsToReplaceFileContent[$time-60]
if($u -le $NumberofNodes-1)
{
$theNode = $Nodes_Ar[$u]
$StringToFindvalue = $StringToFindLineSplit[$u]
$StringToReplacevalue = $StringToReplaceLineSplit[$u]
if (($line -match $theNode) -And ($line -match $StringToFindvalue)){
$replacedLine = $line.replace($StringToFindvalue,$StringToReplacevalue)
add-content -path $WriteOutputfilePathFull -value "$replacedLine"
$uPreviousValue = $u
$checkLineMatched = 1
if (($line -match $LastNodeInArray)) {
$time = $time + 1
$LastStringWithoutFindResult = 0
}
} elseIf (($line -match $LastNodeInArray) -And ($checkLineMatched -eq 0)) {
$LastStringWithoutFindResult = $LastStringWithoutFindResult + 1
} else {
#"Printing lines without match"
add-content -path $WriteOutputfilePathFull -value "$line"
$checkLineMatched = 0
}
}
if ($checkLineMatched -eq 1) {
# incrementing the value of node index to next one in case the last node is found
$u = $uPreviousValue + 1
if ($u -eq $Nodes_Ar.count) {
$u = 0
$timeElapsed = (get-date -displayhint time) - $startTime
"$($timeElapsed.Hours) Hours $($timeElapsed.Minutes) Minutes $($timeElapsed.Seconds) Seconds"
}
}
}
# Checking if the search has failed for more than three cycles
if ($LastStringWithoutFindResult -ge 5) { # showing error dialog in case of search error
[System.Windows.Forms.MessageBox]::Show("StringToFind Search Fail. Please correct StringToFind values. Aborting now" , "Status" , 0)
$OutputfilebyLine.close()
}
}
$OutputfilebyLine.close()
}
The above function is the last part of the script. Which is taking the most time.
I had run the script in under 10 hours 1 year ago.
Update The script sped up running after 4 hours and suddenly time to complete projection reduced from 4 days to under 3 hours. The script finished running in 7 hours and 9 minutes. However i am not sure what made the sudden change in speed other than asking the question on stack overflow :)
As per the suggestion by https://stackoverflow.com/users/478656/tessellatingheckler
I have avoided writing one line at a time using
add-content -path $WriteOutputfilePathFull -value "$replacedLine"
Instead i am now writing ten thousand lines at a time using add-content
$tenThousandLines = $tenThousandLines + "`n" + $replacedLine
And at the appropriate time I am using add-content to write 10,000 lines at one go like below. The if block follows my methods logic
if ($lineNumber/10000 -gt $tenThousandCounter){
clear-host
add-content -path $WriteOffpipeOutputfilePathFull -value "$tenThousandLines"
$tenThousandLines = ""
$tenThousandCounter = $tenThousandCounter + 1
}
I have encountered system out of memmory exception error when trying to add 15,000 or 25,000 lines at a time. After using this the time required for the operation has reduced from 7 hours to 5 hours. And at another time to 2 hours and 36 minutes.

Powershell script exits ForEach-Object loop prematurely [duplicate]

This question already has answers here:
Why does 'continue' behave like 'break' in a Foreach-Object?
(4 answers)
Closed 5 years ago.
So I've been writing a script that will take all of the data that is stored in 238 spreadsheets and copy it into a master sheet, as well as 9 high level report sheets. I'm really not sure why, but after a specific document, the script ends prematurely without any errors being posted. It's very strange. I'll post some anonymized code below so maybe someone can help me find the error of my ways here.
As far as I can tell, the document that it exits after is fine. I don't see any data errors in it, and the info is copied successfully to the master document before powershell just calls it quits on the script completely.
I've tried changing the size of the data set by limiting only to the folder that contains the problem file. It still ends after the same file with no error output. I cannot upload the file due to company policy, but I really don't see anything different about the data on that one file when compared to any other file of the same nature.
Also, apologies in advance for the crappy code. I'm not a developer and have been relearning powershell since it's the only tool available to me right now.
$StartTime = Get-Date -Format g
Write-Host $StartTime
pushd "Z:\Shared Documents\IO"
$TrackTemplate = "C:\Users\USERNAME\Desktop\IODATA\MasterTemplate.xlsx"
# Initialize the Master Spreadsheet
$xlMaster = New-Object -ComObject Excel.Application
$xlMaster.Visible = $False
$xlMaster.DisplayAlerts = $False
$MasterFilePath = "C:\Users\USERNAME\Desktop\IODATA\Master.xlsx"
Copy-Item $TrackTemplate $MasterFilePath
$wbMaster = $xlMaster.Workbooks.Open($MasterFilePath)
$wsMaster = $wbMaster.Worksheets.Item(2)
$wsMaster.Unprotect("PASSWORD")
$wsMasterRow = 3
# Initialize L4 Document Object
$xlL4 = New-Object -ComObject Excel.Application
$xlL4.Visible = $False
$xlL4.DisplayAlerts = $False
# Initialize object for input documents
$xlInput = New-Object -ComObject Excel.Application
$xlInput.Visible = $False
$xlInput.DisplayAlerts = $False
# Arrays used to create folder path names
$ArrayRoot = #("FOLDER1","FOLDER2","FOLDER3","FOLDER4","FOLDER5","FOLDER6","FOLDER7","FOLDER8","FOLDER9")
$ArrayShort = #("SUB1","SUB2","SUB3","SUB4","SUB5","SUB6","SUB7","SUB8","SUB9")
# $counter is used to iterate inside the loop over the short name array.
$counter = 0
$FileNumber = 0
$TotalFiles = 238
$ArrayRoot | ForEach-Object {
$FilePathL4 = "C:\Users\USERNAME\Desktop\IODATA\ROLLUP\" + $ArrayShort[$counter] + "_DOC_ROLLUP.xlsx"
Copy-Item $TrackTemplate $FilePathL4
$wbL4 = $xlL4.Workbooks.Open($FilePathL4)
$wsL4 = $wbL4.Worksheets.Item(2)
$wsL4.Unprotect("PASSWORD")
$wsL4Row = 3
If ($ArrayShort[$counter] -eq "SUB7") {$FilePath = "Z:\Shared Documents\IO\" + $_ + "\" + $ArrayShort[$counter] + " - DOC v2\"}
Else {$FilePath = "Z:\Shared Documents\IO\" + $_ + "\!" + $ArrayShort[$counter] + " - DOC v2\"}
Get-ChildItem -Path $FilePath | ForEach-Object {
If ($_.Name -eq "SPECIFIC_DOC.xlsx") {Continue}
$FileNumber += 1
Write-Host "$FileNumber / $TotalFiles $_"
$wbInput = $xlInput.Workbooks.Open($_.FullName)
$wsInput = $wbInput.Worksheets.Item(2)
$wsInputLastRow = 0
#Find the last row in the Input document
For ($i = 3; $i -le 10000; $i++) {
If ([string]::IsNullOrEmpty($wsInput.Cells.Item($i,1).Value2)) {
$wsInputLastRow = $i - 1
Break
}
Else { Continue }
}
[void]$wsInput.Range("A3:AC$wsInputLastRow").Copy()
Start-Sleep -Seconds 1
[void]$wsMaster.Range("A$wsMasterRow").PasteSpecial(-4163)
Start-Sleep -Seconds 1
$wsMasterRow += $wsInputLastRow - 2
[void]$wsL4.Range("A$wsL4Row").PasteSpecial(-4163)
Start-Sleep -Seconds 1
$wsL4Row += $wsInputLastRow - 2
$wbInput.Close()
$wbMaster.Save()
}
$counter += 1
$wsL4.Protect("PASSWORD")
$wbL4.Save()
$wbL4.Close()
}
$wsMaster.Protect("PASSWORD")
$wbMaster.Save()
$wbMaster.Close()
$xlMaster.Quit()
$EndTime = Get-Date -Format g
$TimeTotal = New-Timespan -Start $StartTime -End $EndTime
Write-Host $TimeTotal
To continue pipeline processing with the next input object, use return - not continue - in the script block passed to the ForEach-Object cmdlet.
The following simple example skips the 1st object output by Get-ChildItem and passes the remaining ones through:
$i = 0; Get-ChildItem | ForEach-Object{ if ($i++ -eq 0) { return }; $_ }
There is currently (PSv5.1) no direct way to stop the processing of further input objects - for workarounds, see this answer of mine.
By contrast, as you've discovered, break and continue only work as expected in the script block of a for / foreach statement, not directly in the script block passed to the ForeEach-Object cmdlet:
For instance, the following produces no output (using break would have the same effect):
$i = 0; Get-ChildItem | ForEach-Object{ if ($i++ -eq 0) { continue }; $_ }
The reason is that continue and break look for an enclosing for / foreach statement to continue / break out of, and since there is none, the entire command is exited; in a script, the entire script is exited if there's no enclosing for / foreach / switch statement on the call stack.

Random time selection logic is not working and loops forever

My input is a text file with license plate and date for tire repair. I am then prompted to write a time e.g like 12:30. Then the script checks if the time is good or not. It's good if there is 30 minutes after and before not being used.
Using the example won't work since the 12:00-13:00 block contains an entry. Then the time isn't good, so the script has to search a good date for me. I tried to check line by line minute per minute. After, if the $help variable didn't change then it's good, but if it did, then make a random time, to check. Any ideas why that logic is not working?
Edit: Since I edited, the only thing I saw that isn't working, is that my script generates a random time, but it didn't check if that time is right, just accept that time and goodbye. Somehow I should make it to check it more times, and don't stop at the first generated time, check it, and if its still wrong, then randomize it again.
Sample data
ABC-145 8:30
BDE-540 9:45
EDS-598 10:30
SDF-478 11:30
HUT-434 12:15
JEC-238 13:15
ASD-325 14:00
VRA-123 16:15
HGV-456 18:00
$file = Get-Content Desktop/database.txt
[datetime]$time = Read-Host "Time for date?"
$start = $time
$check = $time
$help = 0
echo $time
foreach ($line in $file) {
#here I check if the date is free
$out = $line.Split()
while ($true) {
#until I found a good time
for ($i=0; $i -lt 30; $i++) {
#check the next half hour if good
if ($time -eq $out[1]) {
$help = 1
}
$time = $time.AddMinutes(1)
}
for ($i=0; $i -lt 60; $i++) {
#back check the half hour
if ($time -eq $out[1]) {
$help = 1
}
$time = $time.AddMinutes(-1)
}
$time = $time.AddMinutes(30)
if ($help -eq 0) {
break
} else {
$hour = Get-Random -Minimum 8 -Maximum 20
$minute = Get-Random -Minimum 0 -Maximum 59
$start = "$ora`:$perc"
$time = $start
$help = 0
#echo $time
}
}
}
if ($start-ne $check) {
echo "There wasnt a free date, but there is a free spot at " + $start
} else {
echo "This is free date"
}
I would recommend a different approach. First extract the times from your input file and convert them to DateTime values:
$times = Get-Content 'Desktop/database.txt' |
ForEach-Object { $_.Split("`t")[1] | Get-Date }
From that list determine the intervals that are actually wide enough to fit another appointment (at least 30 min after the previous and before the next appointment respectively, hence a minimum time window of 60 minutes):
$available = 0..($times.Count-1) |
Where-Object { ($times[$_+1]-$times[$_]).TotalMinutes -ge 60 }
Pick a random index from that list and add 30 minutes to the corresponding time:
$index = $available[(Get-Random -Maximum $available.Count)]
$start = $times[$index].AddMinutes(30)
'{0:HH:mm}' -f $start
Or, if you want some more variation, calculate a new starting time from the timeframe 30 minutes after the previous appointment to 30 minutes before the next appointment like this:
$index = $available[(Get-Random -Maxmimum $available.Count)]
$delta = ($times[$index+1]-$times[$index]).TotalMinutes - 60
$offset = 30 + [int](Get-Random -Maximum ($delta + 1))
$start = $times[$index].AddMinutes($offset)
'{0:HH:mm}' -f $start
This approach ensures that you don't run into an infinite loop when there are no available timeslots left.

if/elseif not working right

I have a script that checks how much memory the top w3wp process is using. Then depending on how close to 1 gig it is using, it will give out a message.
param($warn, $crit)
$LastExitCode = 3
$Output1 = ""
$output = ""
$myW3WP = Get-Process w3wp | Sort -Descending WS | select -first 1 |
Measure-Object WS -Sum
$myW3WP = ("{0:N2} " -f ($myW3WP.sum / 1mb))
$myW3WP.trim()
if ($myW3WP -gt $crit) {
$LastExitCode = 2
$output1 = "CRITICAL: Memory for W3WP has passed 1 gig $myW3WP"
$output = $output1
} elseif ($myW3WP -gt $warn) {
$LastExitCode = 1
$output1 = "WARN: Memory for W3WP is getting close to 1 gig $myW3WP"
$output = $output1
} else {
$LastExitCode = 0
}
$output = $output1
Write-Host $output $LastExitCode "$myW3WP >= " $crit ($myW3WP -gt $crit) "$myW3WP >= " $warn ($myW3WP -gt $warn)
$myW3WP = ""
In values for $crit is 1000 and the value for $warn is 900.
(Note: the Write-Host line is like that for troubleshooting)
Here is the output I am getting:
CRITICAL: Memory for W3WP has passed 1 gig 161.03 2 161.03 >= 1000 True 161.03 >= 900 False
The 161.03 is the megs of RAM being used.
Any ideas on why it is saying 161.03 is greater than 1000?
Don't compare apples and oranges!
You'll need a numerical value if you want to compare it to other numerical values
When you call:
$myW3WP = ("{0:N2} " -f ($myW3WP.sum / 1mb))
$myW3WP.trim()
you create a string.
Replace those two lines with just:
$myW3WP = $myW3WP.Sum / 1MB
And it'll work
$myW3WP is a string, not a number, so you're doing a textual comparison, rather than a numeric one. The line:
$myW3WP = ("{0:N2} " -f ($myW3WP.sum / 1mb))
Is what forces the variable to be a string. Just leave it as a number and don't worry about the decimal places:
$myW3WP = $myW3WP.sum / 1mb

Powershell adding numbers in a loop

I have the below code which is meant to total up the time offset as the loop rotates (I will then need to divide this by 10 to get the average but first I need to get this bit working).
I'm assuming I need to cast something as [INT] but I've tried multiple locations that would make sense to no avail. I just end up with O's.
$winTimeStripchart = w32tm /stripchart /computer:0.pool.ntp.org /dataonly /samples:10
$WinTimeOffset = $null
For($i=3; $i -le 12; $i++){
$Offset = $winTimeStripchart[$i].split("-")
$trimmedOffset = $Offset[1].trim("s")
$winTimeOffset = $winTimeOffset + $trimmedOffset
}
Write-Host "Total: $winTimeOffset"
# Now need to divide by 10.
sample data:
20:30:23, -00.0698082s
20:30:25, -00.0704645s
20:30:27, -00.0708694s
20:30:29, -00.0728990s
20:30:31, -00.0719226s
20:30:33, -00.0749031s
20:30:36, -00.0778656s
20:30:38, -00.0782183s
20:30:40, -00.0752974s
20:30:42, -00.0760958s
You can try this one line command :
$a = w32tm /stripchart /computer:0.fr.pool.ntp.org /dataonly /samples:10 | select -Skip 3 | % {[double]$_.Substring(11,10)} | Measure-Object -Average -sum
$a can also give you maximum and minimum adding Measure-Object params.
You'll want to cast to a double rather than int. The following should do it:
[System.Collections.ArrayList]$winTimeStripchart = w32tm /stripchart /computer:0.pool.ntp.org /dataonly /samples:10
$WinTimeOffset = $null
$winTimeStripchart.RemoveRange(0,3)
foreach($entry in $winTimeStripchart){
$Offset = $entry.split("-")
$trimmedOffset = $Offset[1].trim("s")
$winTimeOffset = [double]$winTimeOffset + [double]$trimmedOffset
}
Write-Host "Total: $winTimeOffset"
Write-Host "Average: $($winTimeOffset/$winTimeStripchart.count)"
Sample output:
Total: 6.1581437
Average: 0.61581437
I've make some other tweaks to the script as well to make it more scalable:
Foreach rather than a for loop
Using a list rather than array and stripping first 3 entries.
Dividing buy number of entries in list.
regards
Arcas