# Solved: Creating a list from a text file



## hmhill17 (Sep 13, 2010)

I've seen several posts about creating lists from files, but none seem to be quite what I need. What I have is a list of filenames in a text file. I used dir /on /b to create it. What I want to do is take that list and turn it into a comma separated list so I can use that variable with another command. I need to specify the first file in the list, but the subsequent files can be in any order. Sample file list. Tere_cover.tif needs to be the first file in the list.

_0804101805_001002.tif
commercial_accountant.tif
first_try.tif
Quote 1620.tif
servicing_template.TIF
ServicingFiles-Template-Updated8-4-10_2.TIF
tere_cover.tif

Thanks


----------



## Squashman (Apr 4, 2003)

You want all the file names in your text file to be a comma separated list on one line?

_0804101805_001002.tif,commercial_accountant.tif,first_try.tif,Quote 1620.tif,servicing_template.TIF,ServicingFiles-Template-Updated8-4-10_2.TIF,tere_cover.tif


----------



## hmhill17 (Sep 13, 2010)

That is correct. However the tere_cover.tif has to be the first filename in the list.


----------



## Squashman (Apr 4, 2003)

Not on a computer right now but I can give you an idea of how to do it.

Put your dir command into a for loop but also pipe it to a find command so that you can exclude the first file.
Then use the set command to keep appending the for loop variable to another variable with a comma.

Before the for loop set your variable you are appending too as your first fielname.


----------



## TheOutcaste (Aug 8, 2007)

Squashman already did the heavy lifting, all I had to do was write it out:

```
@Echo Off
SetLocal EnableDelayedExpansion
:: Set these next two lines as needed
Set _Source=C:\Temp Dir
Set CSVList=tere_cover.tif
PushD "%_Source%"
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /ON /B^|Findstr /I /V "%CSVList%"') Do Set CSVList=!CSVList!,%%I
PopD
Echo.%CSVList%
```


----------



## hmhill17 (Sep 13, 2010)

Thanks for the help. With a bit of tweaking, I was able to make it do exactly what I want.


----------



## Squashman (Apr 4, 2003)

hmhill17 said:


> Thanks for the help. With a bit of tweaking, I was able to make it do exactly what I want.


Please post your code back here so that it may help people who are searching for a similar example.

If your question is solved then use the Thread Tools link to mark the Thread Solved.


----------



## hmhill17 (Sep 13, 2010)

The only change was for the addition of path information in the list.

@Echo Off
SetLocal EnableDelayedExpansion
:: Set these next two lines as needed
Set _Source=C:\combine_test
Set CSVList=c:\coversheets\tere_cover.tif
PushD "%_Source%"
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /ON /B^|Findstr /I /V "%CSVList%"') Do Set CSVList=!CSVList!,c:\combine_test\%%I
PopD


----------



## Squashman (Apr 4, 2003)

hmhill17 said:


> The only change was for the addition of path information in the list.
> 
> @Echo Off
> SetLocal EnableDelayedExpansion
> ...


Well you could have done that two different other ways besides hard coding the path.
You coudl have used the _Source variable that was already defined if it is always going to be the same.

```
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /ON /B^|Findstr /I /V "%CSVList%"') Do Set CSVList=!CSVList!,%_Source%\%%I
```
You could have used one of the command modifiers as well which makes you Super Cool Batch programmer.

```
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /ON /B^|Findstr /I /V "%CSVList%"') Do Set CSVList=!CSVList!,%%~fI
```


```
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
```


----------



## Squashman (Apr 4, 2003)

TheOutCaste pointed out something to me earlier. Your first file isn't even located in the your source directory. We assumed it was. If it is not in the source directory then you don't even need to use the FindStr command in the For Loop.


----------

