Page 1 of 1 1
Topic Options
#51400 - 2000-09-11 10:59 AM Compare filenames with different extensions
Anonymous
Unregistered


All

Please help me on this.

I want to compare filenames in a directory which sounds easy enough, but what I need to do is this.
Compare filenames BEFORE the extension. If there are two file os the same name, but different extension, then delete both of them. Eg. If there are th efiles name1.csv name1.cnf name2.csv
then the script should delete the files name1.csv and name1.cnf, but leave name2.csv.

I have tried a loop where I get the names of all the files in the dir with the extension CSV and a second list with all the files with extension cnf.

I then loop using readline, by reading the first line of the first file, and comparing to line one of the second file, line two of the second file etc.

The following is a the script I have tried for the compare and loop, but does not appear to work. (The file names in each txt files are the same, so I am checking if the file name1.txt exists in both listings)
The script goes off in an infinite loop even if there are only two files in each list.

AM GOING MAD TRYING TO SORT THIS OUT!!!!!

break on

IF Open (1, dirlist.txt)=0
DO
WHILE @error=0
$y=1

$x=Readline ($y)
"$x "?

If open (2, dirlist2.txt)=0
while @error=0
$a=1
$b=Readline ($a)
If $b=$x
?"$b and $x are the same!!!!!"
else
?"NOTHING"
endif
sleep 1
$a="$a+1"
Loop
Close (2)
Else ?"could not open dirlist2 becasue of error @error"
sleep 5
Endif

$y="$y+1"

Loop

Close (1)
Until $x=0
Else
?"unable to read file"

ENDIF

Top
#51401 - 2000-09-11 02:59 PM Re: Compare filenames with different extensions
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
rcuddy:

Might try this:

code:

break on


$file1 = "dirlist.txt"
$file2 = "dirlist2.txt"


if open (1, "$file1" ) = 0


$x = readline (1)


while @error = 0


if open ( 2, "$file2" ) = 0


$y = readline (2)


while @error=0


if $x = $y


?"$x and $y are the same"


endif


$y = readline (2)


loop


$nul = close (2)


endif


$x = readline (1)


loop


$nul = close (1)


endif


exit



Shawn.

Removed redundant close(2)...

Shawn...

[This message has been edited by Shawn (edited 11 September 2000).]

Top
#51402 - 2000-09-11 06:04 PM Re: Compare filenames with different extensions
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
If the specific extensions you are looking for are: .csv first and then .cnf
You can use this code.

Change the variable $ChkDir to the directory you want to check.

code:
$ChkDir = "C:\Temp\"

$Csv_File = Dir($ChkDir + "*.csv")
While $Csv_File <> ""
$Cnf_File = $ChkDir + SubStr($Csv_File, 1, Len($Csv_File) - 4) + ".cnf"
If Exist($Cnf_File)
Del $Cnf_File
Del $ChkDir + $Csv_File
EndIf
$Csv_File = Dir()
Loop


Erik

[This message has been edited by kholm (edited 11 September 2000).]

Top
#51403 - 2000-09-13 12:08 AM Re: Compare filenames with different extensions
Anonymous
Unregistered


Thank you very much. makes my life easier. Hopefully I can repay the favour in the future.
Top
#51404 - 2000-09-12 02:59 PM Re: Compare filenames with different extensions
Anonymous
Unregistered


Even though it sounds like you have fixed the problem. In the future your script would probably be more efficient if you listed all the files of one extension like you did, but instead of listing the others to a file, take the entries from the first file, chop off the last 3 chars, append the 3 from the other extension, and then check to see if that file exists or not by using GetFileAttr. If it returns a non-zero value, the file exists so delete both files.
This method would reduce the number of embedded loops and file operations.

Luck

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 756 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.056 seconds in which 0.03 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org