Windows Command Prompt Dir Command to Save Text file and have no extentions in the data?

0

I have a Dir Command I use to make a text list of a directory of files.. "dir /a /b /-p /o:gen > z-file_list.txt"

Dose anyone know how I can change this so the text list dose not contain any extensions? So just the filename?

Thanks in advance!

  • EDIT -

Sorry I wasn't clear. What I mean is that his command will save a txt file with a file list of the contents of the directory.

The text file will look like this
filename.ext
filename.ext
filename.ext
filename.ext

I am trying to work out how to get the text file to look like this.
filename
filename
filename
filename

Thanks.. sorry I was unclear.

aJynks

Posted 2016-09-24T01:24:01.250

Reputation: 111

Just remove ".txt" from the file name string? – Ramhound – 2016-09-24T02:02:08.630

that is just the file it is writing into, the data is saved in "z-file_list.txt". It dose not effect the dir command. – aJynks – 2016-09-24T02:07:14.280

Your question does not make sense. You asked how to generate a file without an extension. – Ramhound – 2016-09-24T02:13:39.113

oh, sorry. I have edited the question in the hopes of making it more clear. – aJynks – 2016-09-24T03:47:45.557

Answers

3

The dir command does not have a switch or option to drop extensions.

It can be done by processing the output of dir /b in a for /f loop, though.

(del list.txt 2>nul) & for /f %f in ('dir /a /b /o:gen') do @echo %~nf >>list.txt

If you don't care about subdirectories or system/hidden files (which is what dir /a is for) then a plain for would work, too.

(del list.txt 2>nul) & for %f in (*) do @echo %~nf >>list.txt

Run dir /?, del /? and for /? for more details on the syntax and switches.

dxiv

Posted 2016-09-24T01:24:01.250

Reputation: 1 784

0

You can do it from the CMD prompt with a little cheating with Powershell. From the command prompt, type the follwing:

powershell -command "dir |select basename" > file_list.txt

As I said, this is a bit of a cheat, as powershell -command invokes the Powershell shell to run the Powershell script dir |select basename". FYI, dir in Powershell is just an alias for the command get-childitem and has nothing to do with the CMD prompt's dir command. Powerhsell exectues that script and the results are returned to the CMD prompt and redirected (>) to the text file.

Keltari

Posted 2016-09-24T01:24:01.250

Reputation: 57 019