【问题标题】:How to use the contents of a text file(tab delimited format) to rename files in a folder?如何使用文本文件的内容(制表符分隔格式)重命名文件夹中的文件?
【发布时间】:2015-09-01 07:52:47
【问题描述】:

我正在尝试制作电子证书以分发给我的一项大学活动。我使用 Photoshop 生成具有不同名称和卷号的批次证书。现在文件被命名为core_01Data Set 1.jpg、core_02Data Set 2.jpg等。为了在线分发它们,它们需要重命名并按照参与者的名字排序. 我有一个 .txt 文件,其中包含参与者姓名、他们的卷号和学院名称(以制表符分隔的格式)。我需要根据 .txt 文件的内容重命名文件。我已经尝试了我的一点,但不能走得太远。请帮忙。

#include<stdio.h>
#include<dirent.h>
int main()
{
    FILE *fp;
    char arr[3][20][40];
    int i=0;

    char final_name[19][50]={0};

    fp = fopen("./new/core.txt", "r");
    while(fgetc(fp)!=EOF)
    {
     while(fgetc(fp)!='\n' && i<20)
     {
        fscanf(fp,"%s%s%s", arr[0][i], arr[1][i], arr[2][i]);
        i++;
     }
    }

    i=1;
    while(i<20)
    {
        strcat(final_name[i-1], arr[0][i]);
        strcat(final_name[i-1], "_");
        strcat(final_name[i-1], arr[1][i]);
        strcat(final_name[i-1], "_");
        strcat(final_name[i-1], arr[2][i]);
        printf("%s\n", final_name[i-1]);
        i++;
    }
    char name[19][50]={0};
    char buf[19][50]={0};

    for(i=1; i<20; i++)
    {
        if(i<10)
        {
            sprintf(buf[i-1], "core_0%d_Data Set %d", i, i);
        }
        else
        {
            sprintf(buf[i-1], "core_%d_Data Set %d", i, i);
        }

        printf("%s...\n", buf[i-1]);
        name[i-1]=buf[i-1];
    }

    i=0;
    DIR *d;
    struct dirent *dir;
    d = opendir("./new/");
    if (d)
    {
        while ((dir = readdir(d)) != NULL )
        {
            printf("%s\n", dir->d_name);
            if (strcmp(dir->d_name, buf[i]) == 0)
            {
                rename(dir->d_name, final_name[i]);
                i++;
            }
        }
        closedir(d);
    }
}

文件链接:https://goo.gl/S6B3AB

【问题讨论】:

  • 为什么还要尝试在 C 中执行此操作?只需使用任何简单的脚本语言。如果不是 Windows,我会建议使用 shell,但仍然可以在 python 或 perl(Windows 存在)之类的东西中轻松做到这一点。我相信 powershell 也可以,虽然我不太熟悉
  • 我对 C 和 C++ 比较熟悉,因此决定与他们一起前进。对程序有帮助吗?

标签: c windows string rename


【解决方案1】:

如果你愿意,你可以使用 powershell。我认为这会奏效。

$names = (gc core.txt)
$idx = 1

gci core*.jpg | %{
    $newname = $names[$idx++].trim().split("`t")[0] + ".jpg"
    ren $_ $newname
}

编辑:就是这个

.trim().split("`t")[0]

在选项卡上拆分 core.txt 的每一行并返回第一个元素的部分。如果您还希望包含其他列,请尝试以下操作:

$names = (gc core.txt)
$idx = 1

gci core*.jpg | %{
    $newname = ($names[$idx++].trim() -replace "`t+","-") + ".jpg"
    ren $_ $newname
}

【讨论】:

  • 非常感谢,我第一次尝试了 Powershell,它成功了。你能告诉我如何将roll no和college name添加到重命名的文件中吗?我是 Powershell 的第一个计时器。这是最新的截图:drive.google.com/…
  • @PeayushBhardwaj 查看编辑。我们现在不是在选项卡上拆分,而是用破折号替换一个或多个连续选项卡的每个实例。如果您使用它自学 PowerShell,我使用的一些命令是速记别名。 gc 表示 get-content。 gci 表示 get-childitems。 % 表示 foreach-item。 ren 表示 rename-item。 PowerShell 的语法与 C 和 C++ 有很大不同,因为它经常将一个命令的结果通过管道传递给另一个命令的输入——例如 get-childitems core*.jpg | foreach-item { do stuff }。
【解决方案2】:

如果你愿意,你可以使用 python

#!/usr/bin/python
import string,os

f=open("in.txt")
a=f.readlines();

for x in a:
        x=x[:-1]
        r=string.split(x,",")
        os.rename(r[0],r[1])

【讨论】:

  • 它简短而容易。 (Y)
  • 这个问题要求用 C 语言而不是 Python 来回答。
  • 这是一个使用 Python 的建议,用户没有说 c 是必需的。此外.. 批准的答案在 powershell 中
【解决方案3】:

只是为了给你一个理解你做错了什么的方法,看下面的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>

int main(int argc, char **argv)
{
    FILE *fp;
    char arr[3][20][40];
    int i=0;
    int numberOfNames = 0;

    fp = fopen("a.txt", "r");

    // read until fscanf can get 3 items from file considering tabs as space between strings
    while((fscanf(fp,"%s\t%s\t%s", arr[0][numberOfNames], arr[1][numberOfNames], arr[2][numberOfNames]) == 3) && (numberOfNames<20))
    {
        printf("%s %s %s\n", arr[0][numberOfNames], arr[1][numberOfNames], arr[2][numberOfNames]);
        numberOfNames++;
    }

    // this init only the first string with null termination
    char final_name[20][50]={0};
    //This init the whole matrix
    memset(final_name, 0, 1000);

    i=0;
    while(i<numberOfNames)
    {
        strcat(final_name[i], arr[0][i]);
        strcat(final_name[i], "_");
        strcat(final_name[i], arr[1][i]);
        strcat(final_name[i], "_");
        strcat(final_name[i], arr[2][i]);
        printf("%s\n", final_name[i]);
        i++;
    }

    char name[19][50]={0};
    char buf[19][50]={0};

    for(i=1; i<=numberOfNames; i++)
    {
        // Using %02d format you grants that the output is always with 2 digit at least and padded with 0
        sprintf(buf[i-1], "core_%02d_Data Set %d", i, i);

        printf("%s...\n", buf[i-1]);
        // sprintf is the way to copy a string into another string
        // You cannot simply name[i-1]=buf[i-1];
        sprintf(name[i-1],buf[i-1]);
        printf("%s\n", buf[i-1]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多