Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: lee123abc
dir /b> directory.txt
type nul> result.txt
set counter=1
setlocal enabledelayedexpansion
for /f %%a in (directory.txt) do (
set filename=%%a
for /f %%A in (!filename!) do (
set firstline=%%A
if %counter% GEQ 2 goto :SKIPPED
echo !filename! !firstline!> result.txt
set /a counter +=1
:SKIPPED
)
)

It is forbidden to code GOTOs statements that refer to labels defined inside a For loop. Use the If ... (statements) else (statements) instead.
Issue covered a billion times in this Forum.

Hi all, new development!
The following code works, but doesn't read all the files out of the "directory.txt" The files it isn't reading look like "__Filename1.txt" I think the "_" are the problem. Could anyone help me adjust the code? What I am trying to do here is create a directory of files in a folder, and display the filename and first line of contents into a result file.
Cheers
::================================
@echo offtype nul> directory.txt
type nul> result.txtdir /a-d /x /b> directory.txt
setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%a in (directory.txt) do (
set filename=%%a
set counter=1
for /f "tokens=* delims=" %%b in (!filename!) do (
set firstline=%%b
if !counter! LEQ '1' (
echo !filename! --- !firstline!>> result.txt
set /a counter +=1
)
)
)
start result.txt

I don't see why there should be any problem with underscores, but I think there may be a problem if you have any files with spaces in them. I know you've done a dir/x which is supposed to give you 8.3 short filenames without spaces, but the /b switch seems to turn off the effect of the /x switch so you end up with long filenames again.
Try enclosing in quotes:
for /f "usebackq delims=" %%b in ("!filename!") do (

Here's another way to accomplish the task.
type nul> directory.txt type nul> result.txt dir /a-d /x /b> directory.txt setlocal enabledelayedexpansion for /f "tokens=* delims= " %%a in (directory.txt) do ( set /p firstline=<"%%a" >> result.txt echo %%a --- !firstline! ) start result.txt

Judago, thank you. Your code worked perfectly.
Klint, I did try that but the result was suprisingly the ("filename" --- "filename") instead of ("filename" --- "first line of file"). Thank you for looking at this though.
Have a good day all.

Have you used USEBACKQ as shown in my code? Without it, "filename" will be treated as a literal string instead of a file name.

Klint I apologise for that. Weh I was looking at the replies, I don't know what I did. I misread it completely and didn't try what you suggesteed. In fact I think I looked at my own post. (no excuses there).
I tried it now and it does exactly what I wanted as well. Thank you. Again, sorry.
Cheers

Actually, the confusion may have been my fault. Let me come clean and confess. When I first posted my solution, I had forgotten the USEBACKQ option. After submitting my reply I looked at it and was horrified at my mistake and immediately edited it to add USEBACKQ. You may have looked at it before I corrected it. So the apologies are mine.

hahaha!! One of those days!!! Klint, thanks again for your help, you guys have helped me with quite a few problems in the past. I appreciate it.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |