China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-02 06:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Recommendation] C Language Source Code for Converting BAT to COM View 1,910 Replies 14
Original Poster Posted 2004-08-23 00:00 ·  中国 广东 广州 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Recently I discovered a C language original code that converts BAT files to COM. After compilation, the effect is good. The converted COM file is much smaller than the one converted by BAT2COM, and the execution efficiency is also faster. To respect the original author's copyright, the source program is not listed here. Please visit his website: http://member.netease.com/~gmounto/cpro/BAT2COM.html
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 2 Posted 2004-08-26 00:00 ·  中国 辽宁 朝阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 3 Posted 2006-05-04 21:56 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
A forum user wang6610 reminded me today that the link in post 1 has expired. It might be that the original link was changed or the space was closed because the author didn't pay. Fortunately, I have a backup of the source program, and now I'll post it for everyone to study and research.

/*************************************************************/
/* Program Name: BAT2COM.C 1.50 */
/* Author: Dong Zhanshan */
/* Completion Date: 1993,1995 */
/* Purpose: Convert batch files (.bat) to command files (.COM) */
/* Compilation Method: You can get BAT2COM.COM by compiling and linking with the following commands: */
/* tcc -mt bat2com */
/* tlink c:\tc\lib\c0t+bat2com,bat2com,,c:\tc\lib\cs\lib /t */
/*************************************************************/

#include <stdio.h>
#include <string.h>

/* Generate the header of the command file, including the machine instructions for calling 2EH */
char bat2comhead = {
0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0x0E,0x1F,0x2E,
0x8B,0x0E,0x51,0x01,0xBE,0x51,0x01,0x8B,0xC6,0x50,
0x5B,0x51,0x83,0xC3,0x02,0x8B,0xF3,0x33,0xDB,0x8A,
0x1C,0x53,0x56,0x2E,0x8C,0x16,0x4D,0x01,0x2E,0x89,
0x26,0x4F,0x01,0xCD,0x2E,0x0E,0x1F,0x2E,0x8B,0x26,
0x4F,0x01,0x2E,0x8E,0x16,0x4D,0x01,0x58,0x5B,0x59,
0x03,0xC3,0x50,0x83,0xE9,0x01,0x83,0xF9,0x00,0x75,
0xCD,0xB8,0x00,0x4C,0xCD,0x21,0xC3,0x00,0x00,0x00,
0x00};

int totallength; /* Total length of the generated file */
char buffer; /* Buffer for generating the command file */

/* Function to remove leading meaningless spaces in batch commands */
void removespace(str1)
char *str1;
{
unsigned int i=0;
char *str2;
str2=str1; /* str2 points to the address of str1 */
if ((str1==' ') && (i==0))
{
++str1; /* Increment the address of str1 */
movmem(str1,str2,strlen(str1)+1); /* Remove one leading space */
removespace(str2); /* Recursive call to this function */
}
}

/* Function to change "%%" to "%" in batch commands */
void removedouble(str1)
char *str1;
{
unsigned int j,i=0;
char *str2;
str2=str1; /* str2 points to the address of str1 */
if ((str1=='%') && (str1=='%'))
{
--i;
for (j=0;j<i;j++) {
++str1;++str2;
}
++str1;
movmem(str1,str2,strlen(str1)+1); /* Remove one '%' character */
removedouble(str2); /* Recursive call to this function */
}
}

/* Function to remove the "@" symbol in batch commands */
void removeatchar(str1)
char *str1;
{
char *str2;
str2=str1; /* str2 points to the address of str1 */
if (str1=='@')
{
++str1;
movmem(str1,str2,strlen(str1)+1); /* Remove the character '@' */
}
}

/* Convert all batch commands and write to the buffer */
void transfer(flnm)
char *flnm;
{
unsigned int i, cmnum=0;
char strlen1,ch,str1,*p,*cm;
FILE *txtfl;
if ((txtfl=fopen(flnm,"r"))==NULL) {
printf("Input file is not found !");
exit(0);
}
p = buffer; /* p points to the start address of the buffer */
for (i=0;i<81;++i) *p++=bat2comhead; /* Write the command file header to the buffer */
ch = 0x0d; /* Carriage return character */
totallength = 83; /* Total length of the command file header plus 2 bytes */
for (i=0;i<2;i++) p++; /* Move the p pointer forward by 2 bytes */
do {
fgets(str1,256,txtfl); /* Read a command from the batch file */
removespace(str1); /* Remove leading spaces */
removedouble(str1); /* Change "%%" to "%" */
removeatchar(str1); /* Remove the "@" character */
strlen1 = strlen(str1); /* Calculate the length of the command string */
if (strlen1>2) { /* Is it a valid command? */
++cmnum; /* Increment the command count */
*p++ = strlen1-1; /* Write the command length to the buffer */
++totallength; /* Increment the total length of the command file */
for (i=0;i<strlen1-1;++i) *p++ = str1; /* Write the command string to the buffer */
totallength += strlen1-1; /* Add the command string length to the total length of the command file */
*p++=ch; /* Write a carriage return character */
++totallength; /* Increment the total length of the command file */
strcpy(str1,""); /* Clear the command string content */
}
} while (!feof(txtfl)); /* Is it the end of the file? */
cm = &cmnum; /* Write the command count */
buffer = *cm++;
buffer = *cm;
fclose(txtfl); /* Close the file */
}

/* Write the constructed buffer content to the command file */
void writeBAT2COM(flnm)
char *flnm;
{
FILE *bfl;
unsigned int i;
char drive,dir,name,ext;
fnsplit(flnm,drive,dir,name,ext);
strcat(name,".com");
if ((bfl=fopen(name,"wb"))==NULL) { /* Create and open a binary file */
printf("File does not opened !");
exit(1);
}
fwrite(buffer,totallength,1,bfl); /* Write the content of the buffer */
/* for (i=0;i<totallength;++i) fprintf(bfl,"%c",buffer); */
/* for (i=0;i<totallength;++i) fputc(buffer,bfl); */
fclose(bfl);
}

/* Help function */
void help()
{
printf("\nSyntex : B2C Batch_filename");
exit(0);
}

/* Main function */
main(argc,argv)
int argc;
char *argv;
{
char flnm;
printf("\nB2C Version 1.0 Copyright (c) 1993,94 Dong Zhanshan");
switch (argc) {
case 1 : help();break;
case 2 : strcpy(flnm,argv);break;
default: help();
}
transfer(flnm);
writeBAT2COM(flnm);
}
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 4 Posted 2006-05-05 05:46 ·  中国 山东 菏泽 电信
银牌会员
★★★
Credits 1,246
Posts 488
Joined 2003-11-11 00:00
22-year member
UID 12699
Gender Male
Status Offline
Thank you JonePeng!!!
Floor 5 Posted 2006-05-06 15:24 ·  中国 福建 泉州 电信
高级用户
★★
论坛灌水专业户
Credits 613
Posts 266
Joined 2006-04-19 22:47
20-year member
UID 54113
From 河南省
Status Offline
饮马恒河畔,剑指天山西,碎叶城揽月,库叶岛赏雪,黑海之滨垂钓,贝尔加湖面张弓;中南半岛访古,东京废墟祭祖!
Floor 6 Posted 2006-05-08 07:02 ·  中国 江西 南昌 电信
禁止访问
Credits 97
Posts 41
Joined 2005-10-06 09:15
20-year member
UID 43156
Status Offline
Floor 7 Posted 2006-05-22 21:35 ·  中国 福建 漳州 中移铁通
初级用户
Credits 170
Posts 27
Joined 2005-02-27 00:00
21-year member
UID 36552
Gender Male
Status Offline
Source program list
/*************************************************************/
/* Program name: BAT2COM.C 1.50 */
/* Author: Dong Zhanshan */
/* Completion date: 1993,1995 */
/* Purpose: Convert batch files (.bat) to command files (.COM) */
/* Compilation method: You can get BAT2COM.COM by compiling and linking with the following commands: */
/* tcc -mt bat2com */
/* tlink c:\tc\lib\c0t+bat2com,bat2com,,c:\tc\lib\cs\lib /t */
/*************************************************************/

#include <stdio.h>
#include <string.h>

/* Generate the header of the command file, including the machine instructions for calling 2EH */
char bat2comhead[81] = {
0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0x0E,0x1F,0x2E,
0x8B,0x0E,0x51,0x01,0xBE,0x51,0x01,0x8B,0xC6,0x50,
0x5B,0x51,0x83,0xC3,0x02,0x8B,0xF3,0x33,0xDB,0x8A,
0x1C,0x53,0x56,0x2E,0x8C,0x16,0x4D,0x01,0x2E,0x89,
0x26,0x4F,0x01,0xCD,0x2E,0x0E,0x1F,0x2E,0x8B,0x26,
0x4F,0x01,0x2E,0x8E,0x16,0x4D,0x01,0x58,0x5B,0x59,
0x03,0xC3,0x50,0x83,0xE9,0x01,0x83,0xF9,0x00,0x75,
0xCD,0xB8,0x00,0x4C,0xCD,0x21,0xC3,0x00,0x00,0x00,
0x00};

int totallength; /* Total length of the generated file */
char buffer[10000]; /* Buffer for generating command file */

/* Function to remove meaningless leading spaces in batch commands */
void removespace(str1)
char *str1;
{
unsigned int i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1 == ' ') && (i == 0))
{
++str1; /* str1 address plus 1 */
movmem(str1, str2, strlen(str1) + 1); /* Remove one leading space */
removespace(str2); /* Recursive call to this function */
}
}

/* Function to replace "%%" with "%" in batch commands */
void removedouble(str1)
char *str1;
{
unsigned int j, i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1 == '%') && (str1[++i] == '%'))
{
--i;
for (j = 0; j < i; j++) {
++str1; ++str2;
}
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove one '%' character */
removedouble(str2); /* Recursive call to this function */
}
}

/* Function to remove "@" symbol in batch commands */
void removeatchar(str1)
char *str1;
{
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if (str1[0] == '@')
{
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove character '@' */
}
}

/* Convert all batch commands and write to buffer */
void transfer(flnm)
char *flnm;
{
unsigned int i, cmnum = 0;
char strlen1, ch, str1[256], *p, *cm;
FILE *txtfl;
if ((txtfl = fopen(flnm, "r")) == NULL) {
printf("Input file is not found !");
exit(0);
}
p = buffer; /* p points to the start address of the buffer */
for (i = 0; i < 81; ++i) *p++ = bat2comhead; /* Write command file header to buffer */
ch = 0x0d; /* Carriage return character */
totallength = 83; /* Total length of command file: header plus 2 bytes */
for (i = 0; i < 2; i++) p++; /* Move p pointer forward by 2 bytes */
do {
fgets(str1, 256, txtfl); /* Read a command from the batch file */
removespace(str1); /* Remove leading spaces */
removedouble(str1); /* Replace "%%" with "%" */
removeatchar(str1); /* Remove "@" character */
strlen1 = strlen(str1); /* Calculate command string length */
if (strlen1 > 2) { /* Is it a valid command? */
++cmnum; /* Increment command count */
*p++ = strlen1 - 1; /* Write command length to buffer */
++totallength; /* Increment total length of command file */
for (i = 0; i < strlen1 - 1; ++i) *p++ = str1; /* Write command string to buffer */
totallength += strlen1 - 1; /* Add command string length to total length of command file */
*p++ = ch; /* Write a carriage return character */
++totallength; /* Increment total length of command file */
strcpy(str1, ""); /* Clear command string content */
}
} while (!feof(txtfl)); /* Is it the end of the file? */
cm = &cmnum; /* Write command count */
buffer[81] = *cm++;
buffer[80] = *cm;
fclose(txtfl); /* Close file */
}

/* Write the constructed buffer content to the command file */
void writeBAT2COM(flnm)
char *flnm;
{
FILE *bfl;
unsigned int i;
char drive[3], dir[65], name[9], ext[5];
fnsplit(flnm, drive, dir, name, ext);
strcat(name, ".com");
if ((bfl = fopen(name, "wb")) == NULL) { /* Create and open a binary file */
printf("File does not opened !");
exit(1);
}
fwrite(buffer, totallength, 1, bfl); /* Write buffer content */
/* for (i=0;i<totallength;++i) fprintf(bfl,"%c",buffer); */
/* for (i=0;i<totallength;++i) fputc(buffer,bfl); */
fclose(bfl);
}

/* Help function */
void help()
{
printf("\nSyntex : B2C Batch_filename");
exit(0);
}

/* Main function */
main(argc, argv)
int argc;
char *argv[];
{
char flnm[80];
printf("\nB2C Version 1.0 Copyright (c) 1993,94 Dong Zhanshan");
switch (argc) {
case 1 : help();break;
case 2 : strcpy(flnm, argv[1]);break;
default: help();
}
transfer(flnm);
writeBAT2COM(flnm);
}



Everyone, take a look, is it
Floor 8 Posted 2006-06-14 16:49 ·  中国 海南 海口 电信
新手上路
Credits 10
Posts 5
Joined 2006-06-13 12:45
20-year member
UID 56952
Status Offline
Learning...
:)
Floor 9 Posted 2006-08-13 15:55 ·  中国 山东 菏泽 电信
银牌会员
★★★
Credits 1,246
Posts 488
Joined 2003-11-11 00:00
22-year member
UID 12699
Gender Male
Status Offline
/*************************************************************/
/* Program Name: BAT2COM.C 1.50 */
/* Author: Dong Zhanshan */
/* Completion Date: 1993,1995 */
/* Purpose: Convert batch files (.bat) to command files (.COM)*/
/* Compilation Method: Compile and link with the following commands to get BAT2COM.COM:*/
/* tcc -mt bat2com */
/* tlink c:\tc\lib\c0t+bat2com,bat2com,,c:\tc\lib\cs\lib /t */
/*************************************************************/

#include <stdio.h>
#include <string.h>

/* Generate the header of the command file, including machine instructions to call 2EH */
char bat2comhead[81] = {
0xBB, 0x00, 0x10, 0xB4, 0x4A, 0xCD, 0x21, 0x0E, 0x1F, 0x2E,
0x8B, 0x0E, 0x51, 0x01, 0xBE, 0x51, 0x01, 0x8B, 0xC6, 0x50,
0x5B, 0x51, 0x83, 0xC3, 0x02, 0x8B, 0xF3, 0x33, 0xDB, 0x8A,
0x1C, 0x53, 0x56, 0x2E, 0x8C, 0x16, 0x4D, 0x01, 0x2E, 0x89,
0x26, 0x4F, 0x01, 0xCD, 0x2E, 0x0E, 0x1F, 0x2E, 0x8B, 0x26,
0x4F, 0x01, 0x2E, 0x8E, 0x16, 0x4D, 0x01, 0x58, 0x5B, 0x59,
0x03, 0xC3, 0x50, 0x83, 0xE9, 0x01, 0x83, 0xF9, 0x00, 0x75,
0xCD, 0xB8, 0x00, 0x4C, 0xCD, 0x21, 0xC3, 0x00, 0x00, 0x00,
0x00};

int totallength; /* Total length of the generated file */
char buffer[10000]; /* Buffer for generating the command file */

/* Function to remove leading spaces in batch commands */
void removespace(char *str1)
{
unsigned int i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1[i] == ' ') && (i == 0))
{
++str1; /* Increment the address of str1 */
movmem(str1, str2, strlen(str1) + 1); /* Remove a leading space */
removespace(str2); /* Recursively call this function */
}
}

/* Function to replace "%%" with "%" in batch commands */
void removedouble(char *str1)
{
unsigned int j, i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1[i] == '%') && (str1[++i] == '%'))
{
--i;
for (j = 0; j < i; j++)
{
++str1;
++str2;
}
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove a '%' character */
removedouble(str2); /* Recursively call this function */
}
}

/* Function to remove "@" symbols in batch commands */
void removeatchar(char *str1)
{
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if (str1[0] == '@')
{
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove the '@' character */
}
}

/* Convert all batch commands and write to the buffer */
void transfer(char *flnm)
{
unsigned int i, cmnum = 0;
char strlen1, ch, str1[256], *p, *cm;
FILE *txtfl;
if ((txtfl = fopen(flnm, "r")) == NULL)
{
printf("Input file is not found !");
exit(0);
}
p = buffer; /* p points to the start address of the buffer */
for (i = 0; i < 81; ++i)
*p++ = bat2comhead[i]; /* Write the command file header to the buffer */
ch = 0x0d; /* Carriage return character */
totallength = 83; /* Total length of the command file header plus 2 bytes */
for (i = 0; i < 2; i++)
p++; /* Move the p pointer forward by 2 bytes */
do
{
fgets(str1, 256, txtfl); /* Read a command from the batch file */
removespace(str1); /* Remove leading spaces */
removedouble(str1); /* Replace "%%" with "%" */
removeatchar(str1); /* Remove "@" characters */
strlen1 = strlen(str1); /* Calculate the length of the command string */
if (strlen1 > 2) /* Is it a valid command? */
{
++cmnum; /* Increment the command count by 1 */
*p++ = strlen1 - 1; /* Write the command length to the buffer */
++totallength; /* Increment the total length of the command file by 1 */
for (i = 0; i < strlen1 - 1; ++i)
*p++ = str1[i]; /* Write the command string to the buffer */
totallength += strlen1 - 1; /* Add the command string length to the total length of the command file */
*p++ = ch; /* Write a carriage return character */
++totallength; /* Increment the total length of the command file by 1 */
strcpy(str1, ""); /* Clear the content of the command string */
}
} while (!feof(txtfl)); /* Is it the end of the file? */
cm = &cmnum; /* Write the command count */
buffer[81] = *cm++;
buffer[80] = *cm;
fclose(txtfl); /* Close the file */
}

/* Write the constructed buffer content to the command file */
void writeBAT2COM(char *flnm)
{
FILE *bfl;
unsigned int i;
char drive[3], dir[65], name[9], ext[5];
fnsplit(flnm, drive, dir, name, ext);
strcat(name, ".com");
if ((bfl = fopen(name, "wb")) == NULL)
{ /* Create and open a binary file */
printf("File does not opened !");
exit(1);
}
fwrite(buffer, totallength, 1, bfl); /* Write the content of the buffer */
/* for (i=0;i<totallength;++i) fprintf(bfl,"%c",buffer[i]); */
/* for (i=0;i<totallength;++i) fputc(buffer[i],bfl); */
fclose(bfl);
}

/* Help function */
void help()
{
printf("\nSyntex : B2C Batch_filename");
exit(0);
}

/* Main function */
main(int argc, char *argv[])
{
char flnm[80];
printf("\nB2C Version 1.0 Copyright (c) 1993,94 Dong Zhanshan");
switch (argc)
{
case 1:
help();
break;
case 2:
strcpy(flnm, argv[1]);
break;
default:
help();
}
transfer(flnm);
writeBAT2COM(flnm);
}
Floor 10 Posted 2006-08-15 00:58 ·  中国 江苏 苏州 吴中区 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
The compilation failed. Please ask experts for guidance. Thanks a lot.
Floor 11 Posted 2006-08-16 08:11 ·  中国 福建 厦门 思明区 电信
初级用户
Credits 147
Posts 23
Joined 2005-01-23 00:00
21-year member
UID 35557
Gender Male
Status Offline
Floor 12 Posted 2007-02-08 07:23 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Wonderful!!! Collect~~~
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 13 Posted 2007-02-08 08:13 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
Thanks
Floor 14 Posted 2007-06-25 16:23 ·  中国 山东 菏泽 联通
银牌会员
★★★
Credits 1,246
Posts 488
Joined 2003-11-11 00:00
22-year member
UID 12699
Gender Male
Status Offline
C:\TC>tcc -mt bat2com
Turbo C Version 2.0 Copyright (c) 1987, 1988 Borland International
bat2com.c:
Error bat2com.c 11: Unable to open include file 'stdio.h'
Error bat2com.c 12: Unable to open include file 'string.h'
Error bat2com.c 82: Undefined symbol 'FILE' in function transfer
Error bat2com.c 82: Undefined symbol 'txtfl' in function transfer
Warning bat2com.c 82: Code has no effect in function transfer
Error bat2com.c 83: Undefined symbol 'NULL' in function transfer
Warning bat2com.c 109: Suspicious pointer conversion in function transfer
Error bat2com.c 119: Undefined symbol 'FILE' in function writeBAT2COM
Error bat2com.c 119: Undefined symbol 'bfl' in function writeBAT2COM
Warning bat2com.c 119: Code has no effect in function writeBAT2COM
Error bat2com.c 120: Expression syntax in function writeBAT2COM
Error bat2com.c 121: Expression syntax in function writeBAT2COM
Error bat2com.c 122: Undefined symbol 'drive' in function writeBAT2COM
Error bat2com.c 122: Undefined symbol 'dir' in function writeBAT2COM
Error bat2com.c 122: Undefined symbol 'name' in function writeBAT2COM
Error bat2com.c 122: Undefined symbol 'ext' in function writeBAT2COM
Error bat2com.c 124: Undefined symbol 'NULL' in function writeBAT2COM
*** 14 errors in Compile ***

Available memory 400928
Floor 15 Posted 2007-06-25 17:21 ·  中国 上海 电信
中级用户
★★
Credits 231
Posts 112
Joined 2007-06-19 20:44
19-year member
UID 91827
Gender Male
Status Offline
The compilation environment is incorrect, and stdio.h cannot be found~
Forum Jump: