Thread: Calling batch file from C on Vista

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Calling batch file from C on Vista

    Gudday all
    Need to call a batch file from C on my Vista or XP machine (but the code will eventually go on a Win2003/2007 machine).
    Searching through the forums found a few previous threads which had some information but not quite.
    I want to call a batch file from the C programme and pass one parameter from C to the batch.
    The system call is
    Code:
    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:
    @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".
    The batch file and the exe are in the same directory.
    I tried the combination of
    Code:
    system("testing_parse_lines_DOS-2.bat" oldrptname);
    but that throws up a compiler error.
    Looking through other threads mention has been made of setting paths etc: but I am unsure whether that applies to the C or batch or both.
    I am hoping that the error is a simple syntax error. Perhaps the way I pass the variable from C to the batch or the way I express it in the batch?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you need to create a string (using one of the many string-making tools at your disposal) that contains the command you want to run.

  3. #3
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    So you need to create a string (using one of the many string-making tools at your disposal) that contains the command you want to run.
    I don't fully understand what you mean. I am trying to run a batch file and pass a parameter to it (if possible).
    What do you mean by 'contains the command'?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "contains the command" means "contains the command". You want a string that contains the command you wish to run. If you want to run the command "jimbob" then your string needs to contain "jimbob". And so on.

  5. #5
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Tabstop
    I have tried the following
    Code:
    char DOScommand[50];  //command to be sent to batch file
    
    sprintf(DOScommand, "testing_parse_lines_DOS-2.bat %a", oldrptname);
    system(DOScommand);
    There is a change from the previous result.
    Instead of renaming the file 'error.dat' as 'oldrptname' it now calls it '0x1.014f70p-1019'.
    It is a change but whether for the better or not I cannot tell.
    I must have a syntax/grammar error staring at me in the face.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you believe %a prints a string? %a prints a floating number in hexadecimal notation, hence your value of 0x1.014f70 raised to the power 2^-1019.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you want to run a replaceable filename from the C program, or do you want to run a bat file from the C program, and have the bat file run the filename (again, replaceable filename)?

    The command line of the program you want to run, does accept a filename, right?

  8. #8
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    QUOTE=Adak;904808]Do you want to run a replaceable filename from the C program, or do you want to run a bat file from the C program, and have the bat file run the filename (again, replaceable filename)?[/QUOTE]

    I want to run the bat file from the C programme passing a filename to it each time it runs. The C programme will be set up to run every hour. (Sometimes it will have nothing to do). The file name it passes to the batch file will differ every hour except for the prefix of .rpt.
    If I can create a string that contains the command and the current .rpt filename and give it to the system () then that will be sufficient.
    But creating a valid string seems to be the issue.
    I tried the sprintf command as is noted in previous posts that it did not seem to work though I syspect that my use of sprintf is suspect.
    Quote Originally Posted by Adak View Post
    The command line of the program you want to run, does accept a filename, right?
    You raise a good point.
    I have made a test batch file
    Code:
    echo %1
    echo finished moving of files
    rename error.dat %1
    That will accept as a parameter as a file name. The calling line is
    Code:
    testing_parse_lines.bat testRPT.dat
    The file called error.dat is renamed testRPT.dat. So that seems to work ok.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can obviously, have filenames with replaceable parameters in bat files - %1 is your filename before the extension, and %%1 could be your filename extension. That's one way to do it.

    On the C side, you can have your filenames built up any way you want to - clearly.

    What I'm not sure of, is whether or how, you can give replaceable filenames to the system() function, and then have DOS / Windows, accept them.

    For your work, I'd use a bat file, just because it's so small, and you are doing exactly the kind of thing that bat files were made to do. C is also a good choice, but I don't know if you can use system() and have replaceable filenames.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Calling a batch script from within a C program

    I am not quite sure what exactly your batch script does, but it is probably not relevant - I guess you are looking for how to dynamically pass parameters to a batch script from within a C program.

    I can show you one way of doing this.

    Here is a batch script - takes two arguments

    file - the file to copy
    folder - the folder to copy to

    When copying, the file name is prepended with current time.


    Code:
    # Script CopyFile.txt
    var str file, folder, time
    set $time = gettime()
    system -s copy /b $file ($folder+"/"+$time+"_"+$file)
    I am assuming this script will be saved in file C:/Scripts/CopyFile.txt.

    Now, here is the C code.

    Code:
    #include <stdio.h>
    #include <system.h>
    int main()
    {
        char command[1024] ;
    
        // Go to directory where repport file is located.
       _chdir("C:/Report/") ;
    
        // Set time to sleep ;
        int stime ;
        stime = 60*60*1000 ;
    
        while (true)
        {
            // Copy file "rpt.dat" to folder "E:/Copied Reports"
            memset(command, 0x00, sizeof(command)) ;
            sprintf(command, "C:/biterScripting/biterScripting.exe \"C:/Scripts/CopyFile.txt\" file(\"%s\") folder(\"%s\")", 
                "rpt.dat",
                "E:/Copied Reports") ;
            system(command) ;
    
            // Sleep for an hour.
            sleep(stime) ;
        }
    }


    The script I wrote above is in biterscripting ( http://www.biterscripting.com ). I used biterscripting because it makes it easier to pass arguments - you don't get into %1 etc. But pure batch script will do also. The above C program will copy file E:/Report/rpt.dat to folder E:/Copied Reports every hour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM