PDA

View Full Version : Remove duplicate file textures


fghajhe
07-12-2012, 12:01 AM
I am working on a small script to remove duplicate file textures in the hypershade. First I want to store all the duplicate files in a string array. I think my code should work (eg return in string $trashFiles all the duplicate textures with the same file directory ) but it doesn't end up storing anything. If anyone could point out the flaw it would be much appreciated! -Thanks



clear $trashFiles;
string $trashFiles[];
string $fileTextures[] = `ls -type "file"`;
for ($file in $fileTextures)
{
string $newfileTextures[] = `ls -type "file"`;
for ($newfile in $newfileTextures)
{
string $path1 = `getAttr ($file+".ftn")`;
string $path2 = `getAttr ($newfile+".ftn")`;
if ($path1 == $path2) continue;
if ($file != $newfile) continue;
stringArrayInsertAtIndex( 0, $trashFiles, $newfile );
}
}

NaughtyNathan
07-12-2012, 09:27 AM
if path1 is equal to path2 you continue. "continue" halts the processing of the current iteration of the loop, so if the paths are the same (i.e. duplicates) nothing happens.

Additionally you are always inserting your "$newfile" item at index 0 in the array. This doesn't "push" the other items further up the array, it simply overwrites the item at the first element [0]. No matter how many items this code finds (if it worked), the array will only ever be 1 item long.

Finally, even if you fix these issues, the logic is still faulty as this would generate a list of every file that had the same path as any other, in other words it wouldn't only store the duplicates, but ALL instances (and you wouldn't know which items in the list shared the same path)

oh, and this code will also generate an error every time you clean-run it as the first line references a variable that doesn't yet exist.

fghajhe
07-12-2012, 03:13 PM
Thanks for all the pointers- totally had continue's functionality backwards! I am getting a little bit closer I hope, but now my code returns all the file textures (instances) like you said not just duplicates. Also, unless I am using it incorrectly/ there is another bug with my code, inserting at 0 seems to just place it at the beginning of the array and doesnt overwrite anything.


string $trashFiles[];
string $skipfile = "";
clear $trashFiles;

string $fileTextures[] = `ls -type "file"`;
for ($file in $fileTextures)
{

if ($file == $skipfile) continue;

string $newfileTextures[] = `ls -type "file"`;
for ($newfile in $newfileTextures)
{
string $path1 = `getAttr ($file+".ftn")`;
string $path2 = `getAttr ($newfile+".ftn")`;
if ($path1 != $path2) continue;

if ($file == $newfile) continue;


//string $newfileArray[] = {$newfile};

//appendStringArray($trashFiles, $newfileArray, 2);

stringArrayInsertAtIndex( 0, $trashFiles, $newfile );
string $skipfile = $newfile;


}
}

NaughtyNathan
07-12-2012, 04:42 PM
yeah, it seems I was wrong, the stringArrayInsertAtIndex function (script) does indeed insert an item into an existing array while preserving the existing members. I've never had cause to use it myself, it must be pretty inefficient... doing: $array[size($array)] = $item; is much better I think.

CGTalk Moderation
07-12-2012, 04:42 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.