Home / Microsoft / Microsoft DOS / Calling DOS batch file from C programme
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Calling DOS batch file from C programme  (Read 653 times)
Tigers!
Topic Starter
Rookie



Posts: 29


« 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.
IP logged
Helpmeh
Egghead



Thanked: 116
Posts: 3,583

Experience: Experienced
OS: Windows XP


Roar.

1
« 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.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
Tigers!
Topic Starter
Rookie



Posts: 29


« Reply #2 on: November 03, 2009, 02:49:02 PM »

[size=10pt]Scrap any further comments in this post. I solved it. I will now go and salsh my wrists for stupidity![/size]

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]
IP logged
BC_Programmer
Mastermind


Thanked: 682
Posts: 15,624

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #3 on: November 04, 2009, 01:59:16 AM »

place the %1 in quotes, as in, "%1".
IP logged

Sidewinder
Guru



Thanked: 89
Posts: 4,315

Experience: Experienced
OS: Windows XP

« 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)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
BC_Programmer
Mastermind


Thanked: 682
Posts: 15,624

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« 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.
IP logged

Pages: [1] - (Top) Print 
Home / Microsoft / Microsoft DOS / Calling DOS batch file from C programme « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.088 seconds with 20 queries.