Przykłady skryptów wsadowych

Ostatnia aktualizacja: 25 marca 2025 r.

Sprawdź poniższe przykłady skryptów wsadowych, aby dowiedzieć się, jak w pełni zautomatyzować typowe zadania defragmentacji przy użyciu interfejsu UltraDefrag CLI.

Domyślny skrypt niestandardowy do automatycznej defragmentacji

UltraDefrag domyślnie korzysta z następującego skryptu niestandardowego do automatycznej defragmentacji:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@echo off
::
:: UltraDefrag - a powerful disk defragmenter for Windows.
::
:: auto-defrag-custom.cmd - custom script for automatic defragmentation.
::
:: Visit https://en.wikibooks.org/wiki/Windows_Batch_Scripting
:: for general information about the script syntax.
::
:: Refer to the UltraDefrag CLI section of UltraDefrag documentation
:: at https://https://ultradefrag.net/support/ultradefrag-cli/
:: for detailed information about the `udefrag` command and environment
:: variables controlling its behaviour.
::
:: To use Unicode characters in filters edit this file in the Notepad++
:: editor and then save it in UTF-8 (without BOM) encoding.
::
:: Also Notepad++ features syntax highlighting which makes it much easier
:: to edit the file. The latest version of Notepad++ can be downloaded
:: for free from http://www.notepad-plus-plus.org/
::

:: Below is a sample script. It defragments system drive and heavily

:: fragmented content on all the connected non-removable drives.
:: Feel free to adjust it to make it more suitable for your needs,
:: replace it entirely or keep as is.

:: first of all, let's set UD_EX_FILTER to exclude all

:: the stuff which rarely benefits from defragmentation

:: exclude temporary content

set UD_EX_FILTER=*system volume information*;*temp*;*tmp*;*recycle*
set UD_EX_FILTER=%UD_EX_FILTER%;*dllcache*;*ServicePackFiles*;*.log
set UD_EX_FILTER=%UD_EX_FILTER%;*Windows.old*;*\Windows\servicing\*

:: exclude archives

set ARCHIVE_PATTERNS=*.7z;*.7z.*;*.arj;*.bkf;*.bz2;*.bzip2;*.cab;*.cpio;*.deb
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.dmg;*.gz;*.gzip;*.lha;*.lzh;*.lzma
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.rar;*.rpm;*.swm;*.tar;*.taz;*.tbz
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.tbz2;*.tgz;*.tpz;*.txz;*.xar;*.xz
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.z;*.zip
set UD_EX_FILTER=%UD_EX_FILTER%;%ARCHIVE_PATTERNS%

:: exclude multimedia content

set AUDIO_PATTERNS=*.aif;*.cda;*.flac;*.iff;*.kpl;*.m3u;*.m4a;*.mid
set AUDIO_PATTERNS=%AUDIO_PATTERNS%;*.mp3;*.mpa;*.ra;*.wav;*.wma
set VIDEO_PATTERNS=*.3g2;*.3gp;*.asf;*.asx;*.avi;*.flv;*.mov;*.mp4
set VIDEO_PATTERNS=%VIDEO_PATTERNS%;*.mpg;*.rm;*.srt;*.swf;*.vob;*.wmv
set UD_EX_FILTER=%UD_EX_FILTER%;%AUDIO_PATTERNS%;%VIDEO_PATTERNS%

:: exclude virtual machines

set DISK_IMAGE_PATTERNS=*.fat;*.hdd;*.hfs;*.img;*.iso;*.ntfs;*.squashfs
set DISK_IMAGE_PATTERNS=%DISK_IMAGE_PATTERNS%;*.vdi;*.vhd;*.vmdk;*.wim
set UD_EX_FILTER=%UD_EX_FILTER%;%DISK_IMAGE_PATTERNS%

:: exclude big fragments which rarely benefit from defragmentation

set UD_FRAGMENT_SIZE_THRESHOLD=20MB

:: on solid state drives exclude slightly fragmented content as well

set UD_SSD_FRAGMENTS_THRESHOLD=20

:: uncomment the following line to increase amount of debugging output

:: set UD_DBGPRINT_LEVEL=DETAILED

:: defragment system drive

set UD_LOG_FILE_PATH=%UD_INSTALL_DIR%\logs\autodefrag-system.log
udefrag --wait %SystemDrive%

:: defragment heavily fragmented content

:: on all the connected non-removable drives
set UD_LOG_FILE_PATH=%UD_INSTALL_DIR%\logs\autodefrag-fixed.log
set UD_FRAGMENTS_THRESHOLD=20
udefrag --wait --all-fixed

Sprawdzanie wszystkich dostępnych dysków pod kątem błędów

Jeśli dyski, które chcesz defragmentować, nie były przez długi czas sprawdzane pod kątem błędów, warto przed defragmentacją sprawdzić je wszystkie za pomocą systemowego narzędzia chkdsk

Możesz swobodnie korzystać z poniższego skryptu, aby w pełni zautomatyzować proces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@echo off
::
:: This script defragments all the available non-removable disks.
:: If some disks have errors the script repairs them first. If it
:: is impossible to repair a disk because it is in use, the script
:: schedules the repair for the next system reboot and skips the disk.
::

:: set the UltraDefrag specific environment

:: variables according to the user preferences
call "%UD_INSTALL_DIR%\setenv.cmd"

:: defragment the disks

for /f "skip=7 tokens=1" %%d in ('udefrag -l') do call :defragment %%d

:: clear the environment variables and exit

call "%UD_INSTALL_DIR%\clearenv.cmd"
exit /B 0

::

:: This procedure defragments a single disk.
::
:: Usage:
::
::   call :defragment <drive_letter>
::
:defragment
    echo Preparing to check the disk %1 for errors...
    echo.
    chkdsk %1
    echo.
   
    :: if the disk has errors, try to repair it

    if "%ERRORLEVEL%" equ "3" (
        echo y | chkdsk /f %1
        echo.
    )
   
    :: if the disk had no errors or the repair

    :: was successful, defragment the disk
    if "%ERRORLEVEL%" neq "3" (
        echo Preparing to defragment the disk %1...
        echo.
        udefrag %1
        echo.
    )
exit /B 0

Pełna defragmentacja wszystkich dostępnych dysków

Ręczne sprawdzanie, czy na dyskach znajdują się zablokowane, pofragmentowane pliki, i przeprowadzanie defragmentacji podczas rozruchu w przypadku ich obecności, może być żmudne.

Możesz użyć poniższego skryptu, aby w pełni zautomatyzować ten proces. Dodatkowo, skrypt regularnie defragmentuje dyski i optymalizuje główne tabele plików, jeśli wymagają optymalizacji:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@echo off
::
:: This script performs complete defragmentation
:: of all the available non-removable disks.
::

:: set the UltraDefrag specific environment

:: variables according to the user preferences
call "%UD_INSTALL_DIR%\setenv.cmd"

:: perform regular defragmentation of the disks

for /f "skip=7 tokens=1" %%d in ('udefrag -l') do call :defragment %%d

:: optimize master file tables

if "%MFT_OPT_DISKS%" neq "" (
    echo Preparing to optimize the master file tables on %MFT_OPT_DISKS%...
    echo.
    udefrag --optimize-mft %MFT_OPT_DISKS%
    echo.
)

:: enqueue defragmentation of locked files (they will be defragmented using

:: the boot time interface of UltraDefrag at the next system reboot)
if "%BT_DEFRAG_DISKS%" neq "" (
    echo Enqueuing the boot time defragmentation of %BT_DEFRAG_DISKS%...
    echo.
    call "%UD_INSTALL_DIR%\bt.cmd" /E udefrag %BT_DEFRAG_DISKS%
)

:: clear the environment variables used above and exit

call "%UD_INSTALL_DIR%\clearenv.cmd"
set MFT_OPT_DISKS=
set BT_DEFRAG_DISKS=
exit /B 0

::

:: This procedure defragments a single disk.
::
:: Usage:
::
::   call :defragment <drive_letter>
::
:defragment
    echo Preparing to defragment disk %1...
    echo.
   
    :: defragment the disk

    udefrag %1 || exit /B 1
    echo.
   
    :: check the file fragmentation report

    for /f "eol=; tokens=5,6 usebackq" %%a in (`call "%UD_INSTALL_DIR%\show-report.cmd" %1`) do (
        if /i "%%a" equ "locked" set LOCKED_FILES_ARE_FRAGMENTED=1
        if /i "%%b" equ "%1\$MFT" set MFT_IS_FRAGMENTED=1
    )

    :: if the $MFT file is fragmented, enqueue the disk for the MFT optimization

    if "%MFT_IS_FRAGMENTED%" equ "1" set MFT_OPT_DISKS=%MFT_OPT_DISKS% %1
   
    :: if locked files are fragmented, enqueue the disk for the boot time defragmentation

    if "%LOCKED_FILES_ARE_FRAGMENTED%" equ "1" set BT_DEFRAG_DISKS=%BT_DEFRAG_DISKS% %1
   
    :: clear the environment variables used above

    set MFT_IS_FRAGMENTED=
    set LOCKED_FILES_ARE_FRAGMENTED=
exit /B 0

Optymalizacja wszystkich dostępnych dysków

Jeśli zamierzasz optymalizować dyski za pomocą niestandardowego skryptu rozruchowego, rozważ ustawienie UD_SSD_DEFRAGMENT_AND_TRIM_ONLY zmiennej środowiskowej przed optymalizacją dysków, zwłaszcza jeśli w systemie zainstalowane są dyski SSD. Ta opcja znacznie skraca czas optymalizacji dysków SSD bez utraty wydajności. Wydłuża również żywotność dysków SSD.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@echo off
::
:: This script performs the full optimization
:: of all the available non-removable disks
:: except solid state drives (SSD) for which
:: it performs defragmentation and trimming only.
::

call "%UD_INSTALL_DIR%\setenv.cmd"

:: skip slightly fragmented files on SSD

set UD_SSD_FRAGMENTS_THRESHOLD=20

:: during SSD optimization defragment and trim the disk only

set UD_SSD_DEFRAGMENT_AND_TRIM_ONLY=1

udefrag -o --all-fixed

Wykluczanie plików z defragmentacji

Pliki tymczasowe, archiwa i pliki multimedialne z reguły nie mają wpływu na wydajność systemu, dlatego należy je wykluczyć z defragmentacji, aby skrócić czas przetwarzania dysku.

Najprostszym sposobem wykonania tej czynności jest ustawienie wykluczeń za pomocą okna dialogowego ustawień interfejsu graficznego, a następnie użycie setenv w celu ustawienia środowiska wiersza poleceń zgodnie z preferencjami użytkownika, jak pokazano w powyższych przykładach.

Alternatywnie, możesz ustawić wykluczenia za pomocą UD_EX_FILTER zmiennej środowiskowej. Sprawdź poniższy skrypt, aby dowiedzieć się, jak to zrobić. Skrypt wyklucza również duże fragmenty, ponieważ zazwyczaj nie wpływają one na wydajność systemu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
::
:: This script excludes temporary files, archives,
:: multimedia files and big fragments from defragmentation
:: and then defragments all the available non-removable disks.
::

:: exclude temporary files

set UD_EX_FILTER=*system volume information*;*temp*;*tmp*;*recycle*
set UD_EX_FILTER=%UD_EX_FILTER%;*dllcache*;*ServicePackFiles*;*.log
set UD_EX_FILTER=%UD_EX_FILTER%;*Windows.old*

:: exclude archives

set ARCHIVE_PATTERNS=*.7z;*.7z.*;*.arj;*.bkf;*.bz2;*.bzip2;*.cab;*.cpio;*.deb
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.dmg;*.gz;*.gzip;*.lha;*.lzh;*.lzma
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.rar;*.rpm;*.swm;*.tar;*.taz;*.tbz
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.tbz2;*.tgz;*.tpz;*.txz;*.xar;*.xz
set ARCHIVE_PATTERNS=%ARCHIVE_PATTERNS%;*.z;*.zip
set UD_EX_FILTER=%UD_EX_FILTER%;%ARCHIVE_PATTERNS%

:: exclude multimedia files

set AUDIO_PATTERNS=*.aif;*.cda;*.flac;*.iff;*.kpl;*.m3u;*.m4a;*.mid
set AUDIO_PATTERNS=%AUDIO_PATTERNS%;*.mp3;*.mpa;*.ra;*.wav;*.wma
set VIDEO_PATTERNS=*.3g2;*.3gp;*.asf;*.asx;*.avi;*.flv;*.mov;*.mp4
set VIDEO_PATTERNS=%VIDEO_PATTERNS%;*.mpg;*.rm;*.srt;*.swf;*.vob;*.wmv
set UD_EX_FILTER=%UD_EX_FILTER%;%AUDIO_PATTERNS%;%VIDEO_PATTERNS%

:: exclude big fragments

set UD_FRAGMENT_SIZE_THRESHOLD=20MB

:: defragment all the available non-removable disks

udefrag --all-fixed

Wykluczanie dysków z defragmentacji

Jeśli chcesz wykluczyć określone dyski z defragmentacji, możesz to łatwo zrobić za pomocą UD_EX_FILTER zmiennej środowiskowej:

1
2
3
4
5
6
7
8
9
10
11
@echo off
::
:: This script defragments all the available
:: non-removable disks except the disk C:
::

call "%UD_INSTALL_DIR%\setenv.cmd"

set UD_EX_FILTER=%UD_EX_FILTER%;C:\*

udefrag --all-fixed

Można również łatwo wykluczyć dyski posiadające pewien poziom fragmentacji:

1
2
3
4
5
6
7
8
9
10
11
12
@echo off
::
:: This script performs the full optimization
:: of all the available non-removable disks
:: having fragmentation level above 20%.
::

call "%UD_INSTALL_DIR%\setenv.cmd"

set UD_FRAGMENTATION_THRESHOLD=20

udefrag -o --all-fixed

Defragmentacja dysków, którym nie przypisano żadnej litery dysku

Jeśli chcesz defragmentować dyski, do których nie przypisano żadnej litery dysku, możesz użyć systemowego diskpart w celu tymczasowego przypisania liter dysków.

Aby dowiedzieć się, jak to zrobić, sprawdź poniższy skrypt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@echo off
::
:: This script defragments a disk with no drive
:: letter assigned, mounted to C:\Games.
::

set MOUNT_POINT=C:\Games

:: set the UltraDefrag specific environment

:: variables according to the user preferences
call "%UD_INSTALL_DIR%\setenv.cmd"

:: assign a drive letter to the disk

echo Preparing to assign a drive letter to the disk mounted at %MOUNT_POINT%...
echo.
echo select volume=%MOUNT_POINT% > diskpart.script
echo assign letter=t >> diskpart.script
diskpart /s diskpart.script || goto fail
echo.

:: give the diskpart utility 15 seconds to finish its job

timeout /t 15 /nobreak > nul

:: defragment the disk

echo Preparing to defragment the disk...
echo.
udefrag t: || set DEFRAG_FAILED=1
echo.

:: remove the assigned letter

echo Preparing to remove the assigned drive letter...
echo.
echo select volume=%MOUNT_POINT% > diskpart.script
echo remove letter=t >> diskpart.script
diskpart /s diskpart.script

:: give the diskpart utility 15 seconds to finish its job

timeout /t 15 /nobreak > nul

if "%DEFRAG_FAILED%" equ "1" goto fail

:success

del /q diskpart.script
call "%UD_INSTALL_DIR%\clearenv.cmd"
set MOUNT_POINT=
set DEFRAG_FAILED=
exit /B 0

:fail

del /q diskpart.script
call "%UD_INSTALL_DIR%\clearenv.cmd"
set MOUNT_POINT=
set DEFRAG_FAILED=
exit /B 1
Udostępnij tę stronę:
Przewiń do góry angielski