Computer Hope

Computer Hope Forum Welcome, Guest. Please login or register.
November 22, 2009, 03:13:11 PM
Home Help Staff Chat Login Register
News: Come introduce yourself and meet the other forum members.

Computer Hope Forums  >>  Microsoft  >>  Microsoft DOS (Moderator: Computer Hope Admin)  >>  Topic: 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 246 times)
Tigers!
Topic Starter
Rookie
*
Posts: 19

Thanked: 0
OS: Windows Vista
Experience: Beginner




« 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:
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".
I am hoping that the error is a simple syntax error.
Logged
Helpmeh
Expert
*
Posts: 2187

Thanked: 61
OS: Windows XP
Experience: Familiar



Can you see this as it truly is? >< Tell me.


« 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.
Logged


>< If you have this, you can see it. ><
I love 
Tigers!
Topic Starter
Rookie
*
Posts: 19

Thanked: 0
OS: Windows Vista
Experience: Beginner




« 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:
testing_parse_lines_DOS-2.bat 20090220-TESTMOVE.RPT
I wrote a little batch file for testing
Code:
@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?
Logged
BC_Programmer
Sage
*
Posts: 8849

Thanked: 257
OS: Windows XP
Computer: Specs
Experience: Experienced




« Reply #3 on: November 04, 2009, 01:59:16 AM »

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

If this is real life, where the heck is the decimal?
Sidewinder
Guru
*
Posts: 3847

Thanked: 17
OS: Windows XP
Experience: Experienced


« Reply #4 on: November 04, 2009, 07:26:01 AM »

Code:
@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:
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.

 Cool
Logged

"Do one thing at a time, do it very well, and then move on"

                                -Maj. Charles Emerson Winchester III
BC_Programmer
Sage
*
Posts: 8849

Thanked: 257
OS: Windows XP
Computer: Specs
Experience: Experienced




« 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.
Logged

If this is real life, where the heck is the decimal?
Pages: [1] - (Top) Print 
Computer Hope Forums  >>  Microsoft  >>  Microsoft DOS (Moderator: Computer Hope Admin)  >>  Topic: Calling DOS batch file from C programme « previous next »
Jump to:  


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright 1998-2008 by Computer Hope (tm). All rights reserved
Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC
Page created in 0.091 seconds with 18 queries.