0

I'm looking for a command using cmd.exe (Win 10) that will list all files in a folder and its sub-folders, alphabetically, irrespective of the paths, and that will show the filenames only (no paths).

The commands that I'm familiar with (including, for example, "dir ..\samplefolder /b /s /A-D /o:n > filelist.txt") all include the paths in the output, and so are not what I'm looking for.

Thank you.

quaero
  • 3
  • 3
  • https://stackoverflow.com/questions/25066360/equivalent-to-cut-on-windows – h4z3 Jun 10 '19 at 13:00
  • Can anyone explain what the above link by h4z3 is doing here? – quaero Jun 10 '19 at 14:55
  • Link above explains a similar situation, how to do stuff like bash `cut` in Windows. In the link it's about cutting by slashes, you need to cut by backslashes. I don't deal much with windows cmd, so I searched for what I would use in bash. I posted link only because: a) it's a comment, not an answer, b) the answers in the link already explain stuff, c) I was at work, so no time to explain stuff. – h4z3 Jun 10 '19 at 15:20
  • Thank you. I wasn't able to see how I could use the linked information, but I appreciate your effort. – quaero Jun 10 '19 at 16:25

2 Answers2

1
(for /r "c:\startfolder" %%A in (*) do echo %%~nxA)|sort

(this is batch file syntax; for use directly on the command line, replace every %% with just %)

for /r loops recursively over all (non-hidden) files.

%%~nxA shows name and extension only (if you want just the name without extension, use %%~nA)

See for /? for more information on those modifiers.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you, but I'm not getting the result that I'm looking for: (1) The list of files is not *strictly* alphabetical; it is alphabetical, but the file list order is reflecting the order of the sub-folder paths, rather than ignoring those paths; in other words, the files are being grouped according to their path. (2) The output is showing all the files in the cmd window, as it should, but the output is *not* showing all the files in the .txt file that I have redirected (via ">") the output to; rather, the .txt file is only including the last file in the list. – quaero Jun 10 '19 at 13:52
  • Sorry - I completely forgot to sort them. See my edit please. – Stephan Jun 10 '19 at 13:54
  • Thank you, but the resulting text file for your revised/edited command is completely blank. ? – quaero Jun 10 '19 at 14:43
  • did you adapt the startfolder to your environment? – Stephan Jun 10 '19 at 18:02
  • It appears that the OP's complaint is that `filelist.txt` is not being produced. – lit Jun 10 '19 at 18:10
  • Re your question about adapting my start folder to my environment, I'm guessing that the answer to that would be "no". I just ran the command that you suggested, in the way that I usually do it when running similar commands, for example, a "dir /s" command. Would you please elaborate on what you mean? – quaero Jun 10 '19 at 18:16
  • if @lit is right, then just append `>filelist.txt`. Sorry, I thought that would be too obvious to explicitly mention. – Stephan Jun 10 '19 at 18:33
  • I meant: did you change `c:\startfolder` to the actual folder you want to search in. (if you want to search in your current working folder, you can omit that parameter) – Stephan Jun 10 '19 at 18:34
  • Now it appears that the OP's complaint is that you started from `C:\startfolder` and not `..\samplefolder` as given in the question. You're right, it should be too obvious to mention. – lit Jun 10 '19 at 18:35
  • Take it easy, guys. ;^) Yes, I know all that, I've been using the command line for ~20 years. I just wasn't sure what you meant by "adapt the startfolder to your environment". I may be doing something wrong, but it's not any of the obvious stuff you've mentioned. The suggested command didn't work. – quaero Jun 10 '19 at 19:54
  • 1
    well, it works for me, and as I can't see what you are trying, I have to guess. "not working" isn't very useful. Maybe you should edit your current (not working) command into your question. – Stephan Jun 10 '19 at 19:59
  • Here's one of the commands that I tried and that produced a blank text document: (for /r "D:\0 IPSE\LIBRARY\! OTHER\LANGUAGE\! GRAMMAR, USAGE, & STYLE\0 CMOS" %A in (*) do echo %~nxA)|sort > TEST409.txt – quaero Jun 10 '19 at 20:10
  • believe it or not - when I create that folder, it works. – Stephan Jun 10 '19 at 20:22
  • Hmm . . . The output displays correctly, just the way I want it to, in the cmd *window*---so thank you very much for getting me that far---but when I add "> testxxx.txt" to redirect the output to a .txt file, all I get is a text document that when opened has *no* text. Any ideas? – quaero Jun 10 '19 at 20:46
  • even that works fine for me. Are you sure, you look at the just created `testxxx.txt` and not an old one? (sorry to ask, but those things happen) – Stephan Jun 10 '19 at 20:53
  • Yes, thanks, I use the current time (e.g., 509) in the .txt doc file name each time I create it in order to avoid making that mistake. – quaero Jun 10 '19 at 21:09
  • @quaero, what's your current working directory? check if there's a file `testxxx.txt` there; or try to provide the absolute path by appending something like `> "D:\your\target\path\testxxx.txt"`... – aschipfl Jun 11 '19 at 13:32
  • @aschipfl Thank you, in response to your suggestion, I retested the first method, and when I did so, I had a hunch to scroll (and scroll) down the page of the .txt doc, and this led me to discover that the file list does in fact appear — beginning about several hundred lines down. I have redirected the output from cmd.exe to a .txt doc many times, and have never seen that happen. So, the output did indeed make it into the text file, *but* the problem that I found onscreen with this method where the listed files were duplicated with the full paths shown is also found in the .txt document. – quaero Jun 11 '19 at 19:10
  • @quaero: it doesn't list the files twice - and it doesn't list them with full path. Examine your file again, that's command repetition. Do `@echo off` or at least do `... do @echo %%~nxA...` to supress it (and the empty lines too). – Stephan Jun 11 '19 at 19:15
  • @Stephan Thank you, the other two commands that you've just suggested produce the right result, with no issues. And, yes, what I saw was command repetition, not the full path—sorry for the misinformation, but when I saw the problem, I didn't look at the text closely and so thought I had seen the full path in front of each filename. However, that original command *does* list all of the files twice: once by file name only, and once preceded by the command repetition. – quaero Jun 11 '19 at 20:39
  • no. for each iteration it repeats the command (the file name is part of the given command, so it's not "filename preceeded by..."), then lists the file. Both things have nothing to do with each other. Although `sort` disguises what happened, by listing first all the empty lines, then all of the command repetitions and only then all of the files (there may be a few exceptions due to alphabetical sort). – Stephan Jun 11 '19 at 21:01
  • @Stephan Thank you for the clarification and helpful explanation. I now have a proper understanding of what I was seeing. The practical effect is, however, essentially as I described it: the *original* command results in a list that includes each and every file name *twice*, unnecessarily doubling the length of the list. Your final, revised commands (@echo off, etc.) avoid that. Thanks again for that. – quaero Jun 11 '19 at 23:16
0

If the machine is on the current PowerShell 5 or higher, you could use:

(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name |
    Sort-Object |
    Out-File -PSPath 'filelist.txt' -Encoding ascii

In a .bat file script.

>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
    "(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name | Sort-Object"

If the machine does not have a current PowerShell, it should be upgraded or use:

>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
    "(Get-ChildItem -Recurse -Path '..\samplefolder'|" ^
        "Where-Object { -not $_.IsContainer}).Name |" ^
        "Sort-Object"
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thank you. I do have version 5 of PowerShell. Will try this later today. – quaero Jun 10 '19 at 14:45
  • This (PowerShell) was the best solution: (Get-ChildItem -Recurse -File -Path '..\samplefolder').Name | Sort-Object | Out-File -PSPath filelist.txt' -Encoding ascii – quaero Jun 11 '19 at 11:51
  • The PowerShell method had two advantages over the other (cmd.exe) method: (1) It produced the .txt document with all of the file names listed, and (2) it listed *only* those file names, whereas the other method also listed (in the cmd window, only) the files a *second* time, showing the full path. Thanks to both Stephan and lit. – quaero Jun 11 '19 at 12:00