Strings
Moderator: DosItHelp
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Strings
hi, guys.
File users:
sp\alonso
ac\willian
rj\ronda
BAT:
@echo off
for /F "tokens=*" %%a in (users.txt) do (
echo --------//--------
echo Text ==: %%a
SET str=%%a
SET domain=%str:~0,2%
SET user=%str:~3%
echo domain=: %domain%
echo user ==: %user%
)
Result:
--------//--------
Text ==: sp\alonso
domain=: rj
user ==: ronda
--------//--------
Text ==: ac\willian
domain=: rj
user ==: ronda
--------//--------
Text ==: rj\ronda
domain=: rj
user ==: ronda
The result is correct. How?
--------//--------
Text ==: sp\alonso
domain=: sp
user ==: alonso
--------//--------
Text ==: ac\willian
domain=: ac
user ==: willian
--------//--------
Text ==: rj\ronda
domain=: rj
user ==: ronda
File users:
sp\alonso
ac\willian
rj\ronda
BAT:
@echo off
for /F "tokens=*" %%a in (users.txt) do (
echo --------//--------
echo Text ==: %%a
SET str=%%a
SET domain=%str:~0,2%
SET user=%str:~3%
echo domain=: %domain%
echo user ==: %user%
)
Result:
--------//--------
Text ==: sp\alonso
domain=: rj
user ==: ronda
--------//--------
Text ==: ac\willian
domain=: rj
user ==: ronda
--------//--------
Text ==: rj\ronda
domain=: rj
user ==: ronda
The result is correct. How?
--------//--------
Text ==: sp\alonso
domain=: sp
user ==: alonso
--------//--------
Text ==: ac\willian
domain=: ac
user ==: willian
--------//--------
Text ==: rj\ronda
domain=: rj
user ==: ronda
Re: Strings
This would be better
Output
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%G in (users.txt) do (
echo --------//--------
echo domain=: %%G
echo user ==: %%H
)
Output
Code: Select all
--------//--------
domain=: sp
user ==: alonso
--------//--------
domain=: ac
user ==: willian
--------//--------
domain=: rj
user ==: ronda
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
I want to store the variables (from the 'FOR') in other variables. Not being possible. How do you do?
Code:
Result:
Code:
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%i in (users.txt) do (
echo %%i %%j
SET domain=%%i
SET user=%%j
echo domain: %domain%
echo user : %user%
)
Result:
Code: Select all
sp alonso
domain: rj
user : ronda
ac willian
domain: rj
user : ronda
rj ronda
domain: rj
user : ronda
Re: Strings
Try this, or use delayed expansion.
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%i in (users.txt) do (
echo %%i %%j
SET domain=%%i
SET user=%%j
)
echo domain: %domain%
echo user : %user%
Re: Strings
You should probably explain better what you are trying to do because storing them into a variable is not going to do any good unless you are doing some other string manipulation to the variable. Each iteration of the loop will change your Domain and User variable and if you are just echoing the result all you need are the token variables.
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
Squashman, your interpretation is correct.
Each iteration of the loop will change your Domain ($domain) and User variable ($user).
I would not want to use the variables of tokens.
If this is possible. May you show me an example?
Each iteration of the loop will change your Domain ($domain) and User variable ($user).
I would not want to use the variables of tokens.
If this is possible. May you show me an example?
Re: Strings
alexandredneto wrote:Squashman, your interpretation is correct.
Each iteration of the loop will change your Domain ($domain) and User variable ($user).
I would not want to use the variables of tokens.
If this is possible. May you show me an example?
What are you planning on doing with those variables?
What is the end result of your batch file?
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
Hello!
I need to have a greater knowledge about the DOS environment variables.
The script below:
File no_pager.csv it is a relationship of not having the pager field.
File pager.csv it is the relation of having the pager field, but I want to add information to the pager field in the same line of echo %%d; %%e. How do I do that?
I need to have a greater knowledge about the DOS environment variables.
The script below:
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%d in (users.txt) do (
dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int | dsget user -pager -l | findstr [0-9] && ( echo %%d; %%e; here_pager >> pager.csv ) || ( echo %%d; %%e >> no_pager.csv )
)
)
File no_pager.csv it is a relationship of not having the pager field.
File pager.csv it is the relation of having the pager field, but I want to add information to the pager field in the same line of echo %%d; %%e. How do I do that?
Re: Strings
What does this return to the console? Explain a bit further how you need to use the output.
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%d in (users.txt) do (
echo "%%d","%%e"
dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int | dsget user -pager -l | findstr [0-9]
)
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
I'll try to explain better what the algorithm should do:
The dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int | dsget user -pager -l | findstr [0-9] command displays information pager, only if it exists.
This example, suppose only rj\ronda not return the pager field.
users.txt:
test.bat:
When you run the code (test.bat), there are two situations:
1) When the command does not return the pager field
If no pager field should store ( %%d and %%e ) in the last line of the file no_pager.csv. This is already ok! An example file no_pager.csv:
rj\ronda
2) When the command returns the pager field. Here is my difficulty completing the code:
Existed the information should be stored in the following variables %%d, %%e, and pager information field on the last line of the file pager.csv. An example file pager.csv:
sp\alonso; 12345678
ac\willian; 87654321
The dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int | dsget user -pager -l | findstr [0-9] command displays information pager, only if it exists.
This example, suppose only rj\ronda not return the pager field.
users.txt:
Code: Select all
sp\alonso
ac\willian
rj\ronda
test.bat:
Code: Select all
@echo off
for /F "tokens=1,2 delims=\" %%d in (users.txt) do (
dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int | dsget user -pager -l | findstr [0-9] && ( echo %%d; %%e; here_pager >> pager.csv ) || ( echo %%d; %%e >> no_pager.csv )
)
)
When you run the code (test.bat), there are two situations:
1) When the command does not return the pager field
If no pager field should store ( %%d and %%e ) in the last line of the file no_pager.csv. This is already ok! An example file no_pager.csv:
rj\ronda
2) When the command returns the pager field. Here is my difficulty completing the code:
Existed the information should be stored in the following variables %%d, %%e, and pager information field on the last line of the file pager.csv. An example file pager.csv:
sp\alonso; 12345678
ac\willian; 87654321
Re: Strings
Try this to see if it is what you need: (untested)
You may need to escape the comma in the dsquery command with ^,
You may need to escape the comma in the dsquery command with ^,
Code: Select all
@echo off
setlocal enabledelayedexpansion
for /F "tokens=1,2 delims=\" %%d in (users.txt) do (
set "pager="
for /f "delims=" %%f in ('dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int ^| dsget user -pager -l ^| findstr [0-9]') do set "pager=%%f"
if defined pager (
>> pager.csv echo %%d; %%e; !pager!
) else (
>> no_pager.csv echo %%d; %%e
)
)
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
dsquery failed:'sp' is an unknown parameter.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
type dsget /? for help.dsquery failed:'ac' is an unknown parameter.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
type dsget /? for help.dsquery failed:'rj' is an unknown parameter.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
type dsget /? for help.dsquery failed:'ac' is an unknown parameter.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
type dsget /? for help.dsquery failed:'rj' is an unknown parameter.
type dsquery /? for help.dsget failed:'Target object for this command' is missing.
Re: Strings
You are going to have to escape the = signs too.
-
- Posts: 39
- Joined: 28 Feb 2013 13:40
Re: Strings
Hi, Guys.
I fell that the code (foxidrive) is right.
But I'm having trouble using the espaces in the following line:
Note: You are helping me a lot.
I fell that the code (foxidrive) is right.
But I'm having trouble using the espaces in the following line:
Code: Select all
for /f "delims=" %%f in ('dsquery user -samid %%e dc=%%d,dc=correiosnet,dc=int ^| dsget user -pager -l ^| findstr [0-9]') do set "pager=%%f"
Note: You are helping me a lot.
Re: Strings
I can't test this but see how this goes.
Code: Select all
for /f "delims=" %%f in ('dsquery user -samid %%e dc^=%%d^,dc^=correiosnet^,dc^=int ^| dsget user -pager -l ^| findstr [0-9]') do set "pager=%%f"