patternshellMinor
Script to remove .vb files from a directory
Viewed 0 times
scriptdirectoryremovefilesfrom
Problem
I wrote a script to remove .vb code files when there are corresponding .cs files in a certain directory structure. But I felt like there were some extra statements that I had to put in there that didn't feel natural. Is there a better way to do this check and then action?
Specifically, having to do the
Specifically, having to do the
foreach at the end didn't seem right to me. I also didn't know if there was a more PowerShell-y way to do the change extension and test-path.ls . -include *.vb -recurse
| ? { $cs = [System.IO.Path]::ChangeExtension($_.FullName, ".cs"); Test-Path $cs }
| % { rm $_ -force }Solution
You could combine expression vs. command mode in PowerShell, process only files (
!$_.PSIsContainer) and use regex instead of ChangeExtension:gci -include *.vb -Recurse |
? { (!$_.PSIsContainer) -and (Test-Path ($_.FullName -replace "\.vb$", ".cs") } |
% { rm $_ -force }Code Snippets
gci -include *.vb -Recurse |
? { (!$_.PSIsContainer) -and (Test-Path ($_.FullName -replace "\.vb$", ".cs") } |
% { rm $_ -force }Context
StackExchange Code Review Q#14412, answer score: 4
Revisions (0)
No revisions yet.