3 ms·
Indeed, in PowerShell you could do: 1..500 | ?{ !(Test-Path ('{0:0000}_A.csv' -f $_)) } Explanation: 1..500 generates a sequence of numbers 1 through 500
by useerup 8y ago
Indeed, in PowerShell you could do:
1..500 | ?{ !(Test-Path ('{0:0000}_A.csv' -f $_)) }
Explanation:
1..500 generates a sequence of numbers 1 through 500
| pipes the numbers
?{ … } is a filter that is evaluated for each item (number)
! negates the following expression
Test-Path tests that a file exists
-f formats the string left of -f with the parameters (zero-based to the right of -f
'{0:0000}_A.csv' is a pattern which formats parameter 0 as 4 digits, zero-padded.
EDIT: Explanation
- jclay 8y agoGreat example. PowerShell is definitely underrated. I don’t care to use it as an interactive shell, and that likely turns many off to it, but now that it’s cross platform I think it’s massively underrated for these sorts of tasks.
- eMSF 8y agoI don't think that example really shows the benefits of the "Powershell way". There's hardly any need for objects and what-not when solving an easy problem like this; strings will do just fine. With Bash, a close equivalent would be: for f in {0001..0500}_A.csv; do test -e $f || echo $f; done