Computer Hope

Microsoft => Microsoft DOS => Topic started by: Tigers! on October 29, 2009, 12:14:59 AM

Title: Calling DOS batch file from C programme
Post by: Tigers! 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.
Title: Re: Calling DOS batch file from C programme
Post by: Helpmeh 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.
Title: Re: Calling DOS batch file from C programme
Post by: Tigers! 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]
Title: Re: Calling DOS batch file from C programme
Post by: BC_Programmer on November 04, 2009, 01:59:16 AM
place the %1 in quotes, as in, "%1".
Title: Re: Calling DOS batch file from C programme
Post by: Sidewinder 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)
Title: Re: Calling DOS batch file from C programme
Post by: BC_Programmer 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.