Page 1 of 1

CSV rename script

Posted: 16 Apr 2020 18:45
by Elpolloloco
Is there any script to rename files based on 2 columns in a csv or xlsx file? One column actual name second column new name.

Re: CSV rename script

Posted: 16 Apr 2020 19:23
by highend
Not that I am aware of. But it surely doable. But with the spare information here (which columns? should the .csv / .xlsx files be renamed themselves or do these files contain files that should be renamed?) rather not...

Re: CSV rename script

Posted: 16 Apr 2020 19:26
by Elpolloloco
I dont need the csv or xlsx to be renamed, only files that match the columns.

Re: CSV rename script

Posted: 16 Apr 2020 19:29
by highend
A real world example?

Re: CSV rename script

Posted: 16 Apr 2020 19:37
by Elpolloloco
Oldname, newname
File04.pdf,newnamefile04.pdf
0099file.zip,newname.zip

That folder will have many files, i only want the files File04.pdf and 0099file.zip to be renamed to newnamefile04.pdf and newname.zip respectively

Re: CSV rename script

Posted: 16 Apr 2020 20:12
by highend

Code: Select all

    end (gpc(<curitem>, "ext") UnLikeI "csv"), "No .csv file selected, aborted!";

    $srcFiles = "";
    $dstFiles = "";
    $lines    = readfile(<curitem>);
    foreach($line, $lines, <crlf>, "e") {
        $src = gettoken($line, 1, ",", "t");
        if (exists($src) == 1) {
            $srcFiles .= <curpath> . "\" . $src . "|";
            $dstFiles .= <curpath> . "\" . gettoken($line, 2, ",", "t") . "|";
        }
    }
    if ($srcFiles) {
        $srcFiles = trim($srcFiles, "|", "R");
        $dstFiles = trim($dstFiles, "|", "R");
        rename "l", $dstFiles, "p", $srcFiles;
    }

Re: CSV rename script

Posted: 16 Apr 2020 20:21
by Elpolloloco
Wow amazing works perfectly :appl:

Any chance this can be run off a .xlsx file table by specifiyng the columns and table name?

Re: CSV rename script

Posted: 16 Apr 2020 21:54
by highend
In theory? Yeah. You would need to unpack the .xlsx file, read the sharedStrings.xml file, parse it and then do the XY renaming stuff.

Re: CSV rename script

Posted: 16 Apr 2020 21:57
by Elpolloloco
Which means im staying with the csv method :lol:

Re: CSV rename script

Posted: 16 Apr 2020 22:08
by Elpolloloco
So this script is ignoring the first line? I dont need to include a first line, what do i do to modify it so it starts in first line

Re: CSV rename script

Posted: 16 Apr 2020 22:11
by highend
Where do you see any line in the code that makes you think that it ignores the first line the .csv file?

Re: CSV rename script

Posted: 16 Apr 2020 22:14
by Elpolloloco
My bad, i though it does skip header by default