Comments (2)
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.
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 OverflowI 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)
- fwlink helpURI of Get-Uptime,Get-FileHash and others doesn't work HOT 5
- Unable to Install PowerShell 7.4 with Ubuntu 24.04 HOT 6
- Incorrect Auto-Completion When Using Tab HOT 4
- Mount-DiskImage does not assign driver letter every third time HOT 2
- Does PowerShell support the real "Stream" response? HOT 5
- v7.4.2 not in Ubuntu 22.04 Linux package repository for Microsoft products HOT 3
- `Finally` block isn't executed upon Ctrl+C HOT 36
- Make `ConvertFrom-StringData` more error tolerant HOT 9
- Add-Type ReferencedAssemblies only works with full file paths now? HOT 1
- .Net 8 in PowerShell 7 System.Private.CoreLib fails to register. HOT 41
- `ConvertFrom-StringData` mistakenly emits a statement-terminating error when one of its input strings isn't well-formed HOT 2
- Redirection operator `>` should not be hardcoded to files HOT 6
- Update from System.Data.SqlClient to Microsoft.Data.SqlClient HOT 2
- .NET App using Powershell.SDK fails to run, when building as SingeFile HOT 1
- Add a command which gets the System Lockdown mode information HOT 2
- Overriding .NETs ToString() method can cause unexpected behaviour when using Transcripts HOT 1
- `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly HOT 4
- Invoke-WebRequest needs SupportsShouldProcess
- using `Get-AppxPackage` when `SYSTEMDRIVE` env var is not set creates a (literal) `%SystemDrive%/…` directory HOT 4
- Get-Help shows incorrect spacing and unwanted prefixes for first line of .EXAMPLE text HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.