Execute each file in turn

Good day to all.
I want to take each file from a specified folder and then run a program that will use each file in turn.
in the example, notepad++ opens for me, but there is a problem with names that have a space, and in general it seems to me that the code is not quite correct.
help me please

$path = "C:\temp\*"

# Show all files in the folder
Get-ChildItem -Path $path | Where-Object { 
    !$_.PSIsContainer -and $_.Extension -eq ".xml"
    } | 
Foreach-Object { Start-Process -FilePath "C:\Program Files\Notepad++\notepad++.exe" -ArgumentList $_.FullName }

Your code is actually correct but it can be written a little better I think

$path = "C:\temp\*"
Get-ChildItem -Path $path -Filter '*.xml' -File |  
Foreach-Object { 
    Start-Process -FilePath "C:\Program Files\Notepad++\notepad++.exe" -ArgumentList $_.FullName 
}

The result depends on the program you want to run and on the amount of files you want to process. If the file extension is linked to the proper program you could use Invoke-Item instead of Start-Process.

Thanks Olaf, but the problem with the space in the name is not solved here, I have a file there, for example, which is called “1 (2).xml” and when opened it says that file 1 does not exist, create it? file (2).xml does not exist, create?

Most of the times that’s a problem with the third party tool - not with PowerShell. You could try to wrap the file name in quotes.

$path = "C:\temp\*"
Get-ChildItem -Path $path -Filter '*.xml' -File |  
Foreach-Object { 
    Start-Process -FilePath "C:\Program Files\Notepad++\notepad++.exe" -ArgumentList "$($_.FullName)" 
}

did not help =(( still says that file 1 is missing

If one pair of quotes is not enough you use more

$path = "C:\temp\*"
Get-ChildItem -Path $path -Filter '*.xml' -File |  
Foreach-Object { 
    Start-Process -FilePath "C:\Program Files\Notepad++\notepad++.exe" -ArgumentList """$($_.FullName)""" 
}
1 Like

hahaha, this works great, thanks