skip to Main Content

I’m trying to make e-certificates to distribute for one of my college events. I used Photoshop to generate batch certificates with different names and roll nos. Now the files are named core_01Data Set 1.jpg, core_02Data Set 2.jpg, etc. In order to distribute them online, they need to renamed and sorted wrt the participant’s name.
I have a .txt file with the participant names, their roll no and college name(in tab delimited format). I need to rename the files according to the content of the .txt file. I have tried my bit but couldn’t go far. Please help.

#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("%sn", 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("%sn", dir->d_name);
            if (strcmp(dir->d_name, buf[i]) == 0)
            {
                rename(dir->d_name, final_name[i]);
                i++;
            }
        }
        closedir(d);
    }
}

LINK TO THE FILES : https://goo.gl/S6B3AB

3

Answers


  1. Only to give you a way to understand what You did wrong see the code below

    #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,"%st%st%s", arr[0][numberOfNames], arr[1][numberOfNames], arr[2][numberOfNames]) == 3) && (numberOfNames<20))
        {
            printf("%s %s %sn", 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("%sn", 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("%sn", buf[i-1]);
        }
    }
    
    Login or Signup to reply.
  2. you can use python if you want

    #!/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])
    
    Login or Signup to reply.
  3. You could use powershell if you want. I think this would work.

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

    Edit: It’s this

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

    part that splits each line of core.txt on tabs and returns the first element. If you want the other columns included as well, try this:

    $names = (gc core.txt)
    $idx = 1
    
    gci core*.jpg | %{
        $newname = ($names[$idx++].trim() -replace "`t+","-") + ".jpg"
        ren $_ $newname
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search