get list of files to table format in CMD

2019-08-17 16:14发布

问题:

Could you please help me to get list of files to table format (I prefer batch file). For list command I would use dir /b /o:n to sort files by name.. but this produces paragraphs. I need to copy the list of files to xls table... and the files will be in columns. So my ask is this:

If you have files like 1,2,3,4,5... etc I want them to be written on one line, where tabulator is used as separator between files.

回答1:

"Where tabulator (?) is used as separator" ??? If you want to open the file with Excel, then the simplest option is to use commas as a delimiter (.CSV format). Since a comma can appear in a file name, you want to enclose the names in quotes.

If you are sorting by name, then a simple FOR is your most efficient option, since it always sorts by name. You can use <nul SET /P to output each filename to a .CSV file without introducing a new line.

@echo off
<nul (
  for %%F in (*) do set /p =""%%F","
) >fileList.csv

If you really want to use the DIR command, then you need FOR /F. The only reason I can see to do so is if you want to change the sort order. I set EOL=: to guard against the unlikely possibility of a file name starting with a semicolon. A valid file name (or file path) can never start with a colon.

@echo off
<nul (
  for /f "eol=: delims=" %%F in ('dir /b /o:n') do set /p =""%%F","
) >fileList.csv