Inset Delay (sec) in for loop

Hi,

I have a powershell script where I am using a for loop. I am reading a csv file and for each row, I am running a command (cawto). Is there a way, I can insert a delay (similar to sleep) for a few seconds (5), before it reads the next row on the csv file.

$VPSFile = Import-Csv 'D:\TRHMIBS\VPS\RTN\newfile.csv' 
$Results = @()
foreach ($P in $VPSFile)
{

$PL = $P.PLA_; #
$CT = $P.LAN; #
$LN = $P.LA; #Count
$PL = $P.HASRR; 
$CT = $P.TOTAL_UFM; 
$LN = $P.RRPCT; 

cawto VPSRTNUFM ('{0} {1} {2} {3} {4} {5}' -f $P.PLAZA_, $P.LANE_ID, $P.LA, $P.HASRR, $P.TOTAL_UFM, $P.RRPCT)
}

September 2nd, 2015 8:21am

Hi Mike,

Is there a way, I can update the for loop, to pause before it reads the next row?

Thanks

September 2nd, 2015 8:52am

You could try using a ForEach-Object loop instead for a foreach loop.
Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 8:54am

You could try using a ForEach-Object loop instead for a for
September 2nd, 2015 1:59pm

Import-Csv in a pipeline does not import the whole file first.  It processes one line at a time.  If you place parens around it this it will import the whole thing first.

Import-Csv D:\TRHMIBS\VPS\RTN\newfile.csv |
    Foreach-Object{
        cawto VPSRTNUFM $_.PLAZA_, $_.LANE_ID, $_.LA, $_.HASRR, $_.TOTAL_UFM, $_.RRPCT
        sleep 5
    }

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 2:04pm

Hello,

I just tested this on version 5 (On Windows 8.1)

Import-Csv C:\temp\test.csv  |
    Foreach-Object{
        write-host $_.Name
        sleep 5
}

Test.csv consisted of:

Name
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16
Line 17
..
Line 46

While the loop was processing I deleted lines 18 - 33

PowerShell however kept outputting the data that was no longer in the file.

Karl

September 2nd, 2015 3:06pm

I have duplicated my results on Version 5 Win 10 as well.

Would be cool if this had worked, as added data would be processed.

Maybe I will test that next :)

Karl

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 3:17pm

I have duplicated my results on Version 5 Win 10 as well.

Would be cool if this had worked, as added data would be processed.

Maybe I will test that next :)

So that works - added data is processed.

Karl

September 2nd, 2015 3:21pm

Hello,

I just tested this on version 5 (On Windows 8.1)

Import-Csv C:\temp\test.csv  |
    Foreach-Object{
        write-host $_.Name
        sleep 5
}

Test.csv consisted of:

Name
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16
Line 17
..
Line 46

While the loop was processing I deleted lines 18 - 33

PowerShell however kept outputting the data that was no longer in the file.

Karl

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2015 3:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics