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

Author Topic: help with cmd script  (Read 4360 times)

0 Members and 1 Guest are viewing this topic.

yebalda

    Topic Starter


    Starter

    help with cmd script
    « on: April 21, 2010, 02:37:59 AM »
    hello
    i just have a quick question
    is there a way or a script trough cmd
    that can take some data in a line and put it in other line
    like this:

    <name>boris</name>
    <vir_name></vir_name>

    is there a way to take the boris name and paste it in
    <vir_name>

    so it will look like this
    <name>boris</name>
    <vir_name>boris</vir_name>

    and for multiple cases if theres multiple
    <name>name of someone</name>

    and under that theres an empty
    <vir_name></vir_name>

    it will look for the data in <name> and place the variable in <vir_name>
    in the whole file


    thanks.

    ghostdog74



      Specialist

      Thanked: 27
      Re: help with cmd script
      « Reply #1 on: April 21, 2010, 03:21:17 AM »
      download gawk for windows, then  use this gawk script

      Code: [Select]
      BEGIN{ FS="[<>]" }
      /<name>/{ name=$3 }
      /<vir_name>/{ $0="<vir_name>"name"</vir_name>" }
      {print}
      save as myscript.awk, and on command line

      Code: [Select]
      C:\test>type file
      <name>boris</name>
      <vir_name></vir_name>
      blah
      <name>becker</name>
      <vir_name></vir_name>
      blah

      C:\test>gawk -f myscript.awk file
      <name>boris</name>
      <vir_name>boris</vir_name>
      blah
      <name>becker</name>
      <vir_name>becker</vir_name>
      blah

      redirect using ">" to save to a new file as desired.

      yebalda

        Topic Starter


        Starter

        Re: help with cmd script
        « Reply #2 on: April 21, 2010, 06:31:36 AM »
        download gawk for windows, then  use this gawk script

        Code: [Select]
        BEGIN{ FS="[<>]" }
        /<name>/{ name=$3 }
        /<vir_name>/{ $0="<vir_name>"name"</vir_name>" }
        {print}
        save as myscript.awk, and on command line

        Code: [Select]
        C:\test>type file
        <name>boris</name>
        <vir_name></vir_name>
        blah
        <name>becker</name>
        <vir_name></vir_name>
        blah

        C:\test>gawk -f myscript.awk file
        <name>boris</name>
        <vir_name>boris</vir_name>
        blah
        <name>becker</name>
        <vir_name>becker</vir_name>
        blah

        redirect using ">" to save to a new file as desired.

        thanks for the reply but specifically this i can do with simple cmd script
        what i am looking for is a script that will be more dynamic

        if i am working on an xml file and have multiple properties in there and the property names are
        <DISPLAY_NAME>blabla</DISPLAY_NAME>
        and i also have a property
        <CANONICAL_NAME></CANONICAL_NAME>

        i would want it to fill every value that in <DISPLAY_NAME> in to <CANONICAL_NAME>
        and there can be a dynamic number of <DISPLAY_NAME> properties
        and the <CANONICAL_NAME> property is already in the xml file
        so i dont need to create it
        i just need to insert the data from <DISPLAY_NAME> in to <CANONICAL_NAME>

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: help with cmd script
        « Reply #3 on: April 22, 2010, 05:48:03 AM »
        Quote
        thanks for the reply but specifically this i can do with simple cmd script
        what i am looking for is a script that will be more dynamic

        More dynamic than what? If you already have a simple cmd script, why are you not using it?

        Microsoft provides an object (actually two) for traversing the nodes in a XML file, however yours is ill-formed and would choke a VBScript. For instance, judging from your post, <name> and <vir_name> are siblings. What is the parent node?  Where do <DISPLAY_NAME> and <CANONICAL_NAME> logically fit into the tree? Is there a root to the tree and if so what is it.

        Personally I don't find Gawk to be very intuitive, but it works and sometimes you need brute force instead of dynamic.

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        yebalda

          Topic Starter


          Starter

          Re: help with cmd script
          « Reply #4 on: April 22, 2010, 09:37:08 AM »
          More dynamic than what? If you already have a simple cmd script, why are you not using it?

          Microsoft provides an object (actually two) for traversing the nodes in a XML file, however yours is ill-formed and would choke a VBScript. For instance, judging from your post, <name> and <vir_name> are siblings. What is the parent node?  Where do <DISPLAY_NAME> and <CANONICAL_NAME> logically fit into the tree? Is there a root to the tree and if so what is it.

          Personally I don't find Gawk to be very intuitive, but it works and sometimes you need brute force instead of dynamic.

           8)

          hi, i have a script that does what the gawk script above but i dont really need it in most situations
          for example i have properties in some xml

                 <PROPERTY>
                      <CANONICAL_NAME></CANONICAL_NAME>
                      <CURRENT_STATE>4</CURRENT_STATE>
                      <DISPLAY_NAME>Hello</DISPLAY_NAME>
                      <FORMAT_STRING></FORMAT_STRING>
                      <IS_COMPUTED>1</IS_COMPUTED>
                      <IS_MANAGED>0</IS_MANAGED>
                      <IS_MULTI_VALUED>0</IS_MULTI_VALUED>
                      <IS_PCODE></IS_PCODE>
                      <IS_STRING>1</IS_STRING>
                      <IS_URI></IS_URI>
                      <LOCALE_ID>1</LOCALE_ID>
                      <PROPERTY_ID>11111111</PROPERTY_ID>
                      <STORAGE_UNIT></STORAGE_UNIT>
                      <TIP_TEXT></TIP_TEXT>
                      <VALUE_SORT_TYPE></VALUE_SORT_TYPE>
                      <VIEWBY_UNIT></VIEWBY_UNIT>
                      <MANAGED_PROPERTY_VALUES></MANAGED_PROPERTY_VALUES>
                  </PROPERTY>

          these are most situation and the properties are build that way so what i need is
          a script that will take 'Hello' from <DISPLAY_NAME> and paste it in <CANONICAL_NAME>
          so the final file will look like that

                 <PROPERTY>
                      <CANONICAL_NAME>Hello</CANONICAL_NAME>
                      <CURRENT_STATE>4</CURRENT_STATE>
                      <DISPLAY_NAME>Hello</DISPLAY_NAME>
                      <FORMAT_STRING></FORMAT_STRING>
                      <IS_COMPUTED>1</IS_COMPUTED>
                      <IS_MANAGED>0</IS_MANAGED>
                      <IS_MULTI_VALUED>0</IS_MULTI_VALUED>
                      <IS_PCODE></IS_PCODE>
                      <IS_STRING>1</IS_STRING>
                      <IS_URI></IS_URI>
                      <LOCALE_ID>1</LOCALE_ID>
                      <PROPERTY_ID>11111111</PROPERTY_ID>
                      <STORAGE_UNIT></STORAGE_UNIT>
                      <TIP_TEXT></TIP_TEXT>
                      <VALUE_SORT_TYPE></VALUE_SORT_TYPE>
                      <VIEWBY_UNIT></VIEWBY_UNIT>
                      <MANAGED_PROPERTY_VALUES></MANAGED_PROPERTY_VALUES>
                  </PROPERTY>

          putting manually a possible situation in the script will force me to write a wall of scripting
          and i have numerous properties in the xml that the structure are the same as above but only the property_id changes and the labeling values
          so what i need is that it will go trough the file and put <DISPLAY_NAME> value in <CANONICAL_NAME>
          thats pretty much it...

          thanks :)

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: help with cmd script
          « Reply #5 on: April 22, 2010, 01:28:57 PM »
          I had to add a ROOT node to the tree structure. The root node ties the entire file together.

          Quote
          <?xml version="1.0"?>
          <ROOT>
             <PROPERTY>
                <CANONICAL_NAME>Hello</CANONICAL_NAME>
                <CURRENT_STATE>4</CURRENT_STATE>
                <DISPLAY_NAME>Hello</DISPLAY_NAME>
                <FORMAT_STRING></FORMAT_STRING>
                <IS_COMPUTED>1</IS_COMPUTED>
                <IS_MANAGED>0</IS_MANAGED>
                <IS_MULTI_VALUED>0</IS_MULTI_VALUED>
                <IS_PCODE></IS_PCODE>
                <IS_STRING>1</IS_STRING>
                <IS_URI></IS_URI>
                <LOCALE_ID>1</LOCALE_ID>
                <PROPERTY_ID>11111111</PROPERTY_ID>
                <STORAGE_UNIT></STORAGE_UNIT>
                <TIP_TEXT></TIP_TEXT>
                <VALUE_SORT_TYPE></VALUE_SORT_TYPE>
                <VIEWBY_UNIT></VIEWBY_UNIT>
                <MANAGED_PROPERTY_VALUES></MANAGED_PROPERTY_VALUES>
             </PROPERTY>
             <PROPERTY>
                <CANONICAL_NAME>Goodbye</CANONICAL_NAME>
                <CURRENT_STATE>4</CURRENT_STATE>
                <DISPLAY_NAME>Goodbye</DISPLAY_NAME>
                <FORMAT_STRING></FORMAT_STRING>
                <IS_COMPUTED>1</IS_COMPUTED>
                <IS_MANAGED>0</IS_MANAGED>
                <IS_MULTI_VALUED>0</IS_MULTI_VALUED>
                <IS_PCODE></IS_PCODE>
                <IS_STRING>1</IS_STRING>
                <IS_URI></IS_URI>
                <LOCALE_ID>1</LOCALE_ID>
                <PROPERTY_ID>11111111</PROPERTY_ID>
                <STORAGE_UNIT></STORAGE_UNIT>
                <TIP_TEXT></TIP_TEXT>
                <VALUE_SORT_TYPE></VALUE_SORT_TYPE>
                <VIEWBY_UNIT></VIEWBY_UNIT>
                <MANAGED_PROPERTY_VALUES></MANAGED_PROPERTY_VALUES>
             </PROPERTY>
          </ROOT>

          Code: [Select]
          Set xmlDoc = CreateObject("Microsoft.XMLDOM")

          xmlDoc.Async = "False"
          xmlDoc.Load("c:\temp\test.xml")          'Change as needed (input XML file)

          Set colDisplayNodes=xmlDoc.documentElement.selectNodes("/ROOT/PROPERTY/DISPLAY_NAME")
          For Each displayNode in colDisplayNodes
            Set colCanonNodes = xmlDoc.documentElement.selectNodes _
            ("/ROOT/PROPERTY " & _
          "[DISPLAY_NAME = " & "'" & displayNode.text & "'" & "]/CANONICAL_NAME")
            For Each canonNode In colCanonNodes
               canonNode.text = displayNode.text
            Next
          Next

          xmlDoc.Save "c:\temp\Testout.xml"         'Change as needed (output XML file)

          Save the file with a VBS extension and run from the command prompt as cscript scriptname.vbs Can also be run from a cmd file the same way.

          Good luck.  8)

          The XMLDOM object will realign your XML file correctly. No extra charge.
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein