Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top