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

Author Topic: Open and Reading the file  (Read 38755 times)

0 Members and 1 Guest are viewing this topic.

srujana

  • Guest
Open and Reading the file
« on: August 15, 2007, 10:46:37 PM »
Hi,

 There is a requirement where files have to be renamed and moved to a new location. These files are cognos reports which are stored as two files

 - > One is the actual report
 - > Second is the description file which contains the actual name of the report. This is an XML file.

The description and the report files are stored with a random sequence number as file name.

The requirement is detailed as below:

1. The description file for the corres report has to be opened and browsed for the actual report name.

2. This report name is to be stored in a temporary variable and the report has to be renamed with this name.

3. The renamed file has to be moved from the current location (say C:\sample) to a new location (D:\)

I am trying to create a batch file for the above requirement. I have tried moving the file from one location to the other, but am not aware as how to open and read the XML file contents.

Could you please provide me with suitable DOS commands which will help me achieve the requirement.
Early response would be appreciated.

Thanks in advance,
Srujana

DeltaSlaya



    Apprentice
  • Google
    Re: Open and Reading the file
    « Reply #1 on: August 16, 2007, 12:03:11 AM »
    To open the file you should be able to simply write it's name and whatever it it's extension is assigned to should open it. If this doesn't work you can try writing the path to the executable and then the name of the file you want to open.

    To open a file in notepad manually.

    Quote
    C:\>notepad filename

    If you want to do this as an automated procedure I need more information.

    So firstly, the description file has to be opened ".xml". How would this be distinguished from others? Date? Only one in folder? Modified by Date?

    Secondly the report name has to be obtained from this file. What line is the name on, how many lines are there? Can you post an accurate example?

    Then the name has to be used to rename that same file? I don't understand? Does that actual report have an 'incorrect' name? If so then that needs to be identified too..

    Moving it is easy enough but I don't know what name is going where, please clarify and tell me if the above assumptions are correct. :P
    System specs:
    Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
    ASUS Striker Extreme
    XFX 8600GT XXX Edition
    2x 1gB Corsair XMS2 DDR2-800
    Seagate Barracuda 320gB SATA
    Raidmax Ninja 918 (520W ATXV2.0 PSU)
    -

    ghostdog74



      Specialist

      Thanked: 27
      Re: Open and Reading the file
      « Reply #2 on: August 16, 2007, 02:03:06 AM »
      it is better to show:
      a) example file names that you currently have
      b) things to search for in your files...and what you want to do with it.
      b) examples of expected output

      srujana

      • Guest
      Re: Open and Reading the file
      « Reply #3 on: August 16, 2007, 02:55:26 AM »
      Hi,

       Thanks for your responses. The requirement is explained below:
       
       Aim: To move Cognos reports from one location to other.
       
       The Cognos reports are stored in the Server as "PDF" files with a default name which is actually a random number sequence, for eg., 1325_1834940
       
      Apart from this file, a description file exists which is nothing but an "XML" file. In this XML file, we will find the actual report name, which may be present anywhere in the XML code.

      Now, our task is to open the XML file find that report name, store it in a temporary variable and rename the PDF file with that name.

      After this, the renamed PDF file has to be moved to another location.

      For eg., for a Cognos report  "Rep1_test", two files exist in the current location:

        1325_123467.xml
        1325_123467.pdf

      The XML file will contain the actual name which is "Rep1_test" in the XML code which has to be searched for and with which the PDF file has to be renamed.

      I hope the above explanation is clear, if not let me know.

      Thanks,
      Srujana
       

      DeltaSlaya



        Apprentice
      • Google
        Re: Open and Reading the file
        « Reply #4 on: August 16, 2007, 03:13:33 AM »
        Yes, thats a lot more clear but starting to border on the limits of my ability.

        You haven't answered how it would be decided what xml and pdf files are going to be used in the batch? Is the name stored in a variable or would it be entered, or would it be the most recently modified?

        Also, the actual name has to have something that makes it findable. If it is in a random location it will be impossible to locate and assign to a variable. Is it always on the same line? Does the line it is on have an identifier that will always be the same or what?

        This is what I understand so far:

        Quote
        Find xml / pdf file name. Assign to variable. (need to know what filename to open based on date etc?).
        Find in filename.xml the actual name (need to know how to distinguish this).
        Use actual name as the name to copy the filename.pdf to D:\
        System specs:
        Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
        ASUS Striker Extreme
        XFX 8600GT XXX Edition
        2x 1gB Corsair XMS2 DDR2-800
        Seagate Barracuda 320gB SATA
        Raidmax Ninja 918 (520W ATXV2.0 PSU)
        -

        srujana

        • Guest
        Re: Open and Reading the file
        « Reply #5 on: August 16, 2007, 03:45:25 AM »
        Thanks for a prompt response. Our script would be triggered after a process places the scheduled reports (XML and PDF) in the server. As and when they're placed, our script should perform the required tasks and place the renamed PDF file in the desired location.

        Hence, our script will have XML and PDF files as input parameters (generic) and PDF file as output.

        Regarding the search criteria of the name, the report name would be found after the second "@name" statement in the xml code. The code is given below:

         <reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1']</reportSearchPath>
          <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of Report_test1']</reportViewSearchPath>

         
        Thanks .....

        ghostdog74



          Specialist

          Thanked: 27
          Re: Open and Reading the file
          « Reply #6 on: August 16, 2007, 04:20:45 AM »
          Code: [Select]
          Option Explicit
          Dim objFSO,objFile,line,strToFind
          Dim myFiles, srcFolder, dstFile
          Dim pdfBase,xmlBase,i
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          srcFolder="c:\temp" 'Server location
          strToFind = "Rep1_test" 'Enter string you want to find'
          dstFile = "c:\temp\"&strToFind 'this is file to rename to
          Dim pdfStore(),pdfFullStore()
          i=0
          For Each myFiles In objFSO.GetFolder(srcFolder).Files
          If objFSO.GetExtensionName(myFiles) = "pdf" Then
          pdfBase = objFSO.GetBaseName(myFiles)
          ReDim Preserve pdfStore(i)
          ReDim Preserve pdfFullStore(i)
          pdfStore(i)=pdfBase
          pdfFullStore(i)=myFiles
          i=i+1
          End If
          Next
          For Each myFiles In objFSO.GetFolder(srcFolder).Files
          If objFSO.GetExtensionName(myFiles) = "xml" Then
          xmlBase = objFSO.GetBaseName(myFiles)
          For i=LBound(pdfStore) To UBound(pdfStore)
          If pdfStore(i) = xmlBase Then
          WScript.Echo "found " , xmlBase, pdfStore(i)
          Set objFile=objFSO.OpenTextFile(myFiles,1)
          Do Until objFile.AtEndOfLine
              line=objFile.ReadLine
          If InStr(1, line, strToFind) >0 Then
          WScript.Echo "found repl_test ", line
          WScript.Echo "Renaming pdf ..."
          objFSO.MoveFile pdfFullStore(i),dstFile&".pdf"
          End If
          Loop
          Set objFile=Nothing
          End If
          Next
          End If
          Next
          save as myscript.vbs and run from command line , cscript /nologo myscript.vbs.

          srujana

          • Guest
          Re: Open and Reading the file
          « Reply #7 on: August 16, 2007, 04:25:22 AM »
          Thank you for the code. I shall try and get back to you in case of any clarifications.

           :)

          DeltaSlaya



            Apprentice
          • Google
            Re: Open and Reading the file
            « Reply #8 on: August 16, 2007, 04:41:03 AM »
            If that doesn't work and / or you'd like a 100% automated batch script I STILL would need to know "How to decide what .xml / .pdf file to perform the actions on, is it based on last modified etc. or would it just be the only one in the folder?".

            ghostdog74's code, I believe requires manual input each time, as I believe the actual report name in the xml file would change.

            Quote
            strToFind = "Rep1_test" 'Enter string you want to find'
            dstFile = "c:\temp\"&strToFind 'this is file to rename to
            If I'm correct this means you have to know what you're looking for to find it, I believe you would like that process to be automated.

            Please answer the questions and I'm sure ghostdog will also improve his script if indeed that is required?
            System specs:
            Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
            ASUS Striker Extreme
            XFX 8600GT XXX Edition
            2x 1gB Corsair XMS2 DDR2-800
            Seagate Barracuda 320gB SATA
            Raidmax Ninja 918 (520W ATXV2.0 PSU)
            -

            srujana

            • Guest
            Re: Open and Reading the file
            « Reply #9 on: August 16, 2007, 04:48:03 AM »
            As mentioned previosuly, the search criteria is not fixed. In the sense, it is not static and keeps changing as any report can be placed in the server at any time.

            Hence, strToFind = "Rep1_test" 'Enter string you want to find'
            statement has to be generic in nature.

            The report name is present after the second "@name" statement in the xml code. See the below code again:

            <reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1']</reportSearchPath>
              <reportViewSearchPath>/content/folder[@name='COGNOS-DEV Team']/reportView[@name='Report View of Report_test1']</reportViewSearchPath>

            Thanks.

            DeltaSlaya



              Apprentice
            • Google
              Re: Open and Reading the file
              « Reply #10 on: August 16, 2007, 04:50:47 AM »
              If that doesn't work and / or you'd like a 100% automated batch script I STILL would need to know "How to decide what .xml / .pdf file to perform the actions on, is it based on last modified etc. or would it just be the only one in the folder?".

              ghostdog74's code, I believe requires manual input each time, as I believe the actual report name in the xml file would change.

              Quote
              strToFind = "Rep1_test" 'Enter string you want to find'
              dstFile = "c:\temp\"&strToFind 'this is file to rename to
              If I'm correct this means you have to know what you're looking for to find it, I believe you would like that process to be automated.

              Please answer the questions and I'm sure ghostdog will also improve his script if indeed that is required?

              YES, I mentioned all that, now if you wouldn't mind please answer the question, cheers.
              System specs:
              Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
              ASUS Striker Extreme
              XFX 8600GT XXX Edition
              2x 1gB Corsair XMS2 DDR2-800
              Seagate Barracuda 320gB SATA
              Raidmax Ninja 918 (520W ATXV2.0 PSU)
              -

              srujana

              • Guest
              Re: Open and Reading the file
              « Reply #11 on: August 16, 2007, 05:09:13 AM »
              If that doesn't work and / or you'd like a 100% automated batch script I STILL would need to know "How to decide what .xml / .pdf file to perform the actions on, is it based on last modified etc. or would it just be the only one in the folder?".

              ghostdog74's code, I believe requires manual input each time, as I believe the actual report name in the xml file would change.

              Quote
              strToFind = "Rep1_test" 'Enter string you want to find'
              dstFile = "c:\temp\"&strToFind 'this is file to rename to
              If I'm correct this means you have to know what you're looking for to find it, I believe you would like that process to be automated.

              Please answer the questions and I'm sure ghostdog will also improve his script if indeed that is required?

              YES, I mentioned all that, now if you wouldn't mind please answer the question, cheers.


              Those .xml / .pdf files are considered which are placed in the server by a process.
              Hence, these files can be more than one in number.
              For eg., if two reports have been scheduled a process places a '.xml /.pdf ' for each of the reports in the server. 
              Hence, our trigger point would be the last modified date/time of the folder.
              So, every time a report is scheduled and placed this script has to be invoked.
              So, if the process runs 100times in a day, this script is also invoked those many times.

              I hope this answers your question... let me know in case of further clarity.

              DeltaSlaya



                Apprentice
              • Google
                Re: Open and Reading the file
                « Reply #12 on: August 16, 2007, 05:23:36 AM »
                Quote
                @echo off

                cd C:\
                cls

                :input
                set input=

                :: Check for xmls
                if not exist "*.xml" (
                   echo No .xml files.
                   pause >nul
                   exit
                )

                :: Set input to last .xml in dir
                for /f "usebackq delims=" %%I in (`dir /b /a:-d /o:-d *.xml`) do (
                   set input=%%~nI
                   goto output
                )

                :output

                :: Grab 'actual' name of pdf from xml
                set output=
                for /f "usebackq tokens=4 delims='" %%I in ("%input%.xml") do (
                   set output=%%I
                   goto copy
                )

                :copy
                copy "%input%.pdf" "D:\%output%.pdf"
                pause

                That should work. Grabs filename (without ext) of newest xml (gives error if none exist) then looks in %input%.xml, using tokens finds the actual name. Then setting this name as the output it copies %input%.pdf to D:\%output%.pdf. I believe that is correct?

                Input folder has been set as C: in example, please change only the "CD ..." line to go to the INPUT directory.
                Output folder is D:\.

                Tell me first what the folders should be but otherwise that worked fine for me with your example xml file and a pdf file with same name.
                System specs:
                Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
                ASUS Striker Extreme
                XFX 8600GT XXX Edition
                2x 1gB Corsair XMS2 DDR2-800
                Seagate Barracuda 320gB SATA
                Raidmax Ninja 918 (520W ATXV2.0 PSU)
                -

                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: Open and Reading the file
                  « Reply #13 on: August 16, 2007, 08:52:42 AM »
                  it certainly pays to describe your problem as clearly as possible.
                  Code: [Select]
                  Option Explicit
                  On Error Resume Next
                  Dim objFSO,objFile,objRE,colMatches,oMatches
                  Dim myFiles, srcFolder, dstFolder,dstFile,line,strToFind,strFileName
                  Dim pdfBase,xmlBase,i,strFileContents
                  Dim pdfStore(),pdfFullStore() 'define some array to store paths
                  Set objFSO = CreateObject("Scripting.FileSystemObject")
                  ' Create regexp
                  Set objRE = New RegExp
                  objRE.Global     = True
                  objRE.IgnoreCase = False
                  objRE.Pattern    = "\[@name=(.*?)\]" 'pattern to search for
                  srcFolder="c:\temp" 'Server source folder location
                  dstFolder="c:\temp" 'Destination Folder as desired
                  i=0 'array counter
                  For Each myFiles In objFSO.GetFolder(srcFolder).Files
                  If objFSO.GetExtensionName(myFiles) = "pdf" Then
                  pdfBase = objFSO.GetBaseName(myFiles)
                  ReDim Preserve pdfStore(i)
                  ReDim Preserve pdfFullStore(i)
                  pdfStore(i)=pdfBase
                  pdfFullStore(i)=myFiles
                  i=i+1
                  End If
                  Next
                  For Each myFiles In objFSO.GetFolder(srcFolder).Files
                  If objFSO.GetExtensionName(myFiles) = "xml" Then
                  xmlBase = objFSO.GetBaseName(myFiles)
                  For i=LBound(pdfStore) To UBound(pdfStore)
                  If pdfStore(i) = xmlBase Then
                  WScript.Echo "found " , xmlBase, pdfStore(i)
                  Set objFile=objFSO.OpenTextFile(myFiles,1)
                  strFileContents = objFile.ReadAll
                  objFile.Close
                  Set colMatches = objRE.Execute(strFileContents)
                      Set oMatches = colMatches(1)
                      If Len(oMatches) = 0 Then
                      WScript.Echo "@name not found"
                      Else
                      strToFind = Replace(oMatches,"[@name='","")
                      strToFind = Replace(strToFind,"']","")
                      WScript.Echo "The string to replace is ",strToFind
                      dstFile = dstFolder&"\"&strToFind
                      WScript.Echo dstFile
                      WScript.Echo "Renaming pdf ..."
                  objFSO.MoveFile pdfFullStore(i),dstFile&".pdf"
                      End If
                      Set objFile=Nothing
                  Set oMatches=Nothing
                  Set colMatches=Nothing
                  End If
                  Next
                  End If
                  Next

                  DeltaSlaya



                    Apprentice
                  • Google
                    Re: Open and Reading the file
                    « Reply #14 on: August 16, 2007, 02:02:04 PM »
                    There, you have two options, I don't know how similar in actions ghostdog's is to mine. Though mine definitely has less code and looks easier to follow.

                    I did make one assumption though.

                    Quote
                    <reportSearchPath>/content/package[@name='Provider']/report[@name='Report_test1']</reportSearchPath>

                    That all four of those " ' " apostrophes will always be on the FIRST line in the xml, with the real report name within the third and fourth. I chose to do it this way because another way the apostrophes would have been captured in the variable.
                    System specs:
                    Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
                    ASUS Striker Extreme
                    XFX 8600GT XXX Edition
                    2x 1gB Corsair XMS2 DDR2-800
                    Seagate Barracuda 320gB SATA
                    Raidmax Ninja 918 (520W ATXV2.0 PSU)
                    -