Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2006, 01:01 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
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.

Reply With Quote
  #2  
Old June 5th, 2006, 01:42 PM
cybersaga's Avatar
cybersaga cybersaga is offline
I have a red lunch bag
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2005
Location: The armpit of Niagara
Posts: 3,125 cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 3 m 46 sec
Reputation Power: 333
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.

Reply With Quote
  #3  
Old June 5th, 2006, 05:01 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
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?

Reply With Quote
  #4  
Old June 5th, 2006, 05:18 PM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 23 m 58 sec
Reputation Power: 1008
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.

Reply With Quote
  #5  
Old June 5th, 2006, 05:51 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #6  
Old June 5th, 2006, 06:05 PM
cybersaga's Avatar
cybersaga cybersaga is offline
I have a red lunch bag
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2005
Location: The armpit of Niagara
Posts: 3,125 cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level)cybersaga User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 3 m 46 sec
Reputation Power: 333
Quote:
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.

Reply With Quote
  #7  
Old June 6th, 2006, 12:22 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
Aight, thx, and I use Free Pascal.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Pascal help please: loop through files in directory


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway