![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-25 13:22 |
47,813 topics / 349,918 posts / today 0 new / 48,274 members |
| DOS批处理 & 脚本技术(批处理室) » [Recommendation] C Language Source Code for Converting BAT to COM |
| Printable Version 2,039 / 14 |
| Floor1 JonePeng | Posted 2004-08-23 00:00 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
|
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
|
|
| Floor2 Kinglion | Posted 2004-08-26 00:00 |
| 铂金会员 Posts 1,924 Credits 5,798 From 金獅電腦軟體工作室 | |
| Floor3 JonePeng | Posted 2006-05-04 21:56 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
|
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.
|
|
| Floor4 wang6610 | Posted 2006-05-05 05:46 |
| 银牌会员 Posts 488 Credits 1,246 | |
|
Thank you JonePeng!!!
|
|
| Floor5 chineselgs | Posted 2006-05-06 15:24 |
| 高级用户 Posts 266 Credits 613 From 河南省 | |
| Floor6 272922032 | Posted 2006-05-08 07:02 |
| 禁止访问 Posts 41 Credits 97 | |
| Floor7 home | Posted 2006-05-22 21:35 |
| 初级用户 Posts 27 Credits 170 | |
|
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 |
|
| Floor8 qylml | Posted 2006-06-14 16:49 |
| 新手上路 Posts 5 Credits 10 | |
|
Learning...
:) |
|
| Floor9 wang6610 | Posted 2006-08-13 15:55 |
| 银牌会员 Posts 488 Credits 1,246 | |
|
/*************************************************************/
/* 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); } |
|
| Floor10 NaturalJ0 | Posted 2006-08-15 00:58 |
| 银牌会员 Posts 533 Credits 1,181 | |
|
The compilation failed. Please ask experts for guidance. Thanks a lot.
|
|
| Floor11 mornsmile | Posted 2006-08-16 08:11 |
| 初级用户 Posts 23 Credits 147 | |
| Floor12 redtek | Posted 2007-02-08 07:23 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Wonderful!!! Collect~~~
|
|
| Floor13 anqing | Posted 2007-02-08 08:13 |
| 高级用户 Posts 413 Credits 859 | |
|
Thanks
|
|
| Floor14 wang6610 | Posted 2007-06-25 16:23 |
| 银牌会员 Posts 488 Credits 1,246 | |
|
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 |
|
| Floor15 duanml | Posted 2007-06-25 17:21 |
| 中级用户 Posts 112 Credits 231 | |
|
The compilation environment is incorrect, and stdio.h cannot be found~
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |