The C system command with long filenames in windowsXP

This is a discussion on The C system command with long filenames in windowsXP within the C Programming forums, part of the General Programming Boards category; I have having a bit of a problem with this. What I am trying to do is to output the ...

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    The C system command with long filenames in windowsXP

    I have having a bit of a problem with this.
    What I am trying to do is to output the contents of a folder into a file (to get a directory listing)as follows:-

    The below code works fine.

    Code:
    system("dir 2e6d1\*.log /b /s  >dirs.doc");
    However if the folder/directory name is long as in the following:-


    Code:
    system("dir 2e6d1763e3000000\*.log /b /s  >dirs.doc");
    It gives a file not found error, because it does not like the long folder name.

    I am using the djgpp C compiler (where you compile with GCC).

    I only get the error when I run the command from within a C program via a system command, if I run the same command from the keyboard (via a batch file) it works fine.

    It seems, the C program is using a different 'shell' or whatever you call it (the thing which executes commands), so how do I get it to use the command interpreter which 'likes' long filenames.

    TIA

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hm. What is this escape sequence: \*?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Dave_Sinkula
    Hm. What is this escape sequence: \*?

    It's not an escape sequence it is the MSDOS folder seperator as in:-
    C:\DOCUME~1\HP_OWNER\DESKTOP\TESTDIR\2E6D1\196135~ 1.LOG

    I want to list all the ".log" files so I want *.log

    So the last bit would become:-

    C:\DOCUME~1\HP_OWNER\DESKTOP\TESTDIR\2E6D1\*.LOG

    Of course when I was printing the stuff into a string with sprint I had to do something like

    Code:
    sprintf (asrting, "C:\\DOCUME~1\\HP_OWNER\\DESKTOP\\TESTDIR\\2E6D1\\*.LOG")

    (or something like that, it just an example)

    You can see in the above the long filenames have been truncated.



    Anyway.... I have though of a 'work around' in the meantime. What I can do is just rename all the folders to short filenames and then work with them. This is OK because I actually want to rename the folders anyway, based on the date of the files within them, thus if the files are dated 09/09/2006 (todays date) the folder will be named "09092006". This will meet my needs for what I want to do, however I would still like to know how to 'solve' the original problem.
    Now I just need to find a good folder renaming utiliity, any recommendations?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I want to list all the ".log" files so I want *.log
    If you append a /X onto your dir, it will show you both the short and long filename. Last time I did something like this (in another language), I just wrote up a short bat file with all the various dir options I wanted (it has quite a list, type dir /? to see them all), and had it's output go into my program's input.

    Renaming lots of files is a poor idea, imo. Learn to work with your OS, not work around it.

    Adak
    Last edited by Adak; 09-08-2006 at 08:33 PM.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Oh I give up for the time being, I am wasting time just trying to rename a few folders, if I was
    running Unix I would have done this in 2 minutes.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by esbo
    It's not an escape sequence it is the MSDOS folder seperator as in:-
    C:\DOCUME~1\HP_OWNER\DESKTOP\TESTDIR\2E6D1\196135~ 1.LOG
    Yes, I knew that. The same as I know a forward slash is easier to work with.

    Perhaps the MSDN has something about converting long filenames to short filenames?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    HA HA HA

    I just did it, all you have to do is select all the folders then click rename, rename the first one
    to 'a' (or whatever) then finish and it renames the lot to a(1) a(2) a(3)....a(999999) etc...

    You can buy a utility to do this for about £20 a year if you like

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    That threw a few probs in the program cos of the "a (1)", but I will sort it out later.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be programming it in hex or something?


    Quzah.
    Last edited by quzah; 09-08-2006 at 11:40 PM. Reason: Added a link so others can share in the humor.
    Hope is the first step on the road to disappointment.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Whatever is appropiate

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Well I found a foulder renamer which works!!! (and is easy to use etc...) after a long fruitless
    search.

    http://www.bulkrenameutility.co.uk/Main_Intro.php

    I feel like making a donation!!

  12. #12
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    UmeŚ, Sweden
    Posts
    555
    As you may have heard, calling external applications is not a good practice.
    Could http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/checknamelegaldos8dot3.asp"]CheckNameLegalDOS8Dot3 be of use?
    The output char buffer seems to be something that returns a conversion to short names. Jesus chrsit why can't MSDN be more helpful?
    I was going to try it out but it seems my libraries are out of date and don't have it.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    If it works it's a good idea if it doesn't it's a bad idea.

    In general microsoft is a bad idea.

    Almost got my program to work by the way, I just need to be able to rename a folder from
    within a program, no doubt that will be a thousand times more difficult than it should be
    but I expect I will find a way to do it, by hook or by crook.

    Then I just have to put it in a big loop

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Ha ha just used rename(), works a treat, dunno if it will cope with long filenames though
    (not that it needs to now).

    Actually I just did a test and it will create long filenames, not that would have help me with the earlier prob with
    the shell thingy.
    Last edited by esbo; 09-09-2006 at 05:34 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    To the OP: are you sure that in both versions you escaped your backslashes? It sounds suspicious that it works in a batch file but not in a C program. This was Dave_Sinkula's original point: \* isn't a vaild escape sequence.
    Code:
    system("dir 2e6d1\*.log /b /s  >dirs.doc");
    ->
    Code:
    system("dir 2e6d1\\*.log /b /s  >dirs.doc");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. hton long long
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 06-01-2008, 08:26 PM
  3. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 11:26 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

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