12/2/2023 0 Comments Folder file list export to .csv![]() Some CSV readers handle double-quote escaping differently, treating two double-quote characters in a row as a literal double-quote: $ 'ls' | sed -e 's/"/""/g' -e 's/^/"/' -e 's/$/"/' > files. That is to say, we escape any existing double-quote characters before wrapping the line in semantic quotes. If you really do have double-quotes in your file names, the sed solution extends naturally: $ 'ls' | sed -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/"/' > files.csv These sed string replacement commands put double quotes at the beginning and end of each line, which prevents a CSV reader from treating commas as field separators.Īnother way to achieve the same end is perl -ne 'chomp print "\"$_\"\n"' csv icon on the toolbar, select the destination directory, type in the filename and then click the save button to create the. ![]() This is going to be a much more common case since double quotes have meaning in Unix command shells, so there is a strong disincentive to using them in file names: $ 'ls' | sed -e 's/^/"/' -e 's/$/"/' > files.csv It's not too difficult to cope with these cases.įirst let's take the case of files that may only have commas in them. But What If There Are Special Characters?ĭouble quotes and commas are special characters in CSV files, so if you try the above command on a directory containing files named using such characters, you won't get a valid CSV file. This CSV file won't have a header line, but Excel can cope with that not all CSV readers can. POSIX requires single-column output in this case, and GNU and BSD ls obey. When ls is writing to a pipeline instead of a terminal, it prints one file name per line instead of the multi-column output you show in your question. Such aliases are quite common on modern Unix and Unix-like systems. You want to quote the ls command in case you have an alias that adds flags to it that affect the output, such as -F, which appends file type sigils, or -C, which forces multi-column output. Since your example file names don't have any double quotes or commas in them, the solution is quite simple: $ 'ls' > files.csv 1) type cmd into the start menu to bring up a dos prompt 2) use the command (for instance) 'cd C:\Users\BobJones\Desktop\New folder' to navigate to the folder in question 3) use the command 'DIR > doggy.csv' to create a file named doggy.csv in the same directory that you can open directly with excel. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |