Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Console Applications and Filenames (.Net)

  • 31-05-2005 10:01AM
    #1
    Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭


    'Lo,

    I'm passing filenames into a console application and am having problems with long filenames, namely the spaces in them.

    For example if I say: foo File.txt then foo reads the file name as File.txt. If I pass in foo File Two.txt then I can loop through the args array but the filename is then FileTwo.txt which is obviously incorrect. foo "File Two.txt" works.

    Is there any way I can get the filename passed in properly without using quotes?


Comments

  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Using quotes is how to pass it properly ;)

    The only other way that I'm aware of is to get the shortname (old 8.3 format) for the file, and use that. Buggahed if I can remember how its done (cause you can't just assume its the first 6 valid chars followed by tilde-1) and its not really an elegant solution anyway.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Yeah, quotes is standard really. No harm in asking though :D


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    alernatively use parameters like -Lilename -Ooutputpath
    and search for -L, -O etc in the passed params bit messer though
    quotes is nicer


  • Moderators, Society & Culture Moderators Posts: 9,688 Mod ✭✭✭✭stevenmu


    Evil Phil wrote:
    'Lo,

    I'm passing filenames into a console application and am having problems with long filenames, namely the spaces in them.

    For example if I say: foo File.txt then foo reads the file name as File.txt. If I pass in foo File Two.txt then I can loop through the args array but the filename is then FileTwo.txt which is obviously incorrect. foo "File Two.txt" works.

    Is there any way I can get the filename passed in properly without using quotes?
    I'm probably missing something obvious here it looks to me like you're getting two array elements, one with "File" and the other with "Two.txt" ? Can't you just have:

    filename = args[0] + " " + args[1]

    you'd have to put in some kind of logic to check if args[0] has a file extension, if not use args[1] aswell (altough you should really have some kind of loop to go through as many args as exist untill you get to one with a file extension and add them together with the spaces). If you need to handle other args you'd want to maintain an integer which holds how many places in the array to skip.

    I don't think I've ever seen an app handle long file names properly, nice to see someone making that extra bit of effort.


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Nah, quotes are the way its done I'm afraid. The problem with looping through the args array is you can't be sure how many spaces are being skipped. You could code around it I suppose but people expect to use quotes so I'm not going to bother.


  • Advertisement
Advertisement