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

Author Topic: Calling DOS batch file from C programme  (Read 3215 times)

0 Members and 1 Guest are viewing this topic.

Tigers!

    Topic Starter


    Rookie

    Calling DOS batch file from C programme
    « on: October 29, 2009, 12:14:59 AM »
    Has anyone any familiarity with calling batch files from C?
    I want to call a batch file from a C programme and pass one parameter from the C to the batch.
    The system call in the C programme is
    Code: [Select]
    system("testing_parse_lines_DOS-2.bat oldrptname");
    with the parameter being oldrptname. Note that oldrptname is a variable and contains data which will be different every time the C programme is run.
    The batch file is
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=1,2,3,4* delims=\" %%a in (moveTIFF.dat) do (
     set full_line=\%%a\%%b\%%c\%%d
     set file_name=%%d
     echo Full line: !full_line!
     echo File name: !file_name!
     echo.
     copy /b !file_name! !full_line!
     )
     del RPT.dat
     del moveTiff.dat
     rename error.dat %1
    and the parameter passed is %1.
    The problem is that the renaming works up to a point. It changes the file name 'error.dat' to 'oldrptname' rather than the contents of the variable oldrptname. The data in oldrptname will always have the form "xxxxxxxx.RPT".
    I am hoping that the error is a simple syntax error.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Calling DOS batch file from C programme
    « Reply #1 on: October 29, 2009, 04:20:39 AM »
    I don't know C, but I do know that oldrptname in
    system("testing_parse_lines_DOS-2.bat oldrptname");
    is being run as a switch to the batch file as itself, no variable. Perhaps if you show me how to call a variable in C I can help you more.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    Tigers!

      Topic Starter


      Rookie

      Re: Calling DOS batch file from C programme
      « Reply #2 on: November 03, 2009, 02:49:02 PM »
      Scrap any further comments in this post. I solved it. I will now go and salsh my wrists for stupidity!

      I have done a little further work. Currently I am calling the batch file with the new filename present to see how it behaves.
      Code: [Select]
      testing_parse_lines_DOS-2.bat 20090220-TESTMOVE.RPT
      I wrote a little batch file for testing
      Code: [Select]
      @echo off
       echo %1
       echo finished moving of files
       rename error.dat %1
      This batch file will echo the parameter (20090220-TESTMOVE.RPT) send to it and then attempt to change the file name of error.dat to the parameter (20090220-TESTMOVE.RPT). Unfortunately it gives an error message. See attachment
      I am wondering if the error message is related to my use of %1 in the 'rename error.dat %1'? Is the syntax, grammar etc: correct?
      Or is it something else?

      [Saving space, attachment deleted by admin]

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Calling DOS batch file from C programme
      « Reply #3 on: November 04, 2009, 01:59:16 AM »
      place the %1 in quotes, as in, "%1".
      I was trying to dereference Null Pointers before it was cool.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Calling DOS batch file from C programme
      « Reply #4 on: November 04, 2009, 07:26:01 AM »
      Code: [Select]
      @echo off
       echo %1
       echo finished moving of files
       rename error.dat %1

      There is nothing wrong with your code. Works fine. You cannot have duplicate file names in the same directory.File cannot be found could mean the file does not exist (error.dat) or there is a spelling error.

      Code: [Select]
      C:\Temp>dir
       Volume in drive C is xpSystem
       Volume Serial Number is 60D0-12D6

       Directory of C:\Temp

      11/04/2009  09:22 AM    <DIR>          .
      11/04/2009  09:22 AM    <DIR>          ..
      11/04/2009  09:16 AM                 2 error.dat
      11/04/2009  09:16 AM                73 Test.bat
                     2 File(s)             75 bytes
                     2 Dir(s)   1,739,354,112 bytes free

      C:\Temp>test 20090220-TESTMOVE.RPT
      20090220-TESTMOVE.RPT
      finished moving of files

      C:\Temp>dir
       Volume in drive C is xpSystem
       Volume Serial Number is 60D0-12D6

       Directory of C:\Temp

      11/04/2009  09:22 AM    <DIR>          .
      11/04/2009  09:22 AM    <DIR>          ..
      11/04/2009  09:16 AM                 2 20090220-TESTMOVE.RPT
      11/04/2009  09:16 AM                73 Test.bat
                     2 File(s)             75 bytes
                     2 Dir(s)   1,739,354,112 bytes free

      C:\Temp>


      Quote
      place the %1 in quotes, as in, "%1".

      Quotes are only required if a file name has embedded spaces or special characters that would mess up the interpreter. That does not appear to be the case here.

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

      -- Albert Einstein

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Calling DOS batch file from C programme
      « Reply #5 on: November 04, 2009, 07:35:32 AM »
      I was thinking more along hte lines of the hyphen, but I checked, hyphen is acceptable either way.
      I was trying to dereference Null Pointers before it was cool.