Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Embedded variables not setting  (Read 3151 times)

0 Members and 1 Guest are viewing this topic.

Raven19528

    Topic Starter


    Hopeful
  • Thanked: 30
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Embedded variables not setting
    « on: April 29, 2015, 05:12:43 PM »
    Hello,

    Haven't been on in a while, but ran into a problem today and must be hitting a mental block or something, because I can't see what I'm doing wrong.

    The following code is trying to populate a csv file with some names and statuses. For whatever reason, the embedded variables do not set like they should. What am I doing wrong?

    Code: [Select]
    @echo off
    cls
    setlocal enabledelayedexpansion

    for /f "tokens=1,2*" %%A in (Alpha.txt) do (
      call :subAlpha %%A %%B
    )
    goto :eof


    :subAlpha
    set lname=%1
    set lname=%lname:,=%
    for /f "tokens=*" %%i in ('trim.bat %lname%') do set lname=%%i
    set fname=%2
    set fname=%fname:-=%
    for /f "tokens=*" %%j in ('trim.bat %fname%') do set fname=%%j
    set a43check=-
    set d75check=-
    for /f "tokens=1,2*" %%E in (A43.txt) do (
      call :sub43 %%E %%F
    )
    for /f "tokens=1,2*" %%J in (D75.txt) do (
      call :sub75 %%J %%K
    )
    echo fname = %fname%
    echo lname = %lname%
    echo a43fname = %a43fname%
    echo a43lname = %a43lname%
    echo d75fname = %d75fname%
    echo d75lname = %d75lname%
    pause
    echo "%lname%, %fname%", %a43check%, %d75check% >> revalidate.csv
    goto :eof

    :sub43
    set a43lname=%1
    set a43lname=%a43lname:,=%
    for /f "tokens=*" %%k in ('trim.bat %%a43lname%%') do set a43lname=%%k
    set a43fname=%2
    set a43fname=%a43fname:-=%
    for /f "tokens=*" %%l in ('trim.bat %a43fname%') do set a43fname=%%l
    if "%lname%"=="%a43lname%" (
      if "%fname%"=="%a43fname%" (
        set a43check=X
      )
    )
    goto :eof

    :sub75
    set d75lname=%1
    set d75lname=%d75lname:,=%
    for /f "tokens=*" %%m in ('trim.bat %d75lname%') do set d75lname=%%m
    set d75fname=%2
    set d75fname=%d75fname:-=%
    for /f "tokens=*" %%n in ('trim.bat %d75fname%') do set d75fname=%%n
    if "%lname%"=="%d75lname%" (
      if "%fname%"=="%d75fname%" (
        set d75check=X
      )
    )
    goto :eof
    trim.bat
    Code: [Select]
    @echo off
    set trim=%1
    :backloop
    if "%trim:~-1,1%"==" " (
      set trim=%trim:~0,-1%
      goto backloop
    )
    :frontloop
    if "%trim:~0,1%"==" " (
      set trim=%trim:~1%
      goto frontloop
    )
    echo %trim%
    "All things that are
    Are with more spirit chased than enjoy'd" -Shakespeare

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Embedded variables not setting
    « Reply #1 on: April 30, 2015, 10:01:10 AM »
    A little more explanation of the aim, the problem, and the error or result, would be handy.

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Embedded variables not setting
    « Reply #2 on: April 30, 2015, 03:59:49 PM »
     ;)
    " Anyone who goes to a psychiatrist should have his head examined. "

    Raven19528

      Topic Starter


      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Embedded variables not setting
      « Reply #3 on: April 30, 2015, 05:39:32 PM »
      Yeah that would probably be helpful ;D

      So when I run this script (with the echos in there) I receive the following output on the screen:
      fname = Bob
      lname = Smith
      a43fname =
      a43lname =
      d75fname =
      d75lname =


      So the first variables are setting, but the embedded ones are not setting. What I am trying to accomplish is comparing the list in Alpha, which is a list of over 500 names, with the names in the other two files (about 300 a piece) and see which ones are in all three, only two, or only in the Alpha list.

      I haven't tried breaking embedded calls into their own files yet, but I realize that could be a working option. Is there any way to get this to work in the single file as it is set up now?
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Embedded variables not setting
      « Reply #4 on: April 30, 2015, 06:51:09 PM »
      He should reconsider his use of goto :eof

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Embedded variables not setting
      « Reply #5 on: April 30, 2015, 08:16:57 PM »
      He should reconsider his use of goto :eof
      Why?

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Embedded variables not setting
      « Reply #6 on: April 30, 2015, 09:35:01 PM »
      Yeah that would probably be helpful ;D

      So when I run this script (with the echos in there) I receive the following output on the screen:
      fname = Bob
      lname = Smith
      a43fname =
      a43lname =
      d75fname =
      d75lname =


      Without the needed input file(s) - we'd go crazier than we are, trying to work out what you are doing.
      Give us some data in input file(s) in the correct formats...

      The variables aren't being set because of the way you are setting them - maybe within a loop. 

      Raven19528

        Topic Starter


        Hopeful
      • Thanked: 30
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: Embedded variables not setting
        « Reply #7 on: May 01, 2015, 08:14:09 AM »
        So this is some sample text from the input files:

        Alpha.txt

        SMITH, BOB J
        JONES, JANE A
        WILLIAMS, ANDREW G
        ...


        A43.txt

        SMITH, BOB A43.PDF
        WILLAMS, ANDREW A43.PDF
        WHITE, JIM A43.PDF
        ...


        D75.txt

        SMITH, BOB D75.PDF
        HOBBS, JACOB D75.PDF
        GREEN, NORM D75.PDF
        ...


        Why am I not able to set those variables within a loop? I don't recall that ever being a problem before. Is it something where maybe I should use a call set command to set the variables?
        "All things that are
        Are with more spirit chased than enjoy'd" -Shakespeare

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Embedded variables not setting
        « Reply #8 on: May 01, 2015, 09:57:15 AM »
        Why?
        Because the feature is for advanced users.
        Because one who can not write a few lines of batch code and not know how to debug it not use goto : eof

        Each part of the code should be in a separate file with echo left on. He would verify wash file;'s behavior separately. Only after getting it to work right would he combine the files and user the goto : eof thing. This is the proven method for novices. Beginners should not use that feature until they can write 100 lines of code sand get it right the first  time.
        In other words, he did not use a debugging method suitable for his skill level.
        Not that being a beginner is a crime. But one should not use code that is to hard to debug.

        Raven19528

          Topic Starter


          Hopeful
        • Thanked: 30
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          Re: Embedded variables not setting
          « Reply #9 on: May 01, 2015, 11:11:07 AM »
          Okay, ran this through manually, after breaking out all the calls into separate files. Found that the additional text files weren't processing for some reason. Changed the command to for /f "tokens=1,2" %%E in ('type A43.txt') do (

          Works like it should now. Run time is a little long, but it's reading these files over and over, and honestly, its doing the job for me so I don't have to, so I'm happy with that.

          Thank you Geek. While I'm sure I have some rust, I was doing regular batch scripts for a while, and now getting back into it for some more mundane tasks, I'm sure there is plenty I need to relearn.
          "All things that are
          Are with more spirit chased than enjoy'd" -Shakespeare

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Embedded variables not setting
          « Reply #10 on: May 01, 2015, 02:26:21 PM »
          I am glad that worked for you and glad your replied.
          Having a program that works is more important than speed. Inmost cases.
          Batch files, by nature, are not well suited for speed optimization.
          If  you have a need to write lots of batch files,  you might consider using The Microsoft recommended advancement: PowerShell
          http://en.wikipedia.org/wiki/Windows_PowerShell
          Also see:
          Jump Start PowerShell Video
          Have fun !   :D


          « Last Edit: May 01, 2015, 02:41:12 PM by Geek-9pm »

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: Embedded variables not setting
          « Reply #11 on: May 02, 2015, 10:49:05 PM »
          So this is some sample text from the input files:

          It's nice of you to provide some sample data.

          I know you have a solution but a comment I have from looking briefly at your code is that trim.bat will do nothing at any stage with any variable - because the data you are passing will never have leading or trailing spaces due to the way the data is passed in the call statements.

          Another issue is that the %2 term will always lose part of the second term in the call statements.

          I didn't analyse it past noting those problems - it would be really helpful to discuss what the batch script is supposed to be doing, with some examples, when you ask your question.

          The input data is essential of course - kudo's to you for providing some (Often, instead of input data we get an argument about how we don't need any).

          If you want to optimise your code for speed then give us all the info and someone may be able to offer some ideas.