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

Author Topic: Writing the results of a DOS program to a file  (Read 9697 times)

0 Members and 1 Guest are viewing this topic.

oba_shonowo

    Topic Starter


    Greenhorn

    Writing the results of a DOS program to a file
    « on: May 15, 2008, 03:13:29 AM »
    How can I write the results of my DOS command to a file or event viewer?

    Dias de verano

    • Guest
    Re: Writing the results of a DOS program to a file
    « Reply #1 on: May 15, 2008, 05:59:42 AM »
    use the redirection symbols > and >>

    > creates a file, overwriting forever any file with the same name

    >> creates a file if it does not exist or appends to one if it already exists.

    dir *.jpg > pictures.txt
    dir *.bmp >> pictures.txt



    « Last Edit: May 15, 2008, 08:20:41 AM by Dias de verano »

    macdad-



      Expert

      Thanked: 40
      Re: Writing the results of a DOS program to a file
      « Reply #2 on: May 15, 2008, 06:04:39 AM »
      that will work but it wont put in the commands that were on the line with the pipe
      so if u tried this
      Code: [Select]
      echo this is a test > log.txt.
      your output on log.txt would be
       
      Quote
      this is a test
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      Carbon Dudeoxide

      • Global Moderator

      • Mastermind
      • Thanked: 169
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Experience: Guru
      • OS: Mac OS
      Re: Writing the results of a DOS program to a file
      « Reply #3 on: May 15, 2008, 06:11:11 AM »
      Here is another example:

      Code: [Select]
      dir C:\>>"C:\Documents and Settings\user\Desktop\file.txt"
      Quote from: File.txt
      Volume in drive C has no label.
       Volume Serial Number is 2C71-BE3D

       Directory of C:\

      13-Jan-08  12:57 PM                 0 AUTOEXEC.BAT
      19-Nov-07  10:27 PM                 0 AutoRun.inf
      05-May-08  06:23 PM    <DIR>          Config.Msi
      13-Jan-08  12:57 PM                 0 CONFIG.SYS
      27-Feb-08  11:15 PM    <DIR>          Documents and Settings
      13-Jan-08  01:03 PM    <DIR>          Drivers
      15-May-08  07:58 PM    <DIR>          Program Files
      15-May-08  07:15 AM               520 RTHDCPL_Dump.txt
      15-May-08  07:15 AM    <DIR>          WINDOWS
                     7 File(s)          2,721 bytes
                     8 Dir(s)  49,978,236,928 bytes free


      Dias de verano

      • Guest
      Re: Writing the results of a DOS program to a file
      « Reply #4 on: May 15, 2008, 08:19:05 AM »
      that will work but it wont put in the commands that were on the line with the pipe
      so if u tried this
      Code: [Select]
      echo this is a test > log.txt.
      your output on log.txt would be
       
      Quote
      this is a test

      he wrote

      Quote
      How can I write the results of my DOS command

      macdad-



        Expert

        Thanked: 40
        Re: Writing the results of a DOS program to a file
        « Reply #5 on: May 15, 2008, 01:58:25 PM »
        yes Dias the code will output the results of the commands but not the commands themselves even if u use echo on.
        If you dont know DOS, you dont know Windows...

        Thats why Bill Gates created the Windows NT Family.

        Dias de verano

        • Guest
        Re: Writing the results of a DOS program to a file
        « Reply #6 on: May 15, 2008, 02:00:39 PM »
        I know.  ::) You told him about how to show the commands, I was telling you that is not what he asked for. He asked to see the results.


        macdad-



          Expert

          Thanked: 40
          Re: Writing the results of a DOS program to a file
          « Reply #7 on: May 15, 2008, 02:09:59 PM »
          ok and the pipe deal will work.
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          Dias de verano

          • Guest
          Re: Writing the results of a DOS program to a file
          « Reply #8 on: May 15, 2008, 02:14:25 PM »
          What "pipe"?

          This is a pipe symbol |
          This is a redirection symbol >

          macdad-



            Expert

            Thanked: 40
            Re: Writing the results of a DOS program to a file
            « Reply #9 on: May 15, 2008, 02:44:23 PM »
            srry i ment the redirection
            If you dont know DOS, you dont know Windows...

            Thats why Bill Gates created the Windows NT Family.

            oba_shonowo

              Topic Starter


              Greenhorn

              Re: Writing the results of a DOS program to a file
              « Reply #10 on: May 16, 2008, 04:22:18 AM »
              Thanks alot everybody. However, I was hoping to write the results of a line of code to a file. Let me be more specific. I have this line of code:-

              for /f "tokens=* delims=" %%i in ('dir /o:-d /s /b F:\sqldata\MSSQL\BACKUP\master\*.BAK') do (
                 copy %%i "\\C:\master"
                 goto endmstr1
                 )
              :endmstr1

              for /f "tokens=* delims=" %%i in ('dir /o:d /s /b Z:\master\*.*') do (
                 del %%i
                 goto endmstr2
                 )
              :endmstr2

              How do I get the results of this program to show in a file? I have it scheduled to run long before I get to the office.

              Dias de verano

              • Guest
              Re: Writing the results of a DOS program to a file
              « Reply #11 on: May 16, 2008, 04:46:39 AM »
              That is not "a line of code". That is several lines (plural) of code.

              try your.bat > your.txt

              macdad-



                Expert

                Thanked: 40
                Re: Writing the results of a DOS program to a file
                « Reply #12 on: May 16, 2008, 05:31:13 AM »
                yes, for every line you want outputted to a file you would try this

                Code: [Select]
                echo off
                cls
                echo this is a test > test.txt
                pause >> test.txt
                echo hello >> test.txt

                that code would output to this on the txt file:

                this is a test
                Press any key to continue...
                hello

                If you dont know DOS, you dont know Windows...

                Thats why Bill Gates created the Windows NT Family.

                Dias de verano

                • Guest
                Re: Writing the results of a DOS program to a file
                « Reply #13 on: May 16, 2008, 09:36:44 AM »
                Thanks alot everybody. However, I was hoping to write the results of a line of code to a file. Let me be more specific. I have this line of code:-

                for /f "tokens=* delims=" %%i in ('dir /o:-d /s /b F:\sqldata\MSSQL\BACKUP\master\*.BAK') do (
                   copy %%i "\\C:\master"
                   goto endmstr1
                   )
                :endmstr1

                for /f "tokens=* delims=" %%i in ('dir /o:d /s /b Z:\master\*.*') do (
                   del %%i
                   goto endmstr2
                   )
                :endmstr2

                How do I get the results of this program to show in a file? I have it scheduled to run long before I get to the office.

                That code is crazy! Why are you going to the labels?

                macdad-



                  Expert

                  Thanked: 40
                  Re: Writing the results of a DOS program to a file
                  « Reply #14 on: May 16, 2008, 02:00:28 PM »
                  oba wouldnt it be simpler and easier if you tried this ;)

                  Code: [Select]
                  echo off
                  cls
                  echo this is a test > test.txt
                  pause >> test.txt
                  echo hello >> test.txt
                  If you dont know DOS, you dont know Windows...

                  Thats why Bill Gates created the Windows NT Family.

                  Dias de verano

                  • Guest
                  Re: Writing the results of a DOS program to a file
                  « Reply #15 on: May 16, 2008, 02:04:25 PM »
                  macdad, I don't think you understand what oba is trying to do.

                  macdad-



                    Expert

                    Thanked: 40
                    Re: Writing the results of a DOS program to a file
                    « Reply #16 on: May 16, 2008, 02:17:25 PM »
                    yes i dont know wat oba wants accomplished here

                    Code: [Select]
                    for /f "tokens=* delims=" %%i in ('dir /o:-d /s /b F:\sqldata\MSSQL\BACKUP\master\*.BAK') do (
                       copy %%i "\\C:\master"
                       goto endmstr1
                       )
                    :endmstr1

                    for /f "tokens=* delims=" %%i in ('dir /o:d /s /b Z:\master\*.*') do (
                       del %%i
                       goto endmstr2
                       )
                    :endmstr2
                    If you dont know DOS, you dont know Windows...

                    Thats why Bill Gates created the Windows NT Family.

                    Dias de verano

                    • Guest
                    Re: Writing the results of a DOS program to a file
                    « Reply #17 on: May 16, 2008, 04:45:46 PM »
                    yes i dont know wat oba wants accomplished here

                    In this code, he is looking into the folder F:\sqldata\MSSQL\BACKUP\master\ and all of its subfolders, and copying any .BAK file that may be there to the folder C:\master.

                    He is making dir sort its output in reverse date order, why, I do not know.

                    Code: [Select]
                    for /f "tokens=* delims=" %%i in ('dir /o:-d /s /b F:\sqldata\MSSQL\BACKUP\master\*.BAK') do (
                       copy %%i "\\C:\master"
                       goto endmstr1
                       )
                    :endmstr1

                    Here, he is deleting every file in every subdirectory below the directory Z:\master\. This time the dir output is sorted in date order, again, I do not know why.

                    Code: [Select]
                    for /f "tokens=* delims=" %%i in ('dir /o:d /s /b Z:\master\*.*') do (
                       del %%i
                       goto endmstr2
                       )
                    :endmstr2

                    His eccentric use of labels is intriguing.

                    These 2 pieces are exactly equivalent to the above quoted code sections.

                    Code: [Select]
                    for /f "tokens=* delims=" %%i in ('dir /o:-d /s /b F:\sqldata\MSSQL\BACKUP\master\*.BAK') do copy %%i "\\C:\master"
                    Code: [Select]
                    for /f "tokens=* delims=" %%i in ('dir /o:d /s /b Z:\master\*.*') do del %%i

                    macdad-



                      Expert

                      Thanked: 40
                      Re: Writing the results of a DOS program to a file
                      « Reply #18 on: May 16, 2008, 08:06:27 PM »
                      thanks Dias, i always get lost in the For commands
                      If you dont know DOS, you dont know Windows...

                      Thats why Bill Gates created the Windows NT Family.

                      oba_shonowo

                        Topic Starter


                        Greenhorn

                        Re: Writing the results of a DOS program to a file
                        « Reply #19 on: May 20, 2008, 08:02:48 AM »
                        Thanks for all the input. I have the log I want now. I simple run a batch file that echoes the first batch file's input to a log file.