Page 1 of 1

Reading Each Line Of A Text File

Posted: 02 Sep 2011 16:48
by Ethereality
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

Re: Reading Each Line Of A Text File

Posted: 02 Sep 2011 20:23
by nitt
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".

Re: Reading Each Line Of A Text File

Posted: 03 Sep 2011 02:21
by Ethereality
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

Re: Reading Each Line Of A Text File

Posted: 03 Sep 2011 03:57
by Ed Dyreen
'
New technic: set /p can read multiple lines from a file
viewtopic.php?f=3&t=2128

Re: Reading Each Line Of A Text File

Posted: 03 Sep 2011 13:16
by Ethereality
Thank You very much :) you saved me a lot of time, and I learned something new. :)

Peace

-Phill