If/Else using data from a file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

If/Else using data from a file?

#1 Post by chunter2 » 03 Oct 2014 10:28

I would like to do an if/else statement using data from a text file.

I will have a file list.txt
It will have a list of IDs....
ex)
312906
714598
212375


I want to create an if/else statement that checks to see if the current windows user (%USERNAME%) is listed in the list.txt file

If the current window user is listed in the list.txt, I want to xcopy a file from, let's say C:\test.txt to C:\work\"

else, if the current window user is not listed in the list.txt, I want to xcopy a file from, let's say C:\test2.txt to C:\work\"


thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#2 Post by Squashman » 03 Oct 2014 10:56

How about conditional execution instead.

Code: Select all

findstr /B /E "%USERNAME%" "list.txt" >nul 2>&1 &&(xcopy C:\test.txt C:\work\) ||(xcopy C:\test2.txt C:\work\)

chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

Re: If/Else using data from a file?

#3 Post by chunter2 » 03 Oct 2014 11:41

Squashman wrote:How about conditional execution instead.

Code: Select all

findstr /B /E "%USERNAME%" "list.txt" >nul 2>&1 &&(xcopy C:\test.txt C:\work\) ||(xcopy C:\test2.txt C:\work\)



that's an even better solution. Thank you!

chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

Re: If/Else using data from a file?

#4 Post by chunter2 » 03 Oct 2014 13:02

actually hold on...

I created a file add.bat

it contains this:

Code: Select all

echo %USERNAME% >> "C:\Users\test\Desktop\test\list.txt"


basically, I'm writing the username to the list.txt file....but when i do....and then run that code you gave me to search for it....it says its not there. But, when I hand type the username in the file, then it says its there.

I'm not sure if I'm using the correct method to write data to a file. I want to write a username to the list.txt....and I would like it to appear on the next available line.
Its just really weird that, it is writing the username to the file when I click on the add.bat, but the other code says it cannot find it....but when I type it in myself, it can find it. Strange.
I'm pretty new to batch files and DOS commands, so maybe I'm not understanding something.

I'm running everything from batch files and I'm using Windows XP.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#5 Post by Squashman » 03 Oct 2014 13:25

You are echoing a space to the output file.
The findstr command is checking to make sure the username matches at the beginning of the line and at the end of the line.
Since you are echoing an extra space to the output file it doesn't match.

chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

Re: If/Else using data from a file?

#6 Post by chunter2 » 03 Oct 2014 13:31

Squashman wrote:You are echoing a space to the output file.
The findstr command is checking to make sure the username matches at the beginning of the line and at the end of the line.
Since you are echoing an extra space to the output file it doesn't match.


How do you prevent adding the extra space?
Do I need the >> symbols?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#7 Post by Squashman » 03 Oct 2014 13:33

Code: Select all

C:\BatchFiles\Conditional>echo squashman >>file.txt

C:\BatchFiles\Conditional>findstr /B /E "squashman" "file.txt" >nul 2>&1 &&(echo username is there) ||(echo username not there)
username not there

C:\BatchFiles\Conditional>echo squashman>>file.txt

C:\BatchFiles\Conditional>findstr /B /E "squashman" "file.txt" >nul 2>&1 &&(echo username is there) ||(echo username not there)
username is there

C:\BatchFiles\Conditional>

chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

Re: If/Else using data from a file?

#8 Post by chunter2 » 03 Oct 2014 14:50

ok, this helped me out:

I just now learned that DOS does not have a line break. So, I just first, wrote to an empty line using echo. and then wrote the username.

Code: Select all

echo.>>"C:\Users\test\Desktop\test\list.txt"
echo %USERNAME%>>"C:\Users\test\Desktop\test\list.txt"


this worked...it put the id on a new line.

for future reference, does the space right after the word echo mean anything? Does the blank space between echo and %username% put a blank space in the file?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#9 Post by Squashman » 03 Oct 2014 16:18

chunter2 wrote:Does the blank space between echo and %username% put a blank space in the file?

Did you not understand my last example?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#10 Post by Squashman » 03 Oct 2014 16:23

chunter2 wrote:this worked...it put the id on a new line

If you started a file by typing a username on a line and didn't hit enter, the first line you output to that file from a batch file would append to the last line but would also end the line with a crlf so the next time you run the batch file you would not have to echo a crlf to the file before you echo the username to it.

chunter2
Posts: 6
Joined: 03 Oct 2014 10:12

Re: If/Else using data from a file?

#11 Post by chunter2 » 06 Oct 2014 10:17

Squashman wrote:
chunter2 wrote:this worked...it put the id on a new line

If you started a file by typing a username on a line and didn't hit enter, the first line you output to that file from a batch file would append to the last line but would also end the line with a crlf so the next time you run the batch file you would not have to echo a crlf to the file before you echo the username to it.


I didn't fully understand your example. I'm still really new at DOS. I know javascript pretty well, but DOS is very foreign to me....and the little nuances confuse me.

What is a crlf?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: If/Else using data from a file?

#12 Post by ShadowThief » 06 Oct 2014 11:02

chunter2 wrote:
Squashman wrote:
chunter2 wrote:this worked...it put the id on a new line

If you started a file by typing a username on a line and didn't hit enter, the first line you output to that file from a batch file would append to the last line but would also end the line with a crlf so the next time you run the batch file you would not have to echo a crlf to the file before you echo the username to it.


I didn't fully understand your example. I'm still really new at DOS. I know javascript pretty well, but DOS is very foreign to me....and the little nuances confuse me.

What is a crlf?

"Carriage return line feed." You know it as \r\n.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: If/Else using data from a file?

#13 Post by foxidrive » 06 Oct 2014 11:07

chunter2 wrote:How do you prevent adding the extra space?
Do I need the >> symbols?


This first line will create or overwrite an existing file
The second line will create or append to an existing file.

Code: Select all

 >"C:\Users\test\Desktop\test\list.txt" echo %USERNAME%

>>"C:\Users\test\Desktop\test\list.txt" echo %USERNAME%


Putting the redirection first allows you to avoid spaces at the end of text, and eliminates another issue to do with a numeral directly before the > sign.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: If/Else using data from a file?

#14 Post by Squashman » 06 Oct 2014 12:54

chunter2 wrote:I'm still really new at DOS. I know javascript pretty well, but DOS is very foreign to me....and the little nuances confuse me.

What is a crlf?

I guess you don't know VBscript either and Lucky for you, you are not using DOS.

Post Reply