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

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

Simple File Copy with Retry Loop

Batch

Goal -- WPM

Ready
Exercise Algorithm Area
1@echo off
2
3REM Configuration
4set SOURCE_FILE=C:\Temp\source.txt
5set DEST_FILE=C:\Temp\destination.txt
6set MAX_RETRIES=3
7set RETRY_DELAY_SECONDS=5
8
9REM Main logic
10echo Attempting to copy '%SOURCE_FILE%' to '%DEST_FILE%'.
11
12set ATTEMPTS=0
13:CopyLoop
14
15REM Increment attempt counter
16set /a ATTEMPTS+=1
17
18REM Attempt to copy the file
19copy /Y "%SOURCE_FILE%" "%DEST_FILE%"
20
21REM Check if copy was successful
22if not errorlevel 1 (
23echo File copied successfully.
24goto :EOF
25)
26
27REM If copy failed, check if we have retries left
28if %ATTEMPTS% LSS %MAX_RETRIES% (
29echo Copy failed. Retrying in %RETRY_DELAY_SECONDS% seconds (Attempt %ATTEMPTS%/%MAX_RETRIES%)...
30REM Wait for the specified delay
31timeout /t %RETRY_DELAY_SECONDS% /nobreak > nul
32goto CopyLoop
33)
34
35REM If all retries failed
36echo Final attempt failed. Could not copy file.
37goto :EOF
Algorithm description viewbox

Simple File Copy with Retry Loop

Algorithm description:

This script copies a single file from a source location to a destination. If the initial copy operation fails, it automatically retries the operation a set number of times with a short delay between attempts. This is useful for transient network issues or temporary file locks, ensuring that the operation eventually succeeds if the problem is temporary.

Algorithm explanation:

The script uses a simple `goto` loop to implement retries. It initializes an attempt counter and enters the loop. Inside the loop, it attempts to copy the file. If the `errorlevel` is not 0 (indicating failure), it checks if the number of attempts is less than the maximum allowed retries. If so, it waits for a specified delay using `timeout` and then jumps back to the beginning of the loop. If all retries are exhausted, it prints a final failure message. This demonstrates basic retry loop patterns and error checking in batch scripting.

Pseudocode:

1. Define source file path, destination file path, maximum retries, and retry delay.
2. Initialize an attempt counter to 0.
3. Start a loop:
   a. Increment the attempt counter.
   b. Attempt to copy the source file to the destination.
   c. Check the error level after the copy operation.
   d. If the error level is 0 (success):
      i. Print a success message.
      ii. Exit the script.
   e. If the error level is not 0 (failure):
      i. Check if the current attempt count is less than the maximum retries.
      ii. If yes:
         - Print a retry message.
         - Wait for the specified retry delay.
         - Go back to the start of the loop.
      iii. If no (all retries exhausted):
         - Print a final failure message.
         - Exit the script.