Reading Each Line Of A Text File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ethereality
Posts: 3
Joined: 02 Sep 2011 16:32

Reading Each Line Of A Text File

#1 Post by Ethereality » 02 Sep 2011 16:48

Hello everyone, a friend of mine requested that I create a batch script for him that will transform a text file, full of names, into a series of folders with those same names. I know how to do this but I need help with the very first part. Reading from the text file.

This is what I have so far for this fuction:

Code: Select all

for /f "delims=(code for return...)" %%a in Names.txt do set n1=%%b & set n2=%%c


Then I would use the variables to create the dirs I need. But for some reason I get an error:
(EDIT) I fixed the error because I was using cmd to run the code. Changed %% to % and that got rid of the first problem. Sorry about that, but now there is this one.

Code: Select all

Names.txt was unexpected at this time.



For the record I have no experence with using the 'for' command or any of its functions. Mostly because I havent really been able to find someone to help me with it. It is kind of confusing to me. Anyways I also need the delimiter for a carrage return if anyone knows it. I could also do this in vbs but I would like to try this first.

Thx in advance

-Phill

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Reading Each Line Of A Text File

#2 Post by nitt » 02 Sep 2011 20:23

I'm confused by your question... But reading text line by line isn't too difficult.

You just need to use the "for /f" loop with the "type filename.txt" command. "type" just displays all the text in the file, and we can use "for /f" to loop through each line. Also, the "tokens=*" part just makes it read the entire line, and not just a single delimiter.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('type filename.txt') do (
set line=%%a
echo !line!
)
pause


The reason I used "!line!" instead of just "%%a", is because you can do more with normal variables.

Delimiters work like this. If I set the delimiter to a space by saying: "tokens=1 delimiters= ", and since tokens are 1, if I have a line such as "dog pie lol", then it will separate the string by my delimiter, and display the one labeled by the token. In this case, "1", so it will display "dog". If tokens was "2", it would display "pie". "3", it would display "lol". "*" will display the entire thing. If I change the delimiter to "o", such as "tokens=1 delimiters=o", then the first tokens is "d". The secon is "pie l", and the third is "l".

Ethereality
Posts: 3
Joined: 02 Sep 2011 16:32

Re: Reading Each Line Of A Text File

#3 Post by Ethereality » 03 Sep 2011 02:21

Yeah thx that is really helping me to understand a whole lot better than I did before. But I still have a problem. I need each line in the txt file to be saved to a specific variable. For example if the file reads:

Code: Select all

Sam
Bob
Stephen


I need to be able to set each of those names to make as a specific directory. I can do all of that but I just need to understand how to do set EACH name as a variable. Sorry I dont mean to repeat myself if I am. I am just trying to make a little more sense than I did before.
So far the problem that I have with the code you posted is that "Echo !line!" shows me all of the lines in the whole text file.
If I did this right now:

Code: Select all

Md C:\Users\%username%\Desktop\%line%


LOL My directory would be named "SamBobStephen" instead of just Sam XD. That is all lol I am done I will check this in the morning. Btw you really have been helpful dont give up XD.

-Phill

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Reading Each Line Of A Text File

#4 Post by Ed Dyreen » 03 Sep 2011 03:57

'
New technic: set /p can read multiple lines from a file
viewtopic.php?f=3&t=2128

Ethereality
Posts: 3
Joined: 02 Sep 2011 16:32

Re: Reading Each Line Of A Text File

#5 Post by Ethereality » 03 Sep 2011 13:16

Thank You very much :) you saved me a lot of time, and I learned something new. :)

Peace

-Phill

Post Reply