How to get long filename from ARGV
I don't know if it is possible to change the argument you recieve on a drag and drop, but you could use the Win32 getLongPathName()
function, using the Ruby Win32 bindings
--edit--
Including @peter's solution formatted for readability:
require 'find'
require 'fileutils'
require 'Win32API'
def get_long_win32_filename(short_name)
max_path = 1024
long_name = " " * max_path
lfn_size = Win32API.new("kernel32",
"GetLongPathName", ['P','P','L'],'L').call(short_name, long_name, max_path)
return (1..max_path).include?(lfn_size) ? long_name[0..lfn_size-1] : short_name
end
ARGV.each do|a|
puts a
puts get_long_win32_filename(a)
end
Integer and a filename as two command-line arguments in C
argc
tells you the number of arguments.argv
is an array of char pointers to c-style strings.
So you can simply print all arguments by:
int main(int argc, char *argv[])
{
int i;
for (i=0; i<argc; ++i)
{
printf("%s\n", argv[i]);
}
}
You can use atoi
to convert (the initial portion of) a string to an integer.
So you can do something like:
int main(int argc,char *argv[])
{
char filename[100];
int size = 0;
int i;
if (argc < 3)
{
printf("Too few arguments\n");
return 0;
}
if (strlen(argv[1]) >= 100)
{
printf("File name too long\n");
return 0;
}
strcpy(filename, argv[1]);
size = atoi(argv[2]);
if (size <= 0)
{
printf("Invalid size\n");
return 0;
}
....
....
return 0;
}
Note that it is usually not necessary to copy file name arguments to another variable unless you are going to modify the value in some way.
unable to get filename from argv[1] in C
argv
is of type char **
, so argv[1]
is of type char *
. So that's the type of the variable you want to assign this to.
char *name = argv[1];
You can't declare name
as char []
and initialize it with a char *
. Only a string literal may be used for initialization.
If you want to make a copy of the string rather than have another pointer to it, you can use strdup
which allocates memory for the copied string and copies it over.
char *name = strdup(argv[1]);
Don't forget to free
it when you're done with it.
Extract the file name from filename with path which comes from argument
You can use this:
std::string filename = string(argv[1]);
int index = filename.find_last_of("/\\");
std::string input_trace_filename = filename.substr(index+1);
How do I define and pass in a file name to fopen() from command line?
That's what the arguments to main are for:
#include <stdio.h>
int main(int argc, char **argv)
{
char c;
FILE *fp;
if (argc >= 2)
fp = fopen(argv[1], "w");
else fp = fopen("file.txt", "w");
while ((c = getchar()) != EOF)
{
putc(c, fp);
}
return 0;
}
If you followed this, you might wonder what is in argv[0]
. That's where the program name is. Some operating system environments put the full path to the executable file there. Others put only the program name. Others still put what was typed.
For the command ../../bin/someprogram
on Windows, argv[0]
is "C:\\Documents and Settings\\User\bin\\someprogram.exe"
on Linux/bash, argv[0]
is ../../bin/someprogram
on Ultrix/csh, (I think) argv[0]
is /home/username/bin/someprogram
How to get file names from command-line parameters in Python
Just use sys.argv
:
import sys
import csv
with open(sys.argv[1], "r") as test, open(sys.argv[2], "w") as table:
# more here
Note, sys.argv[0]
contains the script name (in your case, Script.py
). To get the first argument, you should get sys.argv[1]
; to get the second argument, you should get sys.argv[2]
and so on.
Related Topics
How to Convert Ppt to Images in Ruby
Problem Using Openstruct with Erb
Ruby.Metaprogramming. Class_Eval
Accepts_Nested_Attributes_For Ignore Blank Values
How to Create Dynamic CSS in Rails
React Error (Only a Reactowner Can Have Refs.)
Writing an Activerecord Adapter
Rails Server Cannot Start; Getaddrinfo: Nodename Nor Servname Provided, or Not Known (Socketerror)
How to Find Gems That Depend on a Given Gem
Authlogic and Multiple Sessions for the Same User
Does Ruby Have Any Number Formatting Classes
How to Split Routes.Rb into Smaller Files