Batch File help

Discussion in 'Software' started by IrOnMaN, Jun 25, 2013.

  1. IrOnMaN

    IrOnMaN Specialist

    Im trying to set up wireless profiles automatically across the domain. since its a mixed environment of win xp/7 i cant use the built in netsh command to deploy it.

    i found a free program from symantec that can accomplish this on winXP and someone made a batch file to deploy. i tried to do it locally first but it fails. ive attached the batch file and the results i get.

    the program im using can be found here

    let me know if i left any info you need out.

    Thanks,
    KJ
     

    Attached Files:

  2. GermanOne

    GermanOne Guest

    Seems you simply forgot one percent sign.
    Change that line and try again.
    Code:
        echo SSID: %%~ni has been configured!
     
  3. IrOnMaN

    IrOnMaN Specialist

    i need a little more help than that. i didnt write that batch file someone else did on another site. that batch file is more advanced than the batch files i usually deal with. you will have to show me what you think it should be.
     
  4. GermanOne

    GermanOne Guest

    Well I may be familiar with batch files but I'm not familiar with this 3rd party tool. So I need some more information. How does the content of the xml files look like (or what do you expect they should contain)? What exactly will invoking the sp option change in your xml files?

    The remarks explain what your current batch file tries to do.
    Code:
    @echo off
    echo Grabbing WLAN Interface...
    REM Find line beginning with "GUID:" and assign the GUID value to variable "Interface".
    For /F "skip=2 tokens=1,2" %%A IN ('wlan.exe ei') Do If "%%A"=="GUID:" set Interface=%%B
    
    REM For each XML file in the current directory ...
    For %%i in (*.xml) do (
        echo Adding wlan SSID %%~ni
    
        REM ... invoke option sp with the found GUID.
        wlan.exe sp %Interface% %%i
    
        echo.
        echo SSID: %%~ni has been configured!
    )
    pause
    
     
  5. IrOnMaN

    IrOnMaN Specialist

    Ill attach the xml file with the extension changed to txt. What the program does is export the wireless profile from a computer and save it as an xml file so you can import it to another computer. im trying to do it across the domain using group policy so thats why im need this batch file to work. :)

    Thanks for your time and patience!
     

    Attached Files:

  6. GermanOne

    GermanOne Guest

    OK I downloaded the wlan tool. Then I used your file (renamed as linksys.xml and saved into the batch file directory) and the following batch code:
    Code:
    @echo off &setlocal
    cd /d "%~dp0"
    For /F "skip=2 tokens=1,2" %%A IN ('wlan.exe ei') Do If "%%A"=="GUID:" set Interface=%%B
    wlan.exe sp %Interface% linksys.xml
    pause
    
    Output:
    Now checked with the following code:
    Code:
    @echo off &setlocal
    cd /d "%~dp0"
    For /F "skip=2 tokens=1,2" %%A IN ('wlan.exe ei') Do If "%%A"=="GUID:" set Interface=%%B
    wlan.exe gpl %interface%
    pause
    
    Output:
    As you can see profile linksys was added.

    Hope that helps :)
     
  7. IrOnMaN

    IrOnMaN Specialist

    ok i may understand it better if i learn what all these lines mean. these variables are confusing me. we will start at the beginning,

    For /F "skip=2 tokens=1,2" %%A IN ('wlan.exe ei') Do If "%%A"=="GUID:" set Interface=%%B

    i dont know what /F is and skip=2 tokens=1,2 Can you explain this to me?
     
  8. GermanOne

    GermanOne Guest

    Of course I will explain.
    A FOR /F loop processes a stream. In case of your example it processes the output stream of the command line
    wlan.exe ei
    You should execute this line separately to understand what happens. If I do that I'll get back:
    Code:
    There are 1 interfaces in the system.
    Interface 0:
            GUID: 50c7b436-9428-4c19-856a-e2627b28caf3
            Broadcom 802.11n-Netzwerkadapter
            State: "connected"
    
    Command "ei" completed successfully.
    
    "skip=2" skips the first 2 lines of that output and lets the loop execution start with line 3.
    FOR /F splits each line in tokens (substrings) where the default delimiters (separators) are space and tab. Leading delimiters are truncated automatically.
    "tokens=1,2" means that the first 2 substrings of a line are accessible. If %%A is defined as dynamic FOR variable then the first substring is assigned to %%A and the second to %%B. Since FOR iterates line by line you need an IF statement as a filter to catch the line with "GUID:" as first substring.

    Back to my example output.
    Line 1 and 2 are skipped.
    Line 3 is the first line that will be executed in the loop. The leading spaces are truncated. That means GUID: is assigned to %%A. Next character is a space that devides the line. 50c7b436-9428-4c19-856a-e2627b28caf3 is assigned to %%B. Now the IF statement checks whether or not the string GUID: was assigned to %%A. In that case the comparison result is true and the content of %%B will be assigned to variable Interface.

    Just to complete what happens to the remainding lines:
    %%A=Broadcom, %%B=802.11n-Netzwerkadapter, the comparison result is false.
    %%A=State:, %%B="connected", the comparison result is false.
    %%A=Command, %%B="ei", the comparison result is false.

    Thus, the only line where the comparison returned true was the third line and 50c7b436-9428-4c19-856a-e2627b28caf3 was assigned to Interface.
     
    Last edited by a moderator: Jun 27, 2013
  9. IrOnMaN

    IrOnMaN Specialist

    great job with that explanation i have a much better understanding of it now. Now for the second part...

    For %%i in (*.xml) do (
    echo Adding wlan SSID %%~ni
    wlan.exe sp %Interface% %%i

    echo.
    echo SSID: %%~ni has been configured!

    Explain it like you did the first one and i think ill have it.
     
  10. IrOnMaN

    IrOnMaN Specialist

    Also i finally seen what you was talking about in your first post about the extra %. I wasnt paying enough attention and when you quoted that line i thought it already had %% and i needed to add another one. the file seems to work now. I still want the explanation to understand how it imports the xml file.
     
  11. GermanOne

    GermanOne Guest

    If you use a FOR loop in the command window you have to write only one percent sign. In a batch code you have to double them though. So I assume it was just a typo in the code that you copied.

    The explanation for the second half of your code:
    Code:
    For %%i in (*.xml) do (
    A simple FOR loop (without options like /D, /R, /F or /L) is made to process a file or a set of files. The expression enclosed into parentheses represents the file name. "Wildcards" (those are placeholders where an asterisk is for no or any number of characters and a question mark would be for a single character) can be used to find all files that match this pattern.
    Thus, *.xml is for each found XML file in the current working directory. Every found file name will be assigned to %%i step by step and executed in the next block of command lines (again enclosed into a pair of parentheses). Only in this block is %%i a valid variable.


    Code:
      echo Adding wlan SSID %%~ni
    ECHO lines will print text into your batch window. The %%~ni is a modification of %%i. ~n lets the variable expand to the file name only (without extension .xml).
    There are some more of these modifiers available. Execute for /? to get an help message where all of them are explained.


    Code:
      wlan.exe sp %Interface% %%i
    Well actually that line should be clear now. Wlan.exe invokes option sp with the found GUID and one of the found XML files as additional parameters.


    Code:
      echo.
    Often used to print an empty line. I prefer echo( even if it looks odd. It's more stable. Echo. may fail in some circumstances.


    Code:
      echo SSID: %%~ni has been configured!
    Just another message printed in the batch window.


    Code:
    )
    Closing parenthesis for the block of command lines.
     
  12. IrOnMaN

    IrOnMaN Specialist

    Thanks for all the help. sorry for making you type so much. Now if i ever see something similar to that i wont be staring at it with a dumb look on my face. it will be slightly less dumb looking :-D

    Again great explanations and much appreciated.

    Thanks,
    KJ
     
  13. GermanOne

    GermanOne Guest

    You're welcome.
    IMO loops are the most significant elements in every programming language. You should do your best effort to understand how they're working. That's the main reason why I tried to explain them more detailed ;)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds