Thread: Drag and drop onto executable

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    77

    Drag and drop onto executable

    All I want is to automate a program (which requires a lot of command line input to work) via a drag and drop interface. My first stop was batch programming, but I quickly found out that it's not even really possible to grab the extension of a file in there (with any straightforwardness).

    So, I turn to my old standard, a c program. I think I know how to grab an extension in c, but I have no idea how to grab a file name/path from a drag and drop execution. Where would I go to start learning this?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( int argc, char** argv, char** envp )
    {
    	if( argc > 1 )
    		puts( "- Command Line -" );
    	while( *( ++argv ) )
    		puts( *argv );
    	puts( "- Environment Variables -" );
    	while( *envp )
    		puts( *envp++ );
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    77
    OK, I think I understand the argc argv part. So, if I started a program like this:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
    	for (int i = 0; i < argc; i++)
    	{
    how would I then pull the extension out? Is there some scan function I can use to find the index of the period and then pull everything else out after it and put it into an ext var?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Molokai View Post
    OK, I think I understand the argc argv part. So, if I started a program like this:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        for (int i = 0; i < argc; i++)
        {
    how would I then pull the extension out? Is there some scan function I can use to find the index of the period and then pull everything else out after it and put it into an ext var?
    First of all, the first element of the command line parameter array is the name of the program, so you definitely want to skip that. As to your question, probably the easiest way would be to use the 'strrchr' function (included via 'string.h'), which finds the last occurence of a particular character in a string. Search for the '.' and, if found, everything after that will be the actual extension, naturally.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    77
    Ah, OK, so I start with i = 1 and run through the argv[]. Is argv really in char format, and does that mean that it would be in something like argv[argc][] format, or is it really a string array? Or is there a difference?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Molokai View Post
    Ah, OK, so I start with i = 1 and run through the argv[]. Is argv really in char format, and does that mean that it would be in something like argv[argc][] format, or is it really a string array? Or is there a difference?
    I have no idea what this question is asking. But: argv is what it says it is, a char ** (which equals for our purposes in this case a char *[]). So argv[0] is a char *, argv[1] is a char *, argv[2] is a char *, ....

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Molokai View Post
    Ah, OK, so I start with i = 1 and run through the argv[]. Is argv really in char format, and does that mean that it would be in something like argv[argc][] format, or is it really a string array? Or is there a difference?
    It's as if the calling code had passed your program the following array:

    Code:
    char* argv[ ] = 
    {
        "my_prog.exe", 
        "-foo", 
        "bar.txt", 
        "baz.cpp", 
        "qux.dat", 
    /*
        Null terminated, so that the 
        user knows where to stop
    */
        0    
    };

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know if this is heading in the right direction, but this is how you work with file extensions, in a bat file.

    This bat file, will type out all files with the txt file extension, in the current directory.
    Code:
    echo off
    for %%1 in (*.txt) do type %%1
    pause
    Bat files were made for this kind of work, I believe. I'm *very* unclear why you think that bat files are not "straightforward".

    There are extensive tutorials for bat files, on the web.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Drag and drop controls
    By pc2-brazil in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2008, 02:41 AM
  2. DataGrid drag and drop
    By gtriarhos in forum C# Programming
    Replies: 0
    Last Post: 10-11-2005, 12:36 PM
  3. Drag and Drop using CImageList, Device contexts and BitBlt
    By MJWhiteman2 in forum Windows Programming
    Replies: 0
    Last Post: 03-03-2005, 07:22 AM
  4. drag and drop
    By depsypher in forum Windows Programming
    Replies: 2
    Last Post: 01-05-2002, 10:02 PM
  5. Replies: 4
    Last Post: 10-06-2001, 03:12 PM