Announcement

Collapse
No announcement yet.

Open file, but write to another folder ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Open file, but write to another folder ?

    I want to open a set of files in one folder, append a line of extra info and then write it to another folder, thus keeping the original files unchanged.

    I have been thinking of using the shell and Dos copy statement.
    However I would prefer handling things within PB, is this possible ?


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

  • #2
    Yes, very do-able.

    Show what you have so far and someone will help you tune it up.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Michael,

      After posting my question I found the xcopy statement.
      I am not completely sure about the syntax but would I use this it would perhaps look something like this:

      Code:
      FOR j = 1 TO koersstringnr
      
         IF j = 1 THEN
      
            temp$ = "(c:\xcopy e:\FILES\*.prn " + gebruikkoersenmap$ + "\*.prn/v)"  
            SHELL temp$
      
         END IF
      
         OPEN gebruikkoersenmap$ + convarray2krsnamen$( j ) + ".PRN" FOR APPEND AS #1
      
         PRINT #1, convarray2$( j )
      
         CLOSE #1
      
      NEXT j
      With some trial and error I think it will ultimately work, however probably quite ugly looking.
      Therefore I would very much prefer a smooth PB solution.

      [This message has been edited by Erik Loef (edited January 06, 2006).]

      Comment


      • #4
        Code:
        #COMPILE EXE   'copier.bas
        #DIM ALL
        DECLARE FUNCTION ReadWrite(InputFilename AS STRING,  _
                                   OutputFilename AS STRING, _
                                   AddThis AS STRING) AS LONG
        DECLARE FUNCTION Exist(DirFileName AS STRING) AS LONG  'Semen exist function
        FUNCTION PBMAIN () AS LONG
          LOCAL Inputfilename AS STRING, OutputFileName AS STRING, AddThis AS STRING, result AS LONG
          InputFilename = "c:\keep\go.bat"                           'input file
          OutputFileName = "c:\keep\go.bak"                          'output file
          AddThis = "Now is the time for all good men"               'text to add to output file
        
          result = ReadWrite(InputFileName, OutputFileName, AddThis) '0 = success
        END FUNCTION
        
        FUNCTION ReadWrite(InputFileName AS STRING, OutputFileName AS STRING, AddThis  AS STRING) AS LONG
          LOCAL Buffer AS STRING, hInputFile AS LONG, hOutputFile AS LONG
        
          IF ISFALSE Exist(InputFileName) THEN
             FUNCTION = 53
             BEEP
             EXIT FUNCTION
          END IF
        
          hInputFile = FREEFILE
          OPEN InputFilename$ FOR BINARY ACCESS READ AS #hInputfile
          IF ERR THEN FUNCTION = ERR:EXIT FUNCTION
          GET$ #hInputFile, LOF(hInputFile), buffer$
          CLOSE #hInputFile
        
          hOutPutFile = FREEFILE
          OPEN OutputFileName$ FOR BINARY AS #hOutputFile
          IF ERR THEN FUNCTION = ERR:EXIT FUNCTION
          buffer$ = Buffer$ + AddThis$                               'add new text to buffer
          PUT$ #hOutputFile, Buffer$                                 'write buffer to new file
          IF ERR THEN FUNCTION = ERR:EXIT FUNCTION                   'if err, set, exit function
          'SEEK #hOutputFile, LEN(Buffer$) + 1      'not needed      'position to place end of file marker
          'IF ERR THEN FUNCTION = ERR:EXIT FUNCTION 'not needed      'if err, set, exit function
          SETEOF #hOutputFile                                        'set end of file marker
          IF ERR THEN FUNCTION = ERR:EXIT FUNCTION                   'if err, set, exit function
          CLOSE #hOutputfile                                         'close new file
          IF ERR THEN FUNCTION = ERR:EXIT FUNCTION                   'if err, set, exit function
        END FUNCTION
        
        FUNCTION Exist(DirFileName AS STRING) AS LONG                'Semen exist function
        FUNCTION = GETATTR(DirFileName$): FUNCTION = (ERR = 0)
        END FUNCTION
        ------------------


        [This message has been edited by Mike Doty (edited January 07, 2006).]
        The world is full of apathy, but who cares?

        Comment


        • #5
          Mike,

          Thanks for your code, I am going to study it tomorrow.

          I the meantime I managed to perfect my simplistic code a bit.

          Especially the >null statement seems to hide the output of the xcopy statement.

          Code:
          FOR j = 1 TO koersstringnr
          
             IF j = 1 THEN
          
               temp$ = "xcopy " + bronkoersenmap$ + "*.prn " + gebruikkoersenmap$ + ">null" 
               SHELL temp$
                
             END IF 
          
             OPEN gebruikkoersenmap$ + convarray2krsnamen$( j ) + ".PRN" FOR APPEND AS #1
          
             PRINT #1, convarray2$( j )
          
             CLOSE #1
          
          NEXT j
          ------------------

          Comment


          • #6
            Using SHELL to run DOS commands for the purpose of copying and
            appending files does work, but so does just writing a .BAT file
            to do the same thing.

            Opening a file for input and reading the contents into an
            array string, then appending new contents to the end of that
            array before writing it out to a new file in a different folder
            would be another way.

            Opening a file for input and another file for output and reading
            from one while writing to the other one line at a time would
            also work - you just finish up the output file by writing the
            added content at the end before you close it.

            Opening a file for binary and reading in the whole content in
            one step, then appending the additional content to the end of
            the string before writing it to a new output file would also
            work.

            Opening a file for binary and reading in the whole content in
            one step, then writing it all to the new output file, then
            writing out the added content to it before closing it, would
            also work.

            These are just a few possible approaches, though probably the
            most common ones. How you do it, even why you do it the way
            you chose to do it, are left for the programmer to resolve.

            ------------------
            Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired

            Comment


            • #7
              If I understand what you want to do correctly, you'll want Donald's
              approach of "Opening a file for input and another file for output".

              Binary would change the existing(Unless open seperate like above code),
              so you want to open the originals as INPUT, then assuming you are keeping
              the same names in the other folder open OtherFolderString + OrigFilenameString
              for OUTPUT. Read the original files into the output files, append your data
              and close. You could probably even open them for appending. Binary Reading to
              a string all the existing file would probably be the quickest unless you need to
              process the lines and make sure there are no extra CRLFs, etc.

              ------------------
              If you aim at nothing...you will hit it.



              [This message has been edited by Roger Garstang (edited January 07, 2006).]
              sigpic
              Mobile Solutions
              Sys Analyst and Development

              Comment


              • #8
                I would think it would be better to read the input file
                first to keep the heads from moving back and forth between
                the input file and the output file.


                ------------------
                The world is full of apathy, but who cares?

                Comment


                • #9
                  Code:
                  SEEK #hOutputFile, LEN(Buffer$) + 1  'position to place end of file marker
                  You don't want to do this.

                  The PUT$ automatically locates the file pointer at the proper location for SETEOF.

                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Mike's comment about the heads swinging back and forth prompted
                    me to try putting the two folders on different hard drives.
                    This had great results as it cut one third off the processing time,
                    making it 6 seconds.
                    Not so bad..

                    I also tried to incorporate the code, however I haven't succeeded
                    yet in getting it to work properly for some reason.
                    I will give it another try in the near future.

                    Thank you all very much for your insight giving comments and code.

                    Erik

                    [This message has been edited by Erik Loef (edited January 07, 2006).]

                    Comment

                    Working...
                    X