扫描指定文件夹下的所有文件

example1 扫描当前文件夹下的文件并打印

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
 
char buff[512]; 

void print_dir(char *path, int depth)
{
    struct dirent **name_list;
    int n = scandir(path, &name_list, 0, alphasort);
    if(n < 0)
        printf( "scandir return %d \n",n);
    else
    {
        int index=0;
        while(index < n)
        { 
            sprintf(buff, "name %s/%s", path, name_list[index]->d_name);
            printf("%s\n", buff);
            free(name_list[index++]);
        }
        free(name_list);
    }
}
 
 
int main(int argc, char* argv[])
{
    char *now_dir, pwd[2]=".";
    if (argc != 2)
        now_dir = pwd;
    else
        now_dir = argv[1];	

    printf("Directory scan of %s\n",now_dir);
    print_dir(now_dir,0);
    printf("Finish.\n");
    exit(0);
}

example2 扫描当前文件夹下的文件以及子目录的所有文件及文件类型并打印

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>  
#include <string.h>  
#include <sys/stat.h>  
#include <stdlib.h>  


//dir为目录名,depth为空格个数
//2.在printf中使用,表示用后面的形参替代的位置,实现动态格式输出。
//例如:
//printf("%*s", 10, s);
//意思是输出字符串s,但至少占10个位置,不足的在字符串s左边补空格,这里等同于printf("%10s", s);
void scan_dir(char *dir, int depth) 
{ 

    DIR* dp; 
    struct dirent* entry; 
    struct stat statbuf; 

    if (NULL == (dp = opendir(dir))) //打开dir目录
    { 
        fprintf(stderr, "Can`t open directory %s\n", dir); 
        return ; 
    } 

    chdir(dir);//cd dir 

    while (NULL != (entry = readdir(dp)))
    { 
        lstat(entry->d_name, &statbuf);  // 获取文件名信息
        if (S_ISDIR(statbuf.st_mode))    // 如果为目录
        {  
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)//如果为当前或上一目录,则读取下一个文件   
                continue;    

            printf("%*s%s/ 【file type: %s】\n", depth, "", entry->d_name, "directory file"); 
            scan_dir(entry->d_name, depth+4); //为空格个数

        } 
        else 
        {
            if(S_ISREG(statbuf.st_mode))
	         printf("%*s%s  【file type: %s】\n", depth, "", entry->d_name, "regular file");
            else if (S_ISCHR(statbuf.st_mode))	
                 printf("%*s%s  【file type: %s】\n", depth, "", entry->d_name, "character special file");
            else if (S_ISBLK(statbuf.st_mode))
                 printf("%*s%s  【file type: %s】\n", depth, "", entry->d_name, "block special file");	
            else if (S_ISFIFO(statbuf.st_mode)) 
                printf("%*s%s  【file type: %s】\n", depth, "", entry->d_name, "fifo file");	

        }

    } 

    chdir("..");  // 返回上一路径
    closedir(dp); // 关闭目录   

} 


int main(void) 
{ 
    char topdir[32] = {0}; 

    printf("plese input scan of Directory: ");
    scanf("%s", topdir); 
    printf("Directory scan of %s\n", topdir); 
    scan_dir(topdir, 0); 
    printf("done.\n"); 
    return 0; 
}

example3 扫描当前文件夹下的文件输出文件类型及绝对路径并打印

#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/types.h>
#include <string.h>
#include <sys/stat.h>

int getdir(char * pathname)
{
    DIR* path=NULL;
    path=opendir(pathname);

    if(path==NULL)
    {
        perror("failed");
        exit(1);
    }
    struct dirent* ptr; //目录结构体---属性:目录类型 d_type,  目录名称d_name
    char buf[1024]={0};
    while ((ptr=readdir(path))!=NULL)
    {
        if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)
        {
            continue;
        }
        //如果是目录
        if(ptr->d_type==DT_DIR)
        {

            sprintf(buf,"%s/%s",pathname, ptr->d_name);
            printf("目录:%s\n",buf);
            getdir(buf);
        }
        if (ptr->d_type==DT_REG)
        {
            sprintf(buf,"%s/%s",pathname,ptr->d_name);//把pathname和文件名拼接后放进缓冲字符数组
            printf("文件:%s\n",buf);
        }
    }
    return 0;
}

#define DIR_PATH "/opt/file_list"
int main()
{
    int ret;
    DIR * mydir = NULL;
    if ((mydir = opendir(DIR_PATH)) == NULL)
    {
        ret = mkdir(DIR_PATH, 0755);
        if (ret != 0) 
        {
            printf("%s created erro!\n", DIR_PATH);
            return -1;
        }
            
        printf("%s created sucess!\n", DIR_PATH);
    }
    else
    {
        printf("%s exist!\n", DIR_PATH);
    }

    getdir(DIR_PATH);
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用Java中的FTPClient API来连接FTP服务器,扫描本地文件系统中的文件,并将它们上传到FTP服务器上。 下面是一个简单的示例代码,它可以扫描本地文件系统中的所有文件,并将它们上传到FTP服务器上的指定目录下。 ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FtpClient { public static void main(String[] args) throws IOException { String server = "ftp.example.com"; int port = 21; String user = "username"; String password = "password"; String remoteDirectory = "/remote/directory"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); uploadFiles(new File("local/directory"), remoteDirectory, ftpClient); } finally { ftpClient.logout(); ftpClient.disconnect(); } } private static void uploadFiles(File localDirectory, String remoteDirectory, FTPClient ftpClient) throws IOException { File[] files = localDirectory.listFiles(); for (File file : files) { if (file.isDirectory()) { uploadFiles(file, remoteDirectory + "/" + file.getName(), ftpClient); } else { FileInputStream inputStream = new FileInputStream(file); try { ftpClient.storeFile(remoteDirectory + "/" + file.getName(), inputStream); } finally { inputStream.close(); } } } } } ``` 在此示例中,我们首先创建一个FTP连接并登录到FTP服务器。然后,我们设置FTP客户端的工作模式和文件类型,并调用uploadFiles()方法来遍历本地文件系统中的所有文件并将它们上传到FTP服务器上。 uploadFiles()方法是递归的,它会扫描目录下的所有子目录,并将所有文件上传到FTP服务器上的指定目录下。 请注意,示例代码中使用了Apache Commons Net库中的FTPClient类来处理FTP连接和文件上传操作。你需要将该库添加到你的Java项目中,以便使用它提供的API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天使也有爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值