1

I was trying to use dir command to list recursively all files that end with .cpp in a given directory, I tried to follow various solutions but my powershell seems not to accept any options after '/' sign as seen on the picture bellow:

Example

The command I initially tried was 'dir sourcefolder "*.cpp"' but it only lists files in a given folder (because I cant provide any additional options as seen in microsoft doc), also any example command provided there does not work for me giving the same error as shown in example above.

5
  • 1
    dir isn't the same as it is in cmd.exe. In PowerShell, it's an alias for Get-ChildItem where the parameters begin with a dash (-); this doesn't apply to positional parameters but is needed for switch parameters (unless it's being "splatted"). So, it would be: dir sourcefolder "*.cpp" -Recurse. You can also run Get-Help dir for more information about the cmdlet. Commented Nov 30, 2022 at 16:36
  • 1
    it works, thank you. Why would they make cmd commands not work in powershell thats very questionable
    – Goh
    Commented Nov 30, 2022 at 16:46
  • As mentioned above, it's an alias. You still can call on cmd.exe to run your command using cmd.exe /c 'dir ...' from within PowerShell but there's really no point. Different shell, different syntax. Just includes aliases that are common across different shells for an easier transition into PowerShell with the major difference being it's an OO shell. Commented Nov 30, 2022 at 16:46
  • Rather than link an image, please copy and paste the text of the command and the output. Commented Nov 30, 2022 at 16:58
  • "Why would they make cmd commands not work in powershell thats very questionable" because the PowerShell cmdlets are much more powerful and optimized for PowerShell as e.g. they let you stream objects rather than text.
    – iRon
    Commented Nov 30, 2022 at 17:27

1 Answer 1

0

here is how I will bring out all the files in .cpp. Here is a small program in powershell :

$path = "C:\temp\"
$filter = "*.cpp"

$files = Get-ChildItem -Path $path -Filter $filter

Write-Host "here, all the .cpp files in '$path' :"
Write-Host $files -Separator "`r`n"

I prefer to use the cmdlet "Get-ChildItem" rather than "dir".

Here the content folder for my test

And, why so many / ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.