Search code examples
powershellcmd

Cannot find file path error for dir /b - Copying all file names script


I am trying to use a script that I've used before in Powershell, that is supposed to copy and paste all the file names inside a folder, into a text file.

I was able to use this script on a different computer last week, but can't do it now on my laptop.

Is there something I am not aware of?

The error it's giving is

Cannot find path 'C:\b' because it does not exist.

I tried removing the /b and I got a list of all the files but with other extra data like mode, last time write, length, name, and the extension of each file.

I really need the plain file name only. How can I do this? Thanks in advance!


Solution

  • the /b flag only works in cmd, not PowerShell

    So you could try

    cmd
    dir /b
    exit
    

    Which will open CMD in your terminal and execute dir /b as a normal cmd command rather than PowerShell

    Alternatively, just use PowerShell's Get-ChildItem (or gci for short)

    To get just the plain file names using Get-ChildItem, you can do something like this:

    # Assign the folder items to a variable $x
    $x = gci
    # Get only the names of those items
    $x.name
    

    See SS64 for details on CMD's dir and PowerShell's Get-ChildItem