1.6 - פרויקט - פתרון
@echo off
setlocal enabledelayedexpansion
:: Get the current date in YYYY-MM-DD format
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set day=%%a
set month=%%b
set year=%%c
)
set date=%year%-%month%-%day%
:: Create the backup folder if it doesn't exist
set backup_dir=C:\Backup\Backup_%date%
if not exist "%backup_dir%" mkdir "%backup_dir%"
:: Perform the file backup
xcopy "C:\Users\<username>\Documents\*" "%backup_dir%\" /s /e /y > "%backup_dir%\backup_log.txt"
:: Add the backup date and time to the log file
echo Backup completed on %date% at %time% >> "%backup_dir%\backup_log.txt"
:: Display a message to the user
echo Backup was successful! Files are saved in "%backup_dir%" and logged in backup_log.txt.
pause
הסבר על הסקריפט:
- תחילה, אנחנו מקבלים את התאריך הנוכחי בפורמט
YYYY-MM-DDעל ידי פיצול תוצאת הפקודהdate /t. - לאחר מכן, אנחנו בודקים אם התיקייה של הגיבוי קיימת כבר, ואם לא – אנחנו יוצרים אותה.
- הסקריפט מעתיק את כל הקבצים מתוך תיקיית
Documentsלתיקיית הגיבוי החדשה, כולל תיקיות משנה (/s /e), ומוודא שהקבצים לא ידרשו אישור נוסף אם הם קיימים כבר בתיקיית היעד (/y). - כל המידע על הגיבוי, כולל התאריך ושעת ההרצה, מתווסף לקובץ
backup_log.txt. - בסיום, מוצגת הודעה למשתמש על הצלחת הגיבוי.