Git Product home page Git Product logo

Comments (2)

rhubarb-geek-nz avatar rhubarb-geek-nz commented on May 20, 2024

Could you do a single pipeline rather than continually opening and closing?

Also, does setting $ErrorActionPreference at the root make any difference?

$destfile = "\\UNCSHARE\Folder\Export_Sample_$(get-date -Format "yyyyMMdd_HHmmss").txt"

$srcFile = '\\UNCSHARE\Folder\200MB_Export_20231208_1545.txt'

[int64]$ln = 0
[int64]$SampleCount = 0

$ErrorActionPreference = 'Stop'

Get-Content -LiteralPath $srcFile | ForEach-Object {
    $SampleCount++
    $ln++
    if ($ln -eq 1 -or $ln % 500 -eq 0)
    {
        $_
    }
} | Set-Content -LiteralPath $destFile

write-host $SampleCount

(get-content $destfile).count

from powershell.

SimonESHT avatar SimonESHT commented on May 20, 2024

Thanks, Yes, I found that to be an effective workaround. See the Stack Overflow thread [here]https://stackoverflow.com/questions/78378535/powershell-out-file-append-silently-skips-a-few-rows-when-used-inside-foreach-l
The point of raising this ticket though was to alert that the Out-File cmdlet is not raising a terminating error when it cannot open the file for write access. The expected behaviour (as seen in PS5.1 with the native API call) is that it should as I illustrated above

Exception calling "AppendAllText" with "2" argument(s): "The process cannot access the file 
'\\UNCSHARE\Export_sample20240424_164810.txt' because it is being used 
by another process."

To be clear, I have found an alternative approach, but the behaviour of Out-File in PS Core, when writing to UNC network stream, does not make it apparent that any error has occurred.

I did try adding $ErrorActionPreference ="STOP" at the root of the script but it still didn't report an error.

Stack Overflow
I am trying to extract a representative sample of a 200MB csv file by writing the header and every 500th row to a new file for testers to use. My first attempt was knowingly sub-optimal but seemed ...

from powershell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.