AppleScript - rename files

This is a discussion on AppleScript - rename files within the C Programming forums, part of the General Programming Boards category; Hi all, Just lookin for some C script to rename files in a directory while iterating thru files 1 by ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    AppleScript - rename files

    Hi all,

    Just lookin for some C script to rename files in a directory while iterating thru files 1 by 1,

    been searchin for some time now but cant seem to get code together.
    Apple has a builtin scripting which is called applescript that does this but its not efficient, though the goal is almost the same...

    Any help would be much appreciated.


    Thanks in Advance,

    VV

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Code:
    #include <iostream.h>
    
    int main()
    {
    	char file[MAX_PATH];
    	char newfilename[MAX_PATH];
    	char scanf_s[5];
    	sprintf(scanf_s, "%%%is", MAX_PATH - 1);
    	char quit = 'N';
    	while (quit == 'N')
    	{
    		printf("Enter filename to rename:\n");
    		scanf(scanf_s, file);
    		printf("Enter new filename:\n");
    		scanf(scanf_s, newfilename);
    		if ( rename(file, newfilename) )
    			printf("Successfully renamed file!\n");
    		else
    			printf("Oops! Unable to rename file!\n");
    		printf("Rename another file (Y/N)?\n");
    		scanf("%c", &quit);
    		quit = toupper(quit);
    	}
    	return 0;
    }
    Something like that? Not tested and appropriate headers needs to be included, so consider it pseudo.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    Cheers

    Thanks for the quick reply, I will try to build on your code,

    once again thanks heaps.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or something like this:
    Code:
    #!/bin/sh
    for file in  ./*
       move $f $f.new
    end
    Untested, not quite convinced it's that simple. Another version to just display the names is here:
    http://www.juixe.com/techknow/index....-shell-script/

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > Apple has a builtin scripting which is called applescript that does this but its not efficient,
    Writing it in C isn't going to make it vastly quicker. Nearly all the work is being done outside the code you write, so whether you write it in Applescript or C won't make that much of a difference.

    Like what matsp posted, a script takes 30 seconds to write (and then you throw it away probably), whereas a C program might take you all day if you're not already adept at programming in C (this thread is already 3 hrs old, and your program isn't finished yet).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    thanks salem,

    being at work i havent really had a chance to run any code, but still appreciate the help
    speed is not what i m really interested in as Apples automator changes the names of 100 files in a folder in about say 10 seconds or even less never calculated...

    I have been lookin to write the code for this task in any language that i can...
    1 reason C appealed more than the others is i can run C in apples terminal where as for languages like php or java i need to install additional software to compile and run...

    shell script looks quite interesting and might be good if i can hack some code like that...


    Thanks for the post anyways...
    Still any help is appreciated...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the actual renaming you are doing? Are you doing this on a regular basis? What are the original names, and what are the new names (you don't need to give details, but say for example you are renaming IMG_nnnn.JPG to Portrait_Lisa_nn.jpg would give us some idea of what you are actually trying to do. Getting a "bigger picture" often helps in the recommendation of the right solutions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    actually renaming files that go in order

    SL0051.jpg
    .
    .
    SL0056.jpg
    SL0057.jpg
    SL0058.jpg
    SL0059.jpg
    SL0060.jpg
    SL0061.jpg

    so what i m tryin to do is change them to is;
    pic1.jpg
    pic2.jpg
    pic3.jpg

    and so on

    Thanks again...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok. Like Salem, I believe the majority of the time consumed by something like this is the time it takes to ACTUALLY rewrite the directory entry of the file. So it's unlikely that the AppleScript is taking much of the time, compared to the actual rename operation itself. I could of course be wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    i can get my head around the pseudo

    files are all in the same directory so;

    correct me if i m wrong;

    //open directory
    //point to first file (of course check if directory empty)


    //loop (this is 1 of the parts where i lose myself)
    //rename action
    // rename(current, new);

    //end loop

    //end

    Cheers

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    I would do this in Perl. It has lots of nice text manipulation functions, and plenty of APIs to the file system (getting directory entries, renaming files etc).

    But before you go to the trouble of all the text handling in C (which, lets face it, is hard work), try something like.
    Code:
    struct {
      char *old;
      char *new;
    } table[] = {
      { "SL0051.jpg", "pic1.jpg" },
      { "SL0052.jpg", "pic2.jpg" },
    };
    int main ( ) {
      size_t i;
      for ( i = 0 ; i < sizeof table / sizeof *table ; i++ ) {
        rename( table[i].old, table[i].new );
      }
      return 0;
    }
    Pad the table with 100, 1000 (or however many) and time how long it would take to simply rename everything. Then you'll have a baseline to compare with the same thing in Applescript.

    Unless you get stunning performance differences, you're going to question the effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    now that i see the struct and the tabel i understand what is meant by the time spent outside the code

    i was planning to iterate the directory without being have to hard code the file name...

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vvan View Post
    now that i see the struct and the tabel i understand what is meant by the time spent outside the code

    i was planning to iterate the directory without being have to hard code the file name...
    Yes, of course, and you can do that, but it would require a lot of string parsing and other stuff. So instead of spending many hours coming up with the code to convert one filename into another filename with the string parsing and string generation, use simple table to estimate how quickly it can be done - if this proves either side of the argument (AppleScript is slow vs. the actual task of renaming the file is the expensive part), then you know which way to go, and where to spend your time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > i was planning to iterate the directory without being have to hard code the file name.
    Yes, which is yet more time spent outside the program, seeking all over those extremely slow hard disks (in program time terms).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    Fair enuf... Just gonna have to look for an alternative i guess

    Thanks a lot for the help indeed...

    VV

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 09:36 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21