Reputation: 319
This code runs and gives me the file names from the folder but it also throws an error:
Import-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
Not sure what the issue is...
$files = Get-ChildItem "C:\Users\MG\Desktop\ScanFolder\*.csv"
foreach ($item in $files) {
$file_name = $item.name
$check = Import-Csv $_
if ($check) { ...
Upvotes: 0
Views: 1856
Reputation: 175085
$_
is only automatically populated when you pipe (ie. use |
) data from to a cmdlet or function.
Since you're using a foreach()
loop, you should reference the local copy you declare in the parentheses instead, in your case $item
:
foreach($item in $files){
$file_name = $item.name
$check = Import-Csv $item.FullName
# work with $check
}
You could also pipe $item
to Import-Csv
and let PowerShell automatically bind the path of the file to Import-Csv
's LiteralPath
parameter:
foreach($item in $files){
$file_name = $item.name
$check = $item |Import-Csv
# work with $check
}
Note: content below addresses a previous revision of this question:
In PowerShell 4.0 you can use property enumeration on the resulting array, like so:
(Get-ChildItem "C:\Users\MG\Desktop\ScanFolder\*.csv").Name
or use ForEach-Object
:
Get-ChildItem "C:\Users\MG\Desktop\ScanFolder\*.csv" |ForEach-Object Name
If you want to write code compatible with previous versions, use Select-Object
:
Get-ChildItem "C:\Users\MG\Desktop\ScanFolder\*.csv" |Select-Object -ExpandProperty Name
Upvotes: 9