ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Batch File Copy with Progress Indicator

Batch

Goal -- WPM

Ready
Exercise Algorithm Area
1@echo off
2
3REM Script to copy files from a source directory to a destination directory with a progress indicator.
4REM This script demonstrates basic file operations and loop constructs in Batch.
5
6setlocal enabledelayedexpansion
7
8REM --- Configuration ---
9set "SOURCE_DIR=C:\SourceFiles"
10set "DEST_DIR=D:\BackupFiles"
11set "FILE_PATTERN=*.*"
12
13REM --- Pre-checks ---
14if not exist "%SOURCE_DIR%" (
15echo Error: Source directory "%SOURCE_DIR%" not found.
16goto :eof
17)
18
19if not exist "%DEST_DIR%" (
20echo Creating destination directory: "%DEST_DIR%"
21mkdir "%DEST_DIR%"
22if errorlevel 1 (
23echo Error: Could not create destination directory "%DEST_DIR%" due to insufficient permissions.
24goto :eof
25)
26)
27
28REM --- Initialization ---
29echo Starting file copy from "%SOURCE_DIR%" to "%DEST_DIR%"...
30set /a total_files=0
31set /a copied_files=0
32
33REM Count total files first to calculate progress percentage accurately
34for %%F in ("%SOURCE_DIR%\%FILE_PATTERN%") do (
35set /a total_files+=1
36)
37
38if %total_files% equ 0 (
39echo No files found matching pattern "%FILE_PATTERN%" in "%SOURCE_DIR%" to copy.
40goto :eof
41)
42
43REM --- Main Copy Loop ---
44echo Total files to process: %total_files%
45
46for %%F in ("%SOURCE_DIR%\%FILE_PATTERN%") do (
47set "current_file=%%~nxF"
48echo Copying "!current_file!"...
49
50REM Copy the file
51copy "%SOURCE_DIR%\!current_file!" "%DEST_DIR%\!current_file!" > nul
52
53REM Check for copy errors
54if errorlevel 1 (
55echo Error copying "!current_file!". Check permissions or disk space.
56) else (
57set /a copied_files+=1
58)
59
60REM Calculate and display progress
61set /a progress = (copied_files * 100) / total_files
62echo Progress: !progress!%% (Copied: !copied_files! / !total_files!)
63)
64
65REM --- Completion ---
66echo.
67echo File copy process completed.
68echo Successfully copied %copied_files% out of %total_files% files.
69
70endlocal
71exit /b 0
Algorithm description viewbox

Batch File Copy with Progress Indicator

Algorithm description:

This Batch script automates the process of copying files from a specified source directory to a destination directory. It includes a basic progress indicator that shows the percentage of files copied and the count of files processed. This is useful for simple backup tasks or migrating data between locations where manual tracking would be tedious.

Algorithm explanation:

The script utilizes a `for` loop to iterate through files matching a pattern in the source directory. It first counts the total number of files to enable accurate percentage calculation. Inside the loop, it copies each file using the `copy` command and checks the `errorlevel` to detect any issues during the copy operation. A progress percentage is calculated and displayed after each file is processed. The script handles edge cases such as the source directory not existing, the destination directory not existing (and attempts to create it), and no files being found matching the pattern. It also provides basic error reporting for individual file copy failures. The time complexity is O(N*M) where N is the number of files and M is the average time to copy a file, as each file is processed sequentially. The space complexity is O(1) as it only uses a few variables to store counts and paths.

Pseudocode:

1. Define source directory, destination directory, and file pattern.
2. Check if source directory exists; exit if not.
3. Check if destination directory exists; create it if not, exit on failure.
4. Initialize total_files and copied_files counters to 0.
5. Loop through files in source directory matching pattern to count total_files.
6. If total_files is 0, exit.
7. Loop through each file in source directory matching pattern:
   a. Get current file name.
   b. Copy file to destination directory.
   c. If copy was successful, increment copied_files.
   d. Calculate progress percentage: (copied_files * 100) / total_files.
   e. Display progress.
8. Display completion message with total and successfully copied file counts.