3

I have a script which currently pulls the file location from a CSV and uploads the files to a database, using a ForEach-Object loop.

What I'd like it to do is upload 1000 files, then be able to pause the loop and resume it later from file 1001.

I don't want to use the Start-Sleep command, as I do not want the script to automatically resume after a set amount of time.

This is a one time deal, so I'd rather not convert it to a workflow.

What command or cmdlet can be used to accomplish this?


The Read-Host command would be perfect if there were a way to break the script and then resume from the same line later.

5
  • Any chance the files could change between the time you stopped and the time you resumed? Commented Jul 23, 2014 at 0:23
  • have you tried read-host? Commented Jul 23, 2014 at 2:02
  • 1
    It's not clear what you're trying to accomplish. Are you saying that there are 1000 files in the directory and more will be added, or are there more than 1000 files already there and you want to stop after uploading the first thousand and continue later? Also, what exactly does "later" mean, i.e. based on what criteria do you want to resume? You say that you don't want to use Start-Sleep because you don't want to resume after a specific amount of time, but you haven't explained what determines when you want to resume. Based on new files being added to a directory? Manually? Commented Jul 23, 2014 at 2:21
  • mjolinor - The files will not change. Commented Jul 23, 2014 at 17:09
  • @AdiInbar There are more than 5000 files listed in the csv. I'd like to pause the script in between each thousand files, with the option to manually choose whether I resume right away or at a later time. Commented Jul 23, 2014 at 17:14

3 Answers 3

3

Here's how I'd do it:

$i = 0;
foreach ($file in (Get-ChildItem $path_to_directory)) {
  # Code to upload the file referenced by $file
  if (++$i -eq 1000) {
    Write-Host -NoNewLine '1000 files have been uploaded. Press capital "C" to continue uploading the remaining files...'
    do {
    } until (($Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyUp').Character) -ceq 'C')
  }
}

Using pause, as already suggested in Bluecakes' answer, is a perfectly good solution. The advantage of this method is that it gives you more control. pause always requires the Enter key and always gives you the same prompt, Press Enter to continue...:, whereas this way you can define both to your liking.

More significantly, and the reason I personally prefer to do it this way in my own scripts, is that you can protect against accidental key presses. In my example I made the required keystroke a capital C, so that it's very unlikely that you'd continue by accident.

However, if you don't care about any of that and just want a quick and simple say to do it, then pause is really all you need.

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

Comments

3

Use pause:

For ($i=1; $i -lt 2000; $i++)  {

    if ($i -eq 1001)
    {
         pause
    }
    Write-Host $i
}

1 Comment

That's a good solution, definitely the simplest way to do it. I'm going to add a pure PowerShell answer that gives more control, but this works fine.
0

Something along these lines could work for you...

For ($i=1; $i -lt 50; $i++)  {

    if ($i -eq 10)
    {
        $input = Read-Host "Should I continue Y/N"
        if ($input -eq "N")
        {
           break
        }
    }
    Write-Host $i
}

Comments

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.