Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Question

I am trying to sort and rename a large number of files that are labeled #####_##

The LEFT side of the underscore are numbers (e.g., 32956715, 32956810, etc.) that do not repeat.
The RIGHT side of the underscore are also numbers (e.g., 1, 2, 3, etc.) and they do repeat.

Example posted at pastebin

(The left side is the number of a scan and the right side is the page of that particular scan.)

I would like to be able to sort the left side of the underscore Z-A and the right side A-Z.

Example:

3_1
3_2
3_3
2_1
2_2
2_3
1_1
1_2
1_3

I'm open for any suggestions that achieve the result I'm looking for.

Currently I am using irfanview to send the filenames to a text file, then copying and pasting the text into excel, seperating the filenames into colums, then sorting the column with the LEFT side of the underscore by Z-A and the column with the RIGHT side of the underscore by A-Z. Then I essentially reverse the process and have irfanview thumbnail viewer load the files using a txt file created from my correctly sorted excel list.

There has to be a better way right?

Thank You!

UPDATE

More Information to attempt to clarify the question. I'm embarrased to admit this, but the end result is to print out each of these files and have them be face-up, in reverse chronological order (the newest document on top(face-up) and the oldest document on the bottom (face-up)). (Printing isn't very tech savvy is it? Can I still be in the superuser's club--please.)

A client gives us a disc of imaged documents (tiffs or gifs with jpgs mixed in). The discs can have anywhere from 50 to 20000 images on them. I have to print them out. Smaller numbers=older images and larger numbers=newer images. I am using Irfanview thumbnail viewer to batch print to a big copy machine. Right now I can only sort the images A-Z before printing while still keeping them in some recognizable order. Then I have to go through the printed stack and do the reverse chronological sorting myself. This is very time consuming and with attachments to documents it is difficult to determine where one document stops and the next begins.

Comments

I am not sure if I understood the question. If the left set of digits never repeat then the problem does not exist. You can not have a situation like 30000_1, 30000_2 because the left side does not repeat. – Sahil

You are correct. They do repeat if there is more than one page in the scanned document. I was just trying to differentiate that kind of repeating from the repeating on the right side which always starts with 1 and counts up based on the number of pages in the document.

I want to thank everyone who's already asked questions. It means you at least read the question and would try to help if I hadn't gone to great lengths to confuse you. I hope my extra info answers the other questions I did not address specifically.

Answers

The Lua script answer looks promising, but I am getting an error when trying to use it. Thoughts?

lua -e "io.stdout:setvbuf 'no'" "Reverse_Sort.lua" 
lua: Reverse_Sort.lua:16: attempt to compare number with nil
stack traceback:
 Reverse_Sort.lua:16: in function <Reverse_Sort.lua:6>
 [C]: in function 'sort'
 Reverse_Sort.lua:32: in main chunk
 [C]: ?
Exit code: 1
share|improve this question
    
What are you renaming them to? –  Nick T Jan 6 '11 at 21:43
    
Also, please define 'sort'. How/where do you want to see the sorted list? –  Rich Homolka Jan 6 '11 at 21:57
    
Renaming and sorting are at best tangentially related. Please describe in more detail what it is that you're actually trying to accomplish. By the way, what you seem to be describing is referred to as a "stable sort". –  Dennis Williamson Jan 6 '11 at 22:55
    
I am not sure if I understood the question. If the left set of digits never repeat then the problem does not exist. You can not have a situation like 30000_1, 30000_2 because the left side does not repeat. –  Sahil Jan 6 '11 at 23:23
    
@The_Buff: Could you provide an example dir listing on pastebin? I think it may be possible just using for and sort. –  paradroid Jan 8 '11 at 4:03
show 3 more comments

1 Answer

up vote 1 down vote accepted

Since you've said this is a recurring problem, a script is appropriate.

Here's a Lua script to get you going.

Edit: I've updated the script based on your additional information. It will take a file containing filenames, sort them and create a new file in the sorted order.

Script (edited)

-- Get input and output filenames from the command line or pick defaults
input_filename = arg[1] or 'unsorted.txt'
output_filename = arg[2] or 'sorted.txt'

-- Define custom sort function
function custom_sort(filename1, filename2)

    a, b = string.match(filename1, '(%d+)_(%d+)')
    c, d = string.match(filename2, '(%d+)_(%d+)')

    a = tonumber(a)
    b = tonumber(b)
    c = tonumber(c)
    d = tonumber(d)

    if a > c then
        return true
    elseif a < c then
        return false
    else
        return b < d
    end
end

-- Read filenames from file into table
local filenames = {}
for line in io.lines(input_filename) do
    filenames[# filenames + 1] = line
end

-- Sort table using our custom sort function
table.sort(filenames, custom_sort)

-- Write sorted filenames to file
io.output(output_filename)
for _,filename in ipairs(filenames) do
    io.write(filename, '\n')
end

-- Done

Lua has the os.rename() function to actually do the file renaming or you could probably dump the text output of the script into a renaming utility.

share|improve this answer
    
Thank you! I went ahead and marked you as the accepted answer. I'll give it a shot today and see how it goes. If you have any suggestions for what the title of this question ought to be (to make your answer more useful for others) please let me know. –  The_Buff Jan 10 '11 at 20:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.