Jump to content

How to handle defective sectors


UEZ
 Share

Recommended Posts

Is it possible to create a file which is not 0 KB over defective sectors?

I got a 2.5" HD which has a lot of defective sectors and when I create a file over the defective

sector then the file will be 0 KB!

Any idea how to create a file which is not 0 KB?

Thanks,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

The idea was to replace the defective sectors with dummy files.

Here the code:

#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Description=Defect Sector Replacer by (c) UEZ 2008
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_LegalCopyright=UEZ 2008
#AutoIt3Wrapper_res_requestedExecutionLevel=requireAdministrator
://////=__=
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/so
#AutoIt3Wrapper_Res_SaveSource=n
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Change2CUI=n
#AutoIt3Wrapper_Run_After=upx.exe --best "%out%"
#AutoIt3Wrapper_Run_After=del /f /q "Defect Sector Replacer_Obfuscated.au3"

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <Date.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $version = "0.50 Beta Build 2009-01-02"
Global $GUI = GUICreate("DSR by UEZ (c) 2009 v" & $version, 492, 341, -1, -1, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS))
Global $Label4 = GUICtrlCreateLabel("Defect Sector Replacer", 49, 4, 393, 53)
GUICtrlSetFont(-1, 32, 400, 4, "Times New Roman")
Global $Statusbar_Parts[2] = [22, -1] ;set position of the statusbar parts
Global $Statusbar_Icon = _WinAPI_LoadShell32Icon(2) ;set icon in the status bar
Global $StatusBar = _GUICtrlStatusBar_Create($GUI)
_GUICtrlStatusBar_SetParts($StatusBar, $Statusbar_Parts)
_GUICtrlStatusBar_SetIcon($StatusBar, 0, $Statusbar_Icon)
Global $Progress = GUICtrlCreateProgress(1, 280, 490, 17)
Global $Start_Button = GUICtrlCreateButton("Start", 16, 240, 75, 25, 0)
Global $Abort_Button = GUICtrlCreateButton("Abort", 104, 240, 75, 25, 0)
GUICtrlSetTip($Abort_Button, "You can press the Abort button during run process but you need to wait sometimes some seconds until it will abort ;-)")
GUICtrlSetState($Abort_Button, $GUI_DISABLE)
Global $Exit_Button = GUICtrlCreateButton("Exit", 400, 240, 75, 25, 0)
Global $Combo = GUICtrlCreateCombo("", 114, 96, 76, 25)
GUICtrlSetTip($Combo, "Please select drive where the dummy files will be created into folder DSR.")
Global $Label1 = GUICtrlCreateLabel("Select drive:", 24, 96, 90, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Label2 = GUICtrlCreateLabel("MB free space on drive", 304, 96, 166, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Output1 = GUICtrlCreateInput("", 200, 96, 100, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
Global $Label3 = GUICtrlCreateLabel("Enter file size to be created on disk:", 24, 140, 255, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Input1 = GUICtrlCreateInput("1024", 280, 140, 81, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
GUICtrlSetTip($Input1, "Please enter a valid integer number for dummy file size in kilo bytes (kb). Default is 1024 kb")
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
Global $Label5 = GUICtrlCreateLabel("KB", 365, 140, 25, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Label6 = GUICtrlCreateLabel("Amount of files:", 24, 184, 114, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Output2 = GUICtrlCreateInput("", 138, 184, 81, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
GUICtrlSetTip($Output2, "This shows the amount of files which will be created on disk.")
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
Global $Label7 = GUICtrlCreateLabel("Rest:", 246, 184, 42, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Output3 = GUICtrlCreateInput("", 288, 184, 81, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
GUICtrlSetTip($Output3, "This shows the rest of free disk space where an extra dummy file will be created.")
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
Global $Label8 = GUICtrlCreateLabel("KB", 374, 184, 25, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
Global $Settings = GUICtrlCreateGroup("Information", 16, 72, 457, 153)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Global $MenuItem1 = GUICtrlCreateMenu("?")
Global $MenuItem2 = GUICtrlCreateMenuItem("About", $MenuItem1, -1 , 1)
Global $drive_letter = ""
Global $abort, $deep_scan = 0
Dim $arrDrives[1]
$arrDrives = StringSplit(Local_Disks(), ",") ;read all local fixed drives and split drive names to array
If UBound($arrDrives) - 1 = 1 Then
    MsgBox(16, "ERROR!", "An error has occured. Cannot get local fixed drives!" & @CRLF & @CRLF & "Aborting... :-(")
    Exit
EndIf
For $i = 1 To UBound($arrDrives) -1
    $drive_letter &= $arrDrives[$i] & "|" ;read drive letters with delimeter
Next
$drive_letter = StringLeft($drive_letter, StringLen($drive_letter) -1) ;remove last delimeter
GUICtrlSetData($Combo, $drive_letter, $arrDrives[1]) ;set 1st drive letters to combo box
Global $Drive_Space = DriveSpaceFree(StringLeft($arrDrives[1], 2))
GUICtrlSetData($Output1, Round($Drive_Space, 2))
Global $amount_of_files = Int($Drive_Space * 1024 / GUICtrlRead($Input1)) ;calculate amount of files
GUICtrlSetData($Output2, $amount_of_files)
Global $rest = ($Drive_Space * 1024) - ($amount_of_files * GUICtrlRead($Input1)) ;calculate the rest in KB
GUICtrlSetData($Output3, $rest)
GUISetState(@SW_SHOW, $GUI)
_GUICtrlStatusBar_Resize($StatusBar) ;set status bar
_GUICtrlStatusBar_SetText($StatusBar, "Ready", 1)

While 1
    Global $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            AdlibUnRegister()
            GUIDelete()
            Exit
        Case $Start_Button
            GUICtrlSetState($Start_Button, $GUI_DISABLE)
            GUICtrlSetState($Exit_Button, $GUI_DISABLE)
            GUICtrlSetState($Input1, $GUI_DISABLE)
            GUICtrlSetState($Combo, $GUI_DISABLE)
            GUICtrlSetState($MenuItem2, $GUI_DISABLE)
            GUICtrlSetState($Abort_Button, $GUI_ENABLE)
            AdlibRegister("Abort_Check")
            Create_Files()
            Sleep(3000)
            If Not $abort Then Test_Files()
            AdlibUnRegister()
            GUICtrlSetState($Start_Button, $GUI_ENABLE)
            GUICtrlSetState($Exit_Button, $GUI_ENABLE)
            GUICtrlSetState($Input1, $GUI_ENABLE)
            GUICtrlSetState($Combo, $GUI_ENABLE)
            GUICtrlSetState($Abort_Button, $GUI_DISABLE)
            MsgBox(0, "Finished", "Done!")
        Case $Exit_Button
            AdlibUnRegister()
            GUIDelete()
            Exit
        Case $Combo
            GUICtrlSetData($Output1, Round(DriveSpaceFree(StringLeft(GUICtrlRead($Combo), 2)), 2))
            Refresh_Values()
        Case $Input1
            If GUICtrlRead($Input1) <= 0 Then GUICtrlSetData($Input1, 1)
            If GUICtrlRead($Input1) / 1024 >= DriveSpaceFree(StringLeft(GUICtrlRead($Combo), 2)) Then GUICtrlSetData($Input1, DriveSpaceFree(StringLeft(GUICtrlRead($Combo), 2)) * 1024)
            If StringIsInt(GUICtrlRead($Input1)) = 1 Then
                Refresh_Values()
            Else
                MsgBox(16, "ERROR!", "Please enter a number (integer)!")
                GUICtrlSetData($Input1, 1)
            EndIf
        Case $MenuItem2
            MsgBox(0, "Information", "Written by UEZ using AutoIt ;-)", 10)
    EndSwitch
WEnd

Func Refresh_Values() ;refresh GUI values
    $Drive_Space = DriveSpaceFree(StringLeft(GUICtrlRead($Combo), 2))
    If $Drive_Space = 0 Then ;disable Start button if no space is left on drive
        GUICtrlSetState($Start_Button, $GUI_DISABLE)
    Else
        GUICtrlSetState($Start_Button, $GUI_ENABLE)
    EndIf
    GUICtrlSetData($Output1, Round($Drive_Space, 2))
    $amount_of_files = Int($Drive_Space * 1024 / GUICtrlRead($Input1))
    GUICtrlSetData($Output2, $amount_of_files)
    GUICtrlSetData($Output3, ($Drive_Space * 1024) - ($amount_of_files * GUICtrlRead($Input1)))
EndFunc

Func Local_Disks() ;get all local connected disks via WMI
    Local $system = "Localhost"
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $system & "\root\cimv2")
    Local $colItems = $objWMIService.ExecQuery("SELECT Description, DeviceID, FileSystem From Win32_LogicalDisk Where DriveType=3", "WQL", 0x30)
    Local $Local_Disks = ""
    If IsObj($colItems) Then
        For $objItem In $colItems
            $Local_Disks &= $objItem.DeviceID & " (" & $objItem.FileSystem & "),"
        Next
    EndIf
    Return $Local_Disks
EndFunc

Func Sec_2_Time_Format($time) ;convert seconds to hh:mm:ss format
    $sec = Mod($time, 60)
    If $sec < 10 Then $sec = "0" & $sec
    $min = Mod(Int($time / 60), 60)
    If $min < 10 Then $min = "0" & $min
    $hr = Int($time / 60^2)
    If $hr < 10 Then $hr = "0" & $hr
    Return $hr & ":" & $min & ":" & $sec
EndFunc

Func Abort_Check() ;check Abort button; this will be called with Adlib functionality
    $msg = GUIGetMsg()
    Select
            Case $msg = $Abort_Button
                $abort = 1
                GUICtrlSetState($Start_Button, $GUI_ENABLE)
                GUICtrlSetState($Exit_Button, $GUI_ENABLE)
                GUICtrlSetState($Input1, $GUI_ENABLE)
                GUICtrlSetState($Combo, $GUI_ENABLE)
                GUICtrlSetState($MenuItem2, $GUI_ENABLE)
                GUICtrlSetState($Abort_Button, $GUI_DISABLE)
;~              AdlibUnRegister()
        EndSelect
EndFunc

Func Create_Files() ;create files
    Local $i, $j, $k, $file_handle, $file_handle_rest, $drive, $filename, $suffix, $size, $start_time, $duration, $elapsed, $interrupted, $speed, $random_string, $write_string, $part
    $drive = StringLeft(GUICtrlRead($Combo), 2)
    $filename = $drive & "\DSR"
    $suffix = ".txt"
    $size = GUICtrlRead($Input1) * 1024
    _GUICtrlStatusBar_SetText($StatusBar, "Creating buffer of " & $size & " bytes for file write process...Please wait!", 1) ;set statusbar information
    $write_string = ""
    For $j = 1 To $size ;create string which will be written to each file
        $write_string &= "x"
    Next
    $part = 1
    If DriveSpaceFree($drive) * 1024^2 = 0 Then $part = 0
    $start_time = _NowCalc()
    _GUICtrlStatusBar_SetText($StatusBar, "Elapsed: 0 sec.", 1) ;set statusbar information
    $abort = 0
    $interrupted = 0
    For $i = 1 To $amount_of_files
        If FileExists($filename & $i & $suffix) Then
            $random_string = ""
            For $k = 1 To 4
                $random_string &= Random(97, 122, 1)
            Next
            $file_handle = FileOpen($filename & $i & "_" & $random_string & $suffix, 1 + 8)
        Else
            $file_handle = FileOpen($filename & $i & $suffix, 1 + 8)
        EndIf
        FileWrite($file_handle, $write_string)
        Sleep(10) ;to reduce CPU load
        If $abort = 1 Then
            FileClose($file_handle)
            _GUICtrlStatusBar_SetText($StatusBar, "Aborted!", 1)
            GUICtrlSetData($Progress, 0)
            $interrupted = 1
            ExitLoop
        EndIf
        $elapsed = _DateDiff('s', $start_time ,_NowCalc())
        $duration = Round($elapsed / $i * $amount_of_files, 0)
        $speed = Round($i * $size / ($elapsed * 1024), 0)
        _GUICtrlStatusBar_SetText($StatusBar, "Elapsed: " & $elapsed & " sec. Est. duration: " & $duration & " sec. (" & Sec_2_Time_Format($duration) & ") ~" & $speed & " kb/s: " & $i & "/" & $amount_of_files + $part & " ~" & Round($i / ($amount_of_files + $part ) * 100, 2) & "%", 1) ;set statusbar information
        GUICtrlSetData($Progress, ($i / $amount_of_files) * 100)
        FileClose($file_handle)
    Next
    ReduceMemory()
    If Not $interrupted Then
        GUICtrlSetData($Progress, 0)
        $rest = DriveSpaceFree($drive) * 1024^2
        If $rest > 0 And $rest < GUICtrlRead($Input1) * 1024 Then
            _GUICtrlStatusBar_SetText($StatusBar, "Creating the rest (" & $rest & " bytes)...", 1)
            If FileExists($filename & $i & $suffix) Then
                ;FileDelete($filename & $i & $suffix)
                $random_string = ""
                For $k = 1 To 4
                    $random_string &= Random(97, 122, 1)
                Next
                $file_handle_rest = FileOpen($filename & $i & "_" & $random_string & $suffix, 1 + 8)
            Else
                $file_handle_rest = FileOpen($filename & $i & $suffix, 1 + 8)
            EndIf
            $write_string = ""
            For $j = 1 To $rest ;create string which will be written to each file
                $write_string &= "x"
            Next
            FileWrite($file_handle_rest, $write_string)
            FileClose($file_handle_rest)
        EndIf
        _GUICtrlStatusBar_SetText($StatusBar, "Done. All files have been created!", 1)
;~      $rest = DriveSpaceFree($drive) * 1024^2
;~      If Not $interrupted Then
;~          MsgBox(0, "Test", "Finished. Rest = " & $rest & " bytes")
;~      EndIf
    Else
        _GUICtrlStatusBar_SetText($StatusBar, "Aborted!", 1)
    EndIf
    Refresh_Values()
    GUICtrlSetData($Progress, 0)
    ReduceMemory()
EndFunc

Func Test_Files()
    Local $c, $search, $file, $file_check, $file_size, $char, $drive, $search_name, $suffix, $corrupt, $temp, $size, $file_counter, $x, $amount_of_corrupt_files, $rest
    $drive = StringLeft(GUICtrlRead($Combo), 2) & "\"
    $suffix = ".txt"
    $search_name = $drive & "DSR*" & $suffix
    $file_counter = Amount_of_Files($search_name)
    $search = FileFindFirstFile($search_name)
    $size = GUICtrlRead($Input1) * 1024
    If $search <> -1 Then
        GUICtrlSetData($Progress, 0)
        $x = 0
        $amount_of_corrupt_files = 0
        _GUICtrlStatusBar_SetText($StatusBar, "Searching for corrupt files...", 1)
        While $abort = 0
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
;~          ConsoleWrite("File: " & $file & @CRLF)
            $corrupt = 0
            $file_size = FileGetSize ($drive & $file)
            $file_check = FileOpen($drive & $file, 0)
            If $deep_scan Then
                $c = 0
                While 1
                    $char = FileRead($drive & $file, 1)
                    If @error = -1 Then ExitLoop
                    If $char <> "x" Then
                        $corrupt = 1
                        $amount_of_corrupt_files += 1
                        ExitLoop
                    EndIf
                    $c += 1
                WEnd
                FileClose($file_check)
                If $corrupt = 0 And $c = $file_size Then FileDelete($drive & $file)
            Else
                $temp = FileRead($file_check)
                If @error > 0 Or $file_size = 0 Then
                    $corrupt = 1
                    $amount_of_corrupt_files += 1
                EndIf
                FileClose($file_check)
                If $corrupt = 0 And $file_size > 0 Then ;$file_size = $size Or $file_size = $rest
                    FileDelete($drive & $file)
                EndIf
            EndIf
            $x += 1
            GUICtrlSetData($Progress, $x / $file_counter * 100)
            _GUICtrlStatusBar_SetText($StatusBar, "Searching for corrupt files - normal scan: " & $x & " / " & $file_counter & " ~" & Round($x / $file_counter * 100, 2) & "%", 1)
            Sleep(10)
        WEnd
    EndIf
    FileClose($search)
    GUICtrlSetData($Progress, 0)
    If $abort = 0 Then
        _GUICtrlStatusBar_SetText($StatusBar, "Done. Found " & $amount_of_corrupt_files & " on " & $drive & " !", 1)
    Else
        _GUICtrlStatusBar_SetText($StatusBar, "Aborted!", 1)
    EndIf
    Refresh_Values()
EndFunc

Func Amount_of_Files ($path)
    Local $search = FileFindFirstFile($path), $z, $file
    If $search <> -1 Then
        $z = 0
        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            $z += 1
        WEnd
    Else
        $z = 0
    EndIf
    Return $z
EndFunc

; Reduce memory usage
; Author wOuter ( mostly )
Func ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    Return $ai_Return[0]
EndFunc ;==>ReduceMemory

Maybe this makes it clear why I asked that question.

Use it carefully because write operation will be done on selected drive when you push the start button!

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Moderators

UEZ,

I would echo WeaponX's advice on getting a new drive- defective sectors nearly always spread rapidly and you risk very quickly finding yourself with no drive left at all. I can remember once formatting a drive that had only a few bad sectors - hoping to get them all noted. Every format turned up more and more until I ended up with a useless chunk of metal. It made a nice noise as it spun - but that was all it was good for.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

First thing, get a new hard drive. Newegg has 120 Gb 2.5" drives for ~$60.

Second, i'm pretty sure running "chkdsk /R" will flag bad sectors so they aren't used.

Of course I did the checkdisk (/b on Vista) and also a low level format.

And probably the best thing is to buy a new HD.

But it was just an idea whether it is possible to code a tool that create files over defective sectors independently of whether that makes sense to use a defective HD.

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...