If/Else using data from a file?
Moderator: DosItHelp
If/Else using data from a file?
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
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
Re: If/Else using data from a file?
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\)
Re: If/Else using data from a file?
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!
Re: If/Else using data from a file?
actually hold on...
I created a file add.bat
it contains this:
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.
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.
Re: If/Else using data from a file?
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.
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.
Re: If/Else using data from a file?
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?
Re: If/Else using data from a file?
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>
Re: If/Else using data from a file?
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.
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?
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?
Re: If/Else using data from a file?
chunter2 wrote:Does the blank space between echo and %username% put a blank space in the file?
Did you not understand my last example?
Re: If/Else using data from a file?
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.
Re: If/Else using data from a file?
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?
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: If/Else using data from a file?
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.
Re: If/Else using data from a file?
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.
Re: If/Else using data from a file?
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.