1.

Solve : Batch script pipe question?

Answer» HI I am wondering if something like this can be done:
say ECHO %USERNAME% returns hitesh
And I have a variable hitesh with the value
set hitesh=savla

I am wondering If I can do this:

if(%%USERNAME%% eq "savla") {
.......................
}

I KNOW this can be done in shell and perl scripts.
Can I do this in Batch script. I am thinking maybe using pipe or I have to store this in a file and then read it?
If someone can help that would be great.

set result=no&call echo %%username%% | find "savla">nul &&set result=yesThis is quite easy .... sort of, using Call :

Call Set %USERNAME%=savla

The call forces the %USERNAME% to be evaluated and return its content, so the line then is processed as

set hitesh=savla

To do your test, move the value of the wanted variable to a general one and test that :

Call Set %USERNAME%=savla

Call set generic=%USERNAME%
if(%generic% eq "savla") {
.......................
}

Graham



call echo %%username%% | find "savla">nul && (

................................

)Thanks for the replies guys.

Unfortunately none of the solutions works for me.
Neither the assignment to a generic variable or using find.

Let me attach a config file and a piece of my code here which will make a bit more sense.

Code: [Select]for /f "tokens=1,2 delims==" %%S in (users.cfg) do (
set %%S=%%T
)

What I am trying to achieve here is: If you look at the attached file, based on the access level of the user executing the script which could be wither DEV or GSD, I will forward the control to another label.

for eg:

if (%%USERNAME%% eq "GSD") goto label1
else goto label2

Let me know if you want to paste the entire code here.

Keep up the GOOD work!!


[attachment deleted by admin]Quote from: hiteshsavla on December 24, 2008, 08:02:58 AM

Code: [Select]for /f "tokens=1,2 delims==" %%S in (users.cfg) do (
set %%S=%%T
)

That code is nonsense. You cannot assign a value to the loop variable %%S with SET like that.

Wouldn't want to argue with you sir. But the code works.
And I got the solution from this forum only. So let me grab the topic and get back to you.Quote from: hiteshsavla on December 24, 2008, 08:11:17 AM
Wouldn't want to argue with you sir. But the code works.
And I got the solution from this forum only. So let me grab the topic and get back to you.

I think I see how it works now. This is very interesting. If I am wrong, and the code works as described, then I would be very INTERESTED to see it. Please post on here for my education and maybe I can work further on this problem for you.
Unfortunaltely I am unable to find the topic where I got this solution. Maybe sidewinder posted it(No offence to sidewinder, he has been most helpful).

But the code works for sure.

Here is a working script. you might want to add your username in the users.cfg file attached earlier and try the %%USERNAME%% solution.


[attachment deleted by admin]Why not just read the access code from the file?

Code: [Select]@ECHO OFF
set /p username=input username?
for /f "tokens=1,2 delims==" %%S in (users.cfg) do (
if "%%S"=="%username%" set access=%%T
)
echo user %username% has access level: %access%

if "%access%"=="GSD" goto GSDACCESS
if "%access%"=="DEV" goto DEVACCESS
echo Error
goto end

:DEVACCESS

echo here is DEV access
goto end

:GSDACCESS

echo here is GSD access

:end

pause

Even better. This works amazing and it has reduced the complications in the code too.

Thanks a million sir!!

Happy holidays.


Discussion

No Comment Found