Strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Strings

#1 Post by alexandredneto » 28 Feb 2013 13:45

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

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

Re: Strings

#2 Post by Squashman » 28 Feb 2013 14:34

This would be better

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

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#3 Post by alexandredneto » 01 Mar 2013 05:44

I want to store the variables (from the 'FOR') in other variables. Not being possible. How do you do?

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

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

Re: Strings

#4 Post by foxidrive » 01 Mar 2013 06:33

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%

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

Re: Strings

#5 Post by Squashman » 01 Mar 2013 06:43

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.

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#6 Post by alexandredneto » 01 Mar 2013 07:12

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?

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

Re: Strings

#7 Post by Squashman » 01 Mar 2013 08:00

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?

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#8 Post by alexandredneto » 04 Mar 2013 05:56

Hello!
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?

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

Re: Strings

#9 Post by foxidrive » 04 Mar 2013 06:50

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]
)

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#10 Post by alexandredneto » 04 Mar 2013 08:44

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:

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

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

Re: Strings

#11 Post by foxidrive » 04 Mar 2013 11:49

Try this to see if it is what you need: (untested)

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
   )
)

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#12 Post by alexandredneto » 04 Mar 2013 13:07

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.

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

Re: Strings

#13 Post by foxidrive » 04 Mar 2013 13:15

You are going to have to escape the = signs too.

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Strings

#14 Post by alexandredneto » 05 Mar 2013 06:43

Hi, Guys.

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.

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

Re: Strings

#15 Post by foxidrive » 05 Mar 2013 07:34

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"

Post Reply