|
Answer» Hi all,
I am having a hard time trying to get the FOR command to process a text file correctly. The FOR command seems to only process the left colum in my text file and not the entire line which contains more words / strings.
The Scenario: 1) Dump a list of users on this machine to a text file. 2) Compare the list with the users input criterea if users already exist or not. 3) Return the result in a SET command.
My approach: Set all user input as individual SET statements. Eg. USER1=%1 USER2=%2 USER3=%3 etc. etc.
Dump the CURRENT users on this machine to text file. NET USERS >list.txt
Use the FOR command to itterate the users input with the FIND command. Export any hits if true to rslt1.txt
Any subsequent FOR commands are to to filter postive HITS from negative hits and join them into seperate SET statements.
My SOURCE code.
At the command prompt type this: TEST.CMD John Jill Mark Jack Suzan We will asume that Jill & Jack already exists on your system. The remainder are potential new users for creation. I suggest you cut and paste this batch an test it on your system. It does no harm. Test it on some true users accounts and some fake accounts to understand what I mean.
@echo off set errorlevel= PROMPT $P$G CLS
REM Split all the requested users up into seperate variables. REM We do this because the FIND works only with single search strings. set user1=%1 set user2=%2 set user3=%3 set user4=%4 set user5=%5 set user6=%6 set user7=%7 set user8=%8 set user9=%9
REM Here is where we look for our user on this machine. net users>list.txt
REM Individually test each user with the FIND command and store any HITS REM in a text file. FOR %%i in (%user1% %user2% %user3% %user4% %user5% %user6% %user7% %user8% %user9% ) do find /I "%%i" <list.txt >>rslt1.txt
SET EL=%ERRORLEVEL%
REM Breakdown any users already existing on this system with the REM FOR command searching thru our earlier results from FIND. FOR /F %%i in (rslt1.txt) do if /I "%user1%"=="%%i" (set usr1=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user2%"=="%%i" (set usr2=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user3%"=="%%i" (set usr3=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user4%"=="%%i" (set usr4=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user5%"=="%%i" (set usr5=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user6%"=="%%i" (set usr6=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user7%"=="%%i" (set usr7=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user8%"=="%%i" (set usr8=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user9%"=="%%i" (set usr9=%%i)
if /I NOT "%user1%"=="%usr1%" (set us1=%user1%) if /I NOT "%user2%"=="%usr2%" (set us2=%user2%) if /I NOT "%user3%"=="%usr3%" (set us3=%user3%) if /I NOT "%user4%"=="%usr4%" (set us4=%user4%) if /I NOT "%user5%"=="%usr5%" (set us5=%user5%) if /I NOT "%user6%"=="%usr6%" (set us6=%user6%) if /I NOT "%user7%"=="%usr7%" (set us7=%user7%) if /I NOT "%user8%"=="%usr8%" (set us8=%user8%) if /I NOT "%user9%"=="%usr9%" (set us9=%user9%)
REM Now combine the positive (already exists) users in a single list (LISTB) for easier display. set listb=%usr1% %usr2% %usr3% %usr4% %usr5% %usr6% %usr7% %usr8% %usr9%
REM Now combine the negative (available) users in a single list (LISTA) for easier display. set lista=%us1% %us2% %us3% %us4% %us5% %us6% %us7% %us8% %us9%
echo. echo The errorlevel for FIND is: %EL% echo. if not "%EL%"=="1" goto warning goto end
:warning echo WARNING! Some user accounts are already in use! echo. if not "%listb%"==" " echo These user(s) %listb% already exists! echo. if not "%lista%"==" " echo You could continue to make these (new) users: %lista% goto exit
:end echo NOTIFICATION: You can safely use the following list to make users. echo. echo The user(s) %lista% does not exist. This is Good! echo. goto exit
:exit set user1= set user2= set user3= set user4= set user5= set user6= set user7= set user8= set user9= set usr1= set usr2= set usr3= set usr4= set usr5= set usr6= set usr7= set usr8= set usr9= set us1= set us2= set us3= set us4= set us5= set us6= set us7= set us8= set us9= set el= set lista= set listb= Gustaaf,
If I can't read it, I can't answer it. Please repost this in black and white. Save the Technicolor for the movies.
Hi all, I am having a hard time trying to get the FOR command to process a text file correctly. The FOR command seems to only process the left colum in my text file and not the entire line which contains more words / strings.
The Scenario: 1) Dump a list of users on this machine to a text file. 2) Compare the list with the users input criterea if users already exist or not. 3) Return the result in a SET command.
My approach: Set all user input as individual SET statements. Eg. USER1=%1 USER2=%2 USER3=%3 etc. etc. Dump the current users on this machine to text file. NET USERS >list.txt Use the FOR command to itterate the users input with the FIND command. Export any hits if true to rslt1.txt Any subsequent FOR commands are to to filter postive HITS from negative hits and join them into seperate SET statements.
My SOURCE code.
At the command prompt type this: TEST.CMD John Jill Mark Jack Suzan We will asume that Jill & Jack already exists on your system. The remainder are potential new users for creation. I suggest you cut and paste this batch an test it on your system. It does no harm. Test it on some true users accounts and some fake accounts to understand what I mean.
@echo off set errorlevel= PROMPT $P$G cls REM Split all the requested users up into seperate variables. REM We do this because the FIND works only with single search strings. set user1=%1 set user2=%2 set user3=%3 set user4=%4 set user5=%5 set user6=%6 set user7=%7 set user8=%8 set user9=%9 REM Here is where we look for our user on this machine. net users>list.txt REM Individually test each user with the FIND command and store any HITS REM in a text file. FOR %%i in (%user1% %user2% %user3% %user4% %user5% %user6% %user7% %user8% %user9% ) do find /I "%%i" >rslt1.txt SET EL=%ERRORLEVEL% REM Breakdown any users already existing on this system with the REM FOR command searching thru our earlier results from FIND. FOR /F %%i in (rslt1.txt) do if /I "%user1%"=="%%i" (set usr1=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user2%"=="%%i" (set usr2=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user3%"=="%%i" (set usr3=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user4%"=="%%i" (set usr4=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user5%"=="%%i" (set usr5=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user6%"=="%%i" (set usr6=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user7%"=="%%i" (set usr7=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user8%"=="%%i" (set usr8=%%i) FOR /F %%i in (rslt1.txt) do if /I "%user9%"=="%%i" (set usr9=%%i) if /I NOT "%user1%"=="%usr1%" (set us1=%user1%) if /I NOT "%user2%"=="%usr2%" (set us2=%user2%) if /I NOT "%user3%"=="%usr3%" (set us3=%user3%) if /I NOT "%user4%"=="%usr4%" (set us4=%user4%) if /I NOT "%user5%"=="%usr5%" (set us5=%user5%) if /I NOT "%user6%"=="%usr6%" (set us6=%user6%) if /I NOT "%user7%"=="%usr7%" (set us7=%user7%) if /I NOT "%user8%"=="%usr8%" (set us8=%user8%) if /I NOT "%user9%"=="%usr9%" (set us9=%user9%) REM Now combine the positive (already exists) users in a single list (LISTB) for easier display. set listb=%usr1% %usr2% %usr3% %usr4% %usr5% %usr6% %usr7% %usr8% %usr9% REM Now combine the negative (available) users in a single list (LISTA) for easier display. set lista=%us1% %us2% %us3% %us4% %us5% %us6% %us7% %us8% %us9% echo. echo The errorlevel for FIND is: %EL% echo. if not "%EL%"=="1" goto warning goto end :warning echo WARNING! Some user accounts are already in use! echo. if not "%listb%"==" " echo These user(s) %listb% already exists! echo. if not "%lista%"==" " echo You could continue to make these (new) users: %lista% goto exit :end echo NOTIFICATION: You can safely use the following list to make users. echo. echo The user(s) %lista% does not exist. This is Good! echo. goto exit :exit set user1= set user2= set user3= set user4= set user5= set user6= set user7= set user8= set user9= set usr1= set usr2= set usr3= set usr4= set usr5= set usr6= set usr7= set usr8= set usr9= set us1= set us2= set us3= set us4= set us5= set us6= set us7= set us8= set us9= set el= set lista= set listb=I bet you get paid by the line.
Lets see. You want one file with users already defined and one file with available user names.
Code: [Select] @echo off net user > list.txt if exist listA del listA if exist listB del listB
:start if .%1==. goto :end find /i "%1" list.txt > nul if errorlevel 1 goto listA if errorlevel 0 goto listB
:listA echo %1 >> listA goto loop
:listB echo %1 >> listB goto loop
:loop shift goto start
:end echo These names are available: type listA echo. echo These names are in use: type listB
Feel free to make any changes you need but this should give you an idea how to PROCEED. Now that the names are split into separate files, you can do whatever it is you need to do.
Batch files may have mystical powers but there are easier scripting languages available.
Hope this helps. I wish I was paid per line coding, but alas, its all for personal HOBBY. Thanx, the code is simple and lots more faster then my FOR statements. It works great too. Give yourself a golden star on my behalf. I had contemplated using shift to clean up the mysterious gaps in my results. But I would not have placed it in a looping fashion like you did.
But now that is out the way, what is with the FOR command not processing each line of text? Why does it only process the first word of each new line?This link may answer all your questions>>http://www.robvanderwoude.com/index.htmlI believe the /F in your for line is why you only pull the first word. Leave it out and try. You could also do "tokens=1-10 delims= " to get the first 10 words.
|