0

I have read this thread, which helped, but doesn't answer my specific question. I'm hoping someone can help.

I am trying to export a CSV file with TWO COLUMNS of data. Column 1 is the actual filename (with extension), and Column 2 would be the immediate Folder Name (without any path info) of the file location. Question I have is, is that possible? Next question is, how can I export this as a CSV file with two columns of information?

This is a good starting point except this only has the filename (doesn't have the second column that shows the immediate folder name), and this doesn't seem to return to the next line for each filename. Instead this is simply separating with commas and not returning to new lines.

Can you advise if this is possible and offer some ideas?

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

Thanks everyone!

4
  • CSV means "comma separated values", what about comma's in file names?
    – Endoro
    Apr 17, 2013 at 18:38
  • @Endoro - Commas in filenames shouldn't be treated as delimiters as long as the csv cells are in quotation marks.
    – rojo
    Apr 17, 2013 at 22:58
  • @rojo - you are completely right, file names doesn't contain double quotes, so this will work.
    – Endoro
    Apr 18, 2013 at 4:28
  • This is answered in a single line at <a href="stackoverflow.com/questions/19184470/…>.
    – ebruton
    Apr 3, 2014 at 9:11

2 Answers 2

0

If by the "Immediate folder name" you mean the name of the containing directory but without the path to that directory, then:

@ECHO OFF
SETLOCAL
PUSHD "%~1"
FOR /f "delims=" %%i IN ("%cd%") DO SET directory=%%~nxi
(
FOR /f "delims=" %%i IN ('dir /b /a-d /on') DO (
 SETLOCAL enabledelayedexpansion
 ECHO "%%i","!directory!"
 endlocal
)
)>filelist.csv
POPD

The pathname of the directory required should be supplied as the first parameter, quoted if necessary.

Essentially, change to the directory in question, find and save the name of the leaf directory, then execute a directory scan returning the filenames. Quote both and output with a comma between. The inner setlocal is to allow at least some silly directory names.


edit 20130422-1421Z

@ECHO OFF
SETLOCAL
PUSHD "%~1"
FOR /f "delims=" %%i IN ("%cd%") DO SET directory=%%~nxi
(
FOR /f "delims=" %%i IN ('dir /b /a-d /on') DO (
 SET fdate=%%~ti
 SETLOCAL enabledelayedexpansion
  ECHO "%%i","!directory!","!fdate:~0,10!"
 endlocal
)
)>filelist.csv
POPD

Edited to show date as third element. Quotes retained - remove at will. If date AND TIME are required, remove the SET fdate line and replace the "!fdate:~0,10!" with "%%~ti

Date and time format - to be certain, need to know the format you are using.

5
  • Thanks. This works great for my purposes. However, is there anyway to remove the quotes around the data, or does that have to be there to export the list correctly? Here's an example of what the script is generating for me: "108-73.pdf","Manufacturing - Holding Area" "109-27.pdf","Manufacturing - Holding Area" "112-14.pdf","Manufacturing - Holding Area" "112-16.pdf","Manufacturing - Holding Area"
    – Tim S
    Apr 18, 2013 at 19:50
  • Certainly - just remove the quotes from the line ECHO "%%i","!directory!"
    – Magoo
    Apr 18, 2013 at 19:56
  • Thanks so much for the help. Is there a way to add a third column to the exported file with the last "modified date" for the files as "MM/DD/YYYY"?
    – Tim S
    Apr 22, 2013 at 14:00
  • Have edited, but to be certain need to know which date/time format you are using. As-is will display your date format - not necc. the one you want.
    – Magoo
    Apr 22, 2013 at 14:30
  • Thanks - I'm needing this shown as MM/DD/YYYY. Is that possible? Haven't checked yet to see the format your new script generates already though...will be able to check in about 15 minutes after a meeting
    – Tim S
    Apr 22, 2013 at 14:47
0

If you're doing a recursive directory search, filename with extension only can be obtained within your for /f loop from %%F by using %%~nxF. That's easy.

The trickier part is scraping the last folder from the path (%%~pF). There's actually an easy trick to that as well though. Use another for loop to get %%~nxI of %%~dpF. Yes, the filename.ext of a full path is the trailing directory.

This only works if the directory does not end in a trailing backslash, though. Since the result of %%~dpF does end in a trailing backslash, you can work around it simply by adding a single dot to the end. So instead of "c:\users\me\Desktop\" you get the ~nx of "c:\users\me\Desktop\." with a trailing dot.

Enough explanation. Here's how it's done.

@echo off

for /f "delims=" %%F in ('dir /s /b /o:n') do (
    for %%I in ("%%~dpF.") do echo "%%~nxF","%%~nxI"
) >filelist.csv
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.