1

このコードを C 言語で書いています。最初にログインしているユーザーを見つけてから、そのユーザーの AppData からいくつかのファイルをコピーする必要があります。ユーザー、パスを生成できますが、Cを使用してフォルダーとそのコンテンツをコピーする方法がわからないため、System()コマンドを使用することを考えました。しかし、COPY コマンドを使用すると、パスが間違っていると表示されますが、CMD で同じコマンドを使用すると、実際には正しく、正常に動作します。また、XCOPY を使用すると、CMD では XCOPY が正常に動作しているのに、コマンドが内部コマンドまたは外部コマンドとして認識されないというメッセージが表示されます。

では、実際にフォルダとそのコンテンツをコピーする方法を誰か教えてもらえますか?

ファイルパスとコピーコマンドを生成するコードの一部を細断しています。

//making path variable
char path[100]; 
strcat(path,"C:\\Users\\");
strcat(path,username); //username is variable it gets value from function

strcat(path,"\\AppData\\Local\\Google\\Chrome\\*.*");
printf(path);


char command[100]; 
strcat(command,"copy ");
strcat(command,path);
strcat(command," D:\\myFolder");
printf("\n");
printf(command);
printf("\n");
system(command);

アップデート

これが私の完全なコードです。誰かがこれを機能させることができますか?

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
#include <Lmcons.h>

int main()

{
//getting current user
TCHAR username[UNLEN+1];
DWORD len = UNLEN+1;
GetUserName(username, &len);
printf(username);
printf("\n");

//making path variable
char path[100]; 
strcpy(path,"C:\\Users\\");
strcat(path,username);

strcat(path,"\\AppData\\Local\\Google\\Chrome\\*.*");
printf(path);




//listing dir
DIR *dfd = opendir(path);
struct dirent *dp;
if(dfd != NULL) {
    while((dp = readdir(dfd)) != NULL)
        printf("%s\n", dp->d_name);
    closedir(dfd);
}

char command[100]; 
strcpy(command,"copy ");
strcat(command,path);
strcat(command," D:\\myFolder\\");
printf("\n");
printf(command);
printf("\n");
//sprintf(command, "copy %s/*.* D:/myfolder",path);
system(command);




return 0;
}
4

1 に答える 1

1

初期化されていない配列を使用しています。

変化する

strcat(path,"C:\\Users\\");
strcat(command,"copy ");

strcpy(path,"C:\\Users\\");
strcpy(command,"copy ");
于 2013-03-25T19:02:07.553 に答える