# CMD.exe Batch Files - Writing out first line of files residing in a directory



## Dalgetty (Oct 18, 2009)

Apologies if I'm posting in the wrong place but this is my first post

Help! I'm stuck with a simple batch file I've been creating. It should be simple but .... ! I would be ever so grateful if someone could come to my rescue with the variables I need.

I have a directory of text files and I am trying to run these through a batch file so that I can access each file, read the first line of text in each one and write it to another file - ie append each first line, on a separate line in another file, as each file gets read in. However I've got stuck. 

Everywhere I've checked I've found a line of code which will pull the first line of text off a specific file but nowhere can I find one where there are many file names (residing in the directory) which have to be handled. 

What I have got so far is:

for %%a in ("c:\documents and settings\sh\my documents\file*.txt") do (
set /p var=<####
%var% >> output.fil
)

I need something to replace where I have the 4#s to reflect the different file names. They all start with File and have the extension .txt eg File125.txt, File176.txt, File205.txt etc

As a silver surfer I do my best but I'm way behind you experts!

Thank you in anticipation!

Dalgetty


----------



## TheOutcaste (Mar 19, 2009)

Welcome to TSF!

Give this a try:

```
@Echo Off
Setlocal EnableDelayedExpansion
Set _Path=%Userprofile%\My Documents
For /F "Tokens=1 Delims=" %%a In ('Dir /A-D /B "%_Path%\file*.txt"') Do (
Set /p var=<"%_Path%\%%a"
>>output.fil Echo.!var!
)
```
You can spell out the complete path instead of using the UserProfile variable if you are running this from a different user account.

HTH

Jerry


----------



## Dalgetty (Oct 18, 2009)

Jerry,

That was pure dead brilliant!! You're a star! It worked a treat. I have now added %%a between Echo. and !Var! so that the output file has the file name preceding the relevant line of text. However, I find I'm stymied again as I have been unable to get the code to output what I want eg output the name of the as File123 rather than File123.txt. In other words I want to leave off the file extension from the file name but I can't work out how. If this is possible I would be very grateful for your assistance again.

Many many thanks again!

Dalgetty


----------



## TheOutcaste (Mar 19, 2009)

The *~n* loop variable modifier will output the file name only:
*>>output.fil Echo.%%~nA - !var!
*
See *For /?* for a list of the loop variable modifiers, all the way at the bottom:

```
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

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
```


----------



## Dalgetty (Oct 18, 2009)

Hi Jerry,

When I changed the coding to >>output.fil Echo.%%~nA - !var! as you suggested I found it doesn't work - it produces %~nA + the line of text. Any suggestions what I'm doing wrong?

Kind regards

Dalgetty


----------



## TheOutcaste (Mar 19, 2009)

Drat, my bad. The loop variables are case sensitive. %%A and %%a are not the same, so use a lower case a - *%%~na*


----------



## Dalgetty (Oct 18, 2009)

Hi Jerry,

That's ace - a perfect solution for me!

Many thanks

Dalgetty


----------

