Computer Hope

Microsoft => Microsoft DOS => Topic started by: Redd4 on January 28, 2013, 05:34:25 AM

Title: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 05:34:25 AM
I need some help with a script I'm trying to write please. Ive been at this for days and Im not really getting anywhere, although I am getting to know cmd a lot better.

ill try describe what my script has to do, i cant write the commands out on the net im afraid

-------------------------------------------------------------------------------------------------------------------

The script passes the command line a command. CMD returns 4 lines of code. on the third line there is the UUID of the last software build. its not a hardware uuid.

I need to get my script to copy this UUID and append it onto the end of the next command my script passes, in order to proceed with the script.


-------------------------------------------------------------------------------------------------------------------

I'm new to this, I have been watching tutorials, and saw that tokens do something similar, so i tried to write a script using them. it doesnt work though, it keeps telling me it needs a unique key, in other words it doesnt have the uuid


------------------------------------------------------------------------------------------------------------


for /F "tokens=16" %%i in ('the command needed to return the lines of code') do set barney=%%i

something unregister -view -uuid   %barney%



------------------------------------------------------------------------------------------------------------


alas this is not working. can anyone point me in the right direction please? im thinking something like substring or equivalent, but the uuid starts like 120 characters into the returned output, so both tokens ans substring seem wrong

can anyone help please?

kind regards,
redd .
Title: Re: need help, should be simple... something like tokens
Post by: foxidrive on January 28, 2013, 07:33:24 AM
Shows us the string that is returned and it should be an easy fix.   Not knowing the whole string makes it a guessing game.
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 08:09:24 AM
cheers for the reply, I appreciate it


I cant show you the returned output unfortunately. I can show you an edited version with the correct amount of characters but not the words.
this is because I'm in work, I'm an intern.

Tag: 12345.A.B
  Global path: \\1234.1234512345678.com\CC\12345.A.B
  Server host: snap.1234512345678.com
  Region: windows
  Active: NO
  View tag uuid:d0a32387.0e874aae.85aa.86:bd:6a:ac:d3:ed
View on host: snap.1234512345678.com
View server access path: C:\CC\12345.A.B
View uuid: d0a32387.0e874aae.85aa.86:bd:6a:ac:d3:ed
View attributes: snapshot
View owner: 123456789\123451234567
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 08:10:29 AM
I need to somehow grab one of those uuids and add it to the end of my next command
Title: Re: need help, should be simple... something like tokens
Post by: foxidrive on January 28, 2013, 08:14:39 AM
There are more than 4 lines there :)

This should work (untested) after you replace command1 with the command that returns those lines, and command 2 is your next command to use the uuid.  ATM it will just echo the text to the console.


for /f "tokens=2,* delims=: " %%a in (' command1 ^|find /i "uuid:" ') do set "var=%%b"
echo command2 %var%

Note that I edited the above:
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 08:20:30 AM
thanks! Ill try it out and replort back. I think it may be tomorrow before I know, at the more theres a cleanup being peformed, so theres no UUID's to speak of.

I will report back though, once again, many thanks.
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 08:21:18 AM
edit noted
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 09:22:33 AM

okay, im just analysing what you did, and its pretty sweet. i have been trying to find a way to do this for a week now, so its nice seeing a way forward. may i ask you a few questions about the bits I dont understand please?


so far as I can make out, you searched the output for the string "uuid" and stored it, and the token next to it, as A and B.

I'm guessing the * next to tokens stores everything that was returned?

you then piped it to "Find" to search for the string "uuid"

may I ask what the /i    does?

regards,
redd .
Title: Re: need help, should be simple... something like tokens
Post by: Lemonilla on January 28, 2013, 09:49:57 AM
Quote from: cmd.exe
C:\Users\Lemonilla\Documents\bat>find /?
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 28, 2013, 10:29:20 AM
thanks Lemonilla. I just clicked how to read the help results, cheers
Title: Re: need help, should be simple... something like tokens
Post by: foxidrive on January 28, 2013, 03:43:31 PM

for /f "tokens=2,* delims=: " %%a in (' command1 ^|find /i "uuid:" ') do set "var=%%b"

It takes the lines that command1 generates and uses FIND to separate the ones that have uuid: in them.

The FOR then splits those lines up into 'words' by removing : and spaces (as delimiters)
2,* takes the 2nd word and then everything after the second word, and sets var to everything after the second word by incrementing %%a to %%b

In your example it will do this twice, once for each occurrence of uuid: and will remember the last one.
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 29, 2013, 03:38:04 AM
hey all. Thanks for explaining that Foxidrive.  It seems this site is one of the only resources for learning msdos on the net. I found Dilberts tutorial and am having a good look through it.
Theres a guy on youtube also, starkIndustries, hes very good, but, the tutorials cut off at a certain point. - a certain point before the level of foxidrive's command lol, it never gets that advanced.

anywho, I tried the command this morning, and got a "The syntax of the command is incorrect." reply

so i tried with with a SYSTEMINFO command in the parenthesis, and a different string to find, but the same error came back.

it seems theres something slightly awry with the first line
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 29, 2013, 07:20:28 AM
okay, if I take out the * and replace it with 3, -

for /f "tokens=2,3" %a in (' SYSTEMINFO ^|find /i "broadcom" ') do set "var=%b"
echo I like cats and %var%


It works! woohoo! i get this


C:\Windows\System32>echo I like cats and %var%
I like cats and NetXtreme



however, it requires that i hit enter after

C:\Windows\System32>echo I like cats and %var%

in order to get the next line, "I like cats and NetXtreme" which is not ideal. brilliant though, love it.
Title: Re: need help, should be simple... something like tokens
Post by: Lemonilla on January 29, 2013, 01:27:23 PM
The extra enter may be due to running it from command line, when I run it in a batch file, It doesn't pause (until my 'pause>nul').

Code: [Select]
@echo off
for /f "tokens=2,3" %%A in (' systeminfo ^| find /i "broadcom" ') do set "var=%%B"
echo I like cats and %var%
pause>nul

Code: [Select]

C:\Users\Lemonilla>cd ..\..

C:\>test.bat
I like cats and 802.11n

Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 30, 2013, 02:08:55 AM
your right, I just had a go there and it ran through without pausing.

If i may id like to as another question. the batch file im trying to create has in it three tasks. first is the one above, and the next are two delete commands.
the scripts is one of many executed my anthill pro as part of a nightly software build. so, i have to add error handling, im a little bit confused about it.. so far my file goes like this, using the example above

--------------------------------------------------------------------------------------------------

:: step 1

for /f "tokens=2,3" %%a in (' SYSTEMINFO ^|find /i "broadcom" ') do set "var=%%b"

if there are no tokens -

IF ERRORLEVEL 1 (
  goto builderror
)

echo I like cats and %var%


:: below are to delete the directories
::----------------------------------------------
:: step 2

RD /S /q  C:\CA\repositorydriver\directoryA

IF ERRORLEVEL 1 (
  goto builderror
)

:: step 3

RD /S /q  C:\CA\repositorydriver\directoryB

IF ERRORLEVEL 1 (
  goto builderror
)


:builderror
echo *** An error has occured executing clearRegistry.bat 


:exit
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on January 30, 2013, 02:10:58 AM
i need to to proceed to step two, even if it finds no tokens during step 1

but i dont know how to do that. is there a continue command?
Title: Re: need help, should be simple... something like tokens
Post by: Lemonilla on January 30, 2013, 04:25:49 AM
You may want to put an exit before ':builderror' as of now it will display that even without an error.

Another thing, if it errors on the 2nd 'rd' the first time you run, it will always error, as the first 'rd's target is gone. I don't know if you wanted to do that or not.

Code: [Select]
for /f "tokens=2,3" %%a in (' SYSTEMINFO ^|find /i "broadcom" ') do set "var=%%b"
::If var is empty then there were no tokens found
if "%var%"=="" goto builderror
and I do believe you need an opening '(' after "do" if you plan on including other stuff in your for loop.
Title: Re: need help, should be simple... something like tokens
Post by: foxidrive on January 30, 2013, 06:57:30 PM
I'm not sure of your question but check the additions in blue

for /f "tokens=2,3" %%a in (' SYSTEMINFO ^|find /i "broadcom" ') do set "var=%%b"

IF not defined var (
  goto builderror
)

echo I like cats and %var%



RD /S /q  C:\CA\repositorydriver\directoryA

IF ERRORLEVEL 1 (
  goto builderror
)

RD /S /q  C:\CA\repositorydriver\directoryB

IF ERRORLEVEL 1 (
  goto builderror
)

goto :EXIT

:builderror
echo *** An error has occured executing clearRegistry.bat 
pause

:exit
Title: Re: need help, should be simple... something like tokens
Post by: Lemonilla on January 30, 2013, 07:40:21 PM
Was finally able to run some tests, scratch what I said about if the first target is not there.
Code: [Select]
C:\Users\Lemonilla>rd AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
The system cannot find the file specified.

C:\Users\Lemonilla>echo %errorlevel%
0

I did not know about 'if defined', very useful. Thanks foxidrive!
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on February 01, 2013, 02:32:05 AM
thanks guys, really though! still plugging away at getting it right, was nice to log in and see more refinements
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on February 01, 2013, 02:36:48 AM
Lemonilla your version worked also, -  if "%var%"=="" goto builderror -
was better than my "if errorlevel" - which didnt work in that case
Title: Re: need help, should be simple... something like tokens
Post by: Squashman on February 01, 2013, 08:29:44 PM
hey all. Thanks for explaining that Foxidrive.  It seems this site is one of the only resources for learning msdos on the net.

you are not learning msdos. msdos is an operating system. you are learning batch scripting. plenty of places to get help for batch scripting.

rob vanderwoude has a great website as well as eric phelps.
stack overflow is a great forum as is dostips.com, techguy and computer hope.
Title: Re: need help, should be simple... something like tokens
Post by: Redd4 on February 05, 2013, 06:43:41 AM
cheers Squashman, good to know, going forward, seems im not finished learning by a longshot.