/*
文件名:cpyimg.cpp
平台: win2k sp4+vc6
作者:vicallee
版本:1.0
完成日期:2004-9-10
描述:1.
把当前目录下的六个文件复制到指定目录下的某些目录,
复制的条件是:此目录下有001.HTM.
2.修改001.htm和top.htm,把它们中的"../"变为" ./"
3.把top.htm中的<td><img src=" ./images/new_gz开头的部分,它后面的两个字母改为dl
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
const char *mFile[] = {"\\a1.jpg", "\\new_01.gif",
"\\new_02.gif", "\\new_03.gif",
"\\new_gzdl.gif", "\\tb.jpg"};
typedef struct sstack{
char *path;
struct sstack *next;
}mystack;
static mystack *head = NULL;
void push(const char *path);
bool pop(char *s);
bool cpfile(char *dest, char *src);
bool modify(char *filename);
int main(int argc, char* argv[])
{
char path[MAX_PATH], pathcurr[MAX_PATH], imgpath[MAX_PATH];
WIN32_FIND_DATA sr;
HANDLE hFind;
if (argc < 2 )
{
printf("请带上目录名作参数!\n");
return 1;
}
push(strcat(strcpy(path, argv[1]), "\\"));
while(pop(path))
{
strcat(strcpy(pathcurr, path), "*.*");
if ((hFind = FindFirstFile(pathcurr, &sr)) == INVALID_HANDLE_VALUE)
{
puts("搜索过程中出错,文件夹并不存在!");
continue;
}
do
{
if ((strcmp(sr.cFileName, ".") == 0)
|| (strcmp(sr.cFileName, "..") == 0))
{
continue;
}
if ((sr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{/*若为目录*/
strcat(strcat(strcpy(pathcurr, path), sr.cFileName), "\\001.htm");
if (GetFileAttributes(pathcurr) != 0xFFFFFFFF)
{/*是否有001.htm?*/
modify(pathcurr);/*修改001.htm*/
if (GetFileAttributes(strcat(strcat(strcpy(pathcurr, path),
sr.cFileName), "\\top.htm")) != 0xFFFFFFFF)
{/*修改top.htm*/
modify(pathcurr);
}
if (GetFileAttributes(strcat(strcat(strcpy(pathcurr, path),
sr.cFileName), "\\images")) == 0xFFFFFFFF)
{/*目录不存在则创建*/
CreateDirectory(pathcurr, NULL);
}
if ( GetFileAttributes(pathcurr) != 0xFFFFFFFF)
{/*复制文件条件成立*/
GetModuleFileName(NULL, imgpath, sizeof(imgpath));
*(strrchr(imgpath, '\\') + 1) = '\0';
if (cpfile(pathcurr, imgpath))
{/*复制文件成功*/
printf("已复制%s---->%s\n", imgpath, pathcurr);
}
}
}
else
{/*此目录下并无001.htm*/
strcpy(pathcurr, path);
strcat(strcat(pathcurr, sr.cFileName), "\\");
push(pathcurr);
puts(pathcurr);
}
}
else
{/*是文件,检查*/
strcat(strcpy(pathcurr, path), sr.cFileName);
}
}while(FindNextFile(hFind, &sr) != 0);
FindClose(hFind);
}/*End while*/
printf("执行完毕!\n");
scanf("%*c");
return 0;
}/*end main*/
void push(const char *path)
{
mystack *p = (mystack *)malloc(sizeof(mystack));
p->path = (char *)malloc(strlen(path) + 1);
strcpy(p->path, path);
p->next = head;
head = p;
}
bool pop(char *s)
{
mystack *p = head;
if(p != NULL)
{
strcpy(s, p->path);
head = head->next;
free(p);
return true;
}
return false;
}
bool cpfile(char *dest, char *src)
{
int i;
int srcpos = strlen(src);
int destpos = strlen(dest);
for (i = 0; mFile[i] != NULL; i++)
{
src[srcpos] = '\0';
dest[destpos] = '\0';
if (CopyFile(strcat(src, mFile[i]), strcat(dest, mFile[i]), false) == 0)
{
return false;
}
}/*end for*/
dest[destpos] = src[srcpos] = '\0';
return true;
}
bool modify(char *filename)
{
char buf[BUFSIZ], fbuf[0xFFFF];
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL)
{
return false;
}
memset(fbuf, 0, sizeof(fbuf));
if (strstr(filename, "001.htm") != NULL)
{
while ( fgets(buf, sizeof(buf), fp) != NULL)
{
if (strstr(buf, "<frameset rows=\"*,32\">") != NULL)
{
fgets(buf, sizeof(buf), fp);
strcat(fbuf,buf);
while( strstr(buf, "<noframes>") ==NULL)
fgets(buf, sizeof(buf), fp);
}
strcat(fbuf, buf);
}
freopen(filename, "w", fp);
fputs(fbuf, fp);
freopen(filename,"r",fp);
}
memset(fbuf, 0, sizeof(fbuf));
while ( fgets(buf, sizeof(buf), fp) != NULL)
{
if (strstr(buf, "../") != NULL)
{
*strstr(buf, "../") = ' ';
}
if (strstr(buf, "<td><img src=\" ./images/new_gz") !=
NULL)
{
memcpy(strstr(buf, "<td><img src=\" ./images/new_gz")
+
strlen("<td><img src=\" ./images/new_gz"), "dl",
2);
}
if (strstr(buf, "src=\" ./images/new_04.gif\"") != NULL)
{
fgets(buf, sizeof(buf), fp);
}
strcat(fbuf, buf);
}
freopen(filename, "w", fp);
fputs(fbuf, fp);
fclose(fp);
return true;
}
本文地址:http://com.8s8s.com/it/it27514.htm