Here's some code that fails when it hits a file name containing unicode:
Code: Select all
' FreeBasic Application
' dups.bas by Jim Dunn, 2022-03-27, v0.1
' =============================================================================
#Include "jCommon.inc"
' =============================================================================
Function WinMain( _
ByVal hInstance As HINSTANCE, _
ByVal hPrevInstance As HINSTANCE, _
ByVal szCmdLine As ZString Ptr, _
ByVal nCmdShow As Long _
) As Long
Dim AS CWSTR wszFolder, wszFileName(Any)
Dim As String sBuf1, sBuf2
Dim As ULONGINT uliSizes(Any), bufSiz, i
Dim As Long x, y, z, lineCount
Dim As Integer fp1, fp2, jFlag
if Len(Command) < 1 then
jPrint("")
jPrint("Usage: dups.exe [folder to scan for duplicates]")
goto exitfunction
end if
wszFolder = Command
lineCount = jListFiles(wszFolder,wszFileName(),uliSizes())
for x = 0 to UBound(wszFileName) - 1
for y = x+1 to UBound(wszFileName)
if uliSizes(x) > 0 and uliSizes(x) = uliSizes(y) then
sBuf1 = ""
fp1 = FreeFile
If Open(wszFileName(x) For Binary Access Read As #fp1) <> 0 Then
jPrint("Error reading [" & wszFileName(x) & "]")
goto exitfunction
End If
sBuf2 = ""
fp2 = FreeFile
If Open(wszFileName(y) For Binary Access Read As #fp2) <> 0 Then
jPrint("Error reading [" & wszFileName(y) & "]")
goto exitfunction
End If
bufSiz = 4194304
if bufSiz > LOF(fp1) then
bufSiz = LOF(fp1)
end if
i = 0
While i < LOF(fp1)
sBuf1 = String(LOF(fp1),0):
if Get(#fp1,bufSiz,sBuf1) <> 0 then
sBuf1 = ""
end if
sBuf2 = String(LOF(fp2),0)
if Get(#fp2,bufSiz,sBuf2) <> 0 then
sBuf2 = ""
end if
if sBuf1 <> sBuf2 then
i = 0 ' setting to 0 meaning NOT A DUPLICATE
exit while
end if
i = i + bufSiz
Wend
Close #fp1
Close #fp2
if i > 0 then ' >0 = duplicate
if Len(wszFileName(x)) > Len(wszFileName(y)) then
jPrint("del " & chr(34) & wszFileName(x) & chr(34))
uliSizes(x) = 0 ' filesize = 0 skips compare on next loop
else
jPrint("del " & chr(34) & wszFileName(y) & chr(34))
uliSizes(y) = 0 ' filesize = 0 skips compare on next loop
end if
end if
end if
next y
next x
' =========================================================================
exitfunction:
WinMain = 0
End Function
' =============================================================================
End WinMain(GetModuleHandle(Null), Null, Command(), SW_NORMAL)
' =============================================================================
Code: Select all
Function jListFiles OverLoad(ByVal wszPath as CWSTR, wszArray() AS CWSTR, uliArray() AS ULONGINT) as LONG
Dim hSearch as HANDLE
Dim WFD AS WIN32_FIND_DATAW
Dim wszCurPath AS CWSTR
Dim wszFullPath AS CWSTR
Dim AS LONG lineCount = 0
if right(wszPath,1) = "*" then
wszCurPath = wszPath
else
if right(wszPath,1) <> "\" then
wszPath += "\"
end if
wszCurPath = wszPath + "*.*"
end if
' Find the files
hSearch = FindFirstFileW(wszCurPath, @WFD)
IF hSearch <> INVALID_HANDLE_VALUE THEN
DO
IF (WFD.dwFileAttributes AND FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY THEN
' found a folder
ELSE
lineCount += 1
wszFullPath = wszPath & WFD.cFileName ' Store the full path
ReDim Preserve wszArray(UBound(wszArray) + 1) AS CWSTR ' zero-based array
wszArray(UBound(wszArray)) = wszFullPath ' file name
ReDim Preserve uliArray(UBound(uliArray) + 1) AS ULONGINT ' zero-based array
uliArray(UBound(uliArray)) = (WFD.nFileSizeHigh * (&hFFFFFFFF + 1)) + WFD.nFileSizeLow ' file size
END IF
LOOP WHILE FindNextFileW(hSearch, @WFD)
FindClose(hSearch)
END IF
Return lineCount
End Function