#1
  1. C++arl!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Stockholm
    Posts
    165
    Rep Power
    18

    Pascal help please: loop through files in directory


    I have made a program that searches a folder for .txt files that only have one character as a name, like "a.txt" or "K.txt". Sounds stupid I know but it is just a test, if i get it to work then i will make it more useful. Anyway, my problem is that if I for instance have the file "a.txt" in the folder then the programs says that I have "a.txt AND "A.txt" wich is very odd Take a look.

    Code:
    program muuuu;
    uses
      crt;
    var
      i : integer;
      yesno : boolean;
      janej : char;
    
      procedure search;
      const
        format = '.txt';
      var
        a,b,c : array [0..255] of char;
        filnamn : string;
        antal, x : integer;
    
         function exists(filnamn:string):boolean;
         var
          fil : text;
         begin
           exists := false;
           assign(fil,filnamn);
           {$I-}reset(fil);{$I+}
          if IOResult = 0 then begin
            close(fil);
            writeln(filnamn);
            exists := true;
          end;
         end;
    
      begin
        antal := 0;
        for x := 1 to 255 do begin
          a[x] := chr(x);
          filnamn := a[x]+format;
          if exists(filnamn) then
            inc(antal);
        end;
        writeln('There are ',antal,' files in this folder.');
      end;
    
    begin
      clrscr;
      writeln('Do you want to scan folder?');
      readln(janej);
      if janej in ['Y','y'] then begin
        clrscr;
        writeln('The files are as follows:');
        search;
      end
      else
        halt;
      readln();
    end.
  2. #2
  3. I have a red lunch bag
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2005
    Location
    The armpit of Niagara
    Posts
    3,125
    Rep Power
    339
    Code:
      procedure search;
      const
        format = '.txt';
      var
        a,b,c : array [0..255] of char;
        filnamn : string;
        antal, x : integer;
    
     -- snip --
    
      begin
        antal := 0;
        for x := 1 to 255 do begin
          a[x] := chr(x);
          filnamn := a[x]+format;
          if exists(filnamn) then
            inc(antal);
        end;
        writeln('There are ',antal,' files in this folder.');
      end;
    Your problem is that you're going through all 255 characters in the ASCII chart, which includes upper case and lower case characters. So you are searching for "a.txt" and "A.txt". My guess is you're using Windows, which is not case sensitive. So if you have a file called "a.txt", and you request "A.txt", Windows will hand you "a.txt".

    To avoid this, look at an ASCII chart (specifically the numbers beside the characters) and modify your loop so that you're only looking at upper case, or only looking at lower case.

    Comments on this post

    • LinuxPenguin agrees : So used to working with linux that i would never have thought of that
    Web Design Tips - Posting and You

    If I've been helpful, and/or you're really nice, consider buying something from my Amazon.com wishlist.
    • There is no room for pride when trying to be the best at what you do.
    • Friends don't let friends use bad code.
    The club is dead.
  4. #3
  5. C++arl!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Stockholm
    Posts
    165
    Rep Power
    18
    So you are saying its best to make one procedure that only checks for small letters and then one that only checks for big letters? I thought that that wouldnt be needed since for instance 'a' and 'A' do have different ASCII values and thus should not be taken for the same thing?
  6. #4
  7. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    Windows isn't case sensitive. On windows a you can call a file a.txt and then you can refer to it either as a.txt or A.TXT or anything in between. Not case sensitive, but the same file.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Is that Turbo Pascal (or Borland Pascal)? If so, try using FindFirst and FindNext procedures.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. I have a red lunch bag
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2005
    Location
    The armpit of Niagara
    Posts
    3,125
    Rep Power
    339
    Originally Posted by lingon
    So you are saying its best to make one procedure that only checks for small letters and then one that only checks for big letters? I thought that that wouldnt be needed since for instance 'a' and 'A' do have different ASCII values and thus should not be taken for the same thing?
    I didn't say you need both, I said you need only one. Check upper case or lower case, as either one will point to the same file.

    As LP reiterated, Windows is not case sensitive. If you were on any unix derivative however, it would be case sensitive.
    Web Design Tips - Posting and You

    If I've been helpful, and/or you're really nice, consider buying something from my Amazon.com wishlist.
    • There is no room for pride when trying to be the best at what you do.
    • Friends don't let friends use bad code.
    The club is dead.
  12. #7
  13. C++arl!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Stockholm
    Posts
    165
    Rep Power
    18
    Aight, thx, and I use Free Pascal.

IMN logo majestic logo threadwatch logo seochat tools logo