Draft a Windows batch file script for Rclone Portable that: - auto-detects that the source USB drives are mounted - checks that the remote Nextcloud accounts are online - suggests corrective action when required - select an individual account or all a
Below is a sample Windows batch file script for Rclone Portable that meets your requirements. This script handles various functionalities such as auto-detecting USB drives, checking remote Nextcloud accounts, managing logs, settings, and more.
### Rclone Batch Script `rclone_batch_script.bat`
```batch
@echo off
setlocal enabledelayedexpansion
:: Set variables
set "RCLONE_DIR=.\Programs\Rclone"
set "CONFIG_DIR=.\Documents\Profile\Settings"
set "SOURCE_DIR=.\Documents"
set "LOG_FILE=mirror_log.txt"
set "TEMP_DIR=%TEMP%"
set "RETRY_LIMIT=3"
set "DEFAULT_REMOTE=default"
set "LOG_RETENTION=10"
set /a "TIMEOUT=10"
:: Initialize settings
if not exist "%CONFIG_DIR%\rclone.conf" (
echo Configuration file not found!
exit /b
)
:: Function to check if USB drives are mounted
:CheckUSB
set "USB_DRIVES_FOUND=false"
for /f "tokens=1" %%a in ('wmic logicaldisk where "drivetype=2" get deviceid ^| findstr /r /v "^$"') do (
set "USB_DRIVES_FOUND=true"
echo Found USB drive: %%a
)
if not "%USB_DRIVES_FOUND%"=="true" (
echo No USB drives found! Please connect a USB drive.
pause
exit /b
)
:: Function to check if accounts are online
:CheckAccounts
echo Checking account status...
rclone listremotes --config "%CONFIG_DIR%\rclone.conf" >nul 2>&1
if errorlevel 1 (
echo Unable to connect to remote accounts! Check your internet connection or credentials.
pause
exit /b
)
echo All accounts are online.
:: Menu function
:Menu
cls
echo ===========================
echo Rclone Mirror Utility
echo ===========================
echo Tap or press a key:
echo 1 Check and clear temp folder
echo 2 Mirror
echo L View Logs
echo F Find in Logs
echo R Set Retry Limit
echo D Set Default Remote
echo G Set Log Retention
echo V View Current Settings
echo X Reset to Defaults
echo B Backup Settings
echo O Restore Settings
echo Q Stop Mirror
echo H Help
echo S System info
echo A About version
set /p choice=Enter your choice:
goto handle_choice
:handle_choice
if /i "%choice%"=="1" goto CheckTemp
if /i "%choice%"=="2" goto Mirror
if /i "%choice%"=="L" goto ViewLogs
if /i "%choice%"=="F" goto FindLogs
if /i "%choice%"=="R" goto SetRetry
if /i "%choice%"=="D" goto SetDefault
if /i "%choice%"=="G" goto SetLogRetention
if /i "%choice%"=="V" goto ViewSettings
if /i "%choice%"=="X" goto ResetDefaults
if /i "%choice%"=="B" goto BackupSettings
if /i "%choice%"=="O" goto RestoreSettings
if /i "%choice%"=="Q" goto StopMirror
if /i "%choice%"=="H" goto Help
if /i "%choice%"=="S" goto SystemInfo
if /i "%choice%"=="A" goto About
goto Menu
:: Function to check and clear temp folder
:CheckTemp
echo Clearing temporary files...
del /q "%TEMP%\*.*"
echo Temporary files cleared.
pause
goto Menu
:: Function to mirror files
:Mirror
set "MIRROR_LIST="
echo Available accounts:
for /f "tokens=1" %%a in ('rclone listremotes --config "%CONFIG_DIR%\rclone.conf"') do (
echo %%a
)
set /p "account=Choose an account: "
echo Available folders for %account%:
rclone lsd %account%: --config "%CONFIG_DIR%\rclone.conf"
set /p "folder=Choose a folder (or leave blank for all): "
set "EXCLUDE_PARAMS=--exclude *.log --exclude *.tmp --exclude .DS_Store --exclude .git/**"
set "FOLDER_PARAM=%account%:%folder%"
echo Mirroring from %FOLDER_PARAM%...
:: List files and confirm before mirroring
echo Listing files to be mirrored...
rclone lsl %FOLDER_PARAM% %EXCLUDE_PARAMS% --config "%CONFIG_DIR%\rclone.conf" > temp_file_list.txt
type temp_file_list.txt
echo Continue mirroring? (y/n)
set /p "continue_choice="
if /i "%continue_choice%"=="y" (
rclone sync %FOLDER_PARAM% %SOURCE_DIR% --config "%CONFIG_DIR%\rclone.conf" --log-file="%LOG_FILE%" --log-level=INFO %EXCLUDE_PARAMS%
echo Mirroring completed! Check the log for details.
echo %date% %time% : Mirroring completed >> %LOG_FILE%
call :SoundBell
) else (
echo Mirroring canceled by user.
)
goto Menu
:: Function to view logs
:ViewLogs
echo Viewing logs...
type "%LOG_FILE%"
pause
goto Menu
:: Function to find in logs
:FindLogs
set /p "search_term=Enter term to search in logs: "
findstr /i "%search_term%" "%LOG_FILE%"
pause
goto Menu
:: Functions to set and manage retry limits
:SetRetry
set /p "new_limit=Enter new retry limit (default is %RETRY_LIMIT%): "
if "%new_limit%"=="" set new_limit=%RETRY_LIMIT%
set "RETRY_LIMIT=%new_limit%"
echo Retry limit set to %RETRY_LIMIT%.
pause
goto Menu
:: Functions to set and view default remote
:SetDefault
set /p "new_default=Enter new default remote (current is %DEFAULT_REMOTE%): "
if "%new_default%"=="" set new_default=%DEFAULT_REMOTE%
set "DEFAULT_REMOTE=%new_default%"
echo Default remote set to %DEFAULT_REMOTE%.
pause
goto Menu
:: Function to set log retention
:SetLogRetention
set /p "new_retention=Enter new log retention count (default is %LOG_RETENTION%): "
if "%new_retention%"=="" set new_retention=%LOG_RETENTION%
set "LOG_RETENTION=%new_retention%"
echo Log retention set to %LOG_RETENTION%.
pause
goto Menu
:: Function to view current settings
:ViewSettings
echo Current settings:
echo Retry Limit: %RETRY_LIMIT%
echo Default Remote: %DEFAULT_REMOTE%
echo Log Retention: %LOG_RETENTION%
pause
goto Menu
:: Function to reset settings to defaults with auto-backup
:ResetDefaults
echo Backing up current settings...
copy "%CONFIG_DIR%\settings_backup.txt" "%CONFIG_DIR%\settings.bak" > nul
set "RETRY_LIMIT=3"
set "DEFAULT_REMOTE=default"
set "LOG_RETENTION=10"
echo Settings reset to defaults.
pause
goto Menu
:: Function to backup settings
:BackupSettings
copy "%CONFIG_DIR%\rclone.conf" "%CONFIG_DIR%\backup_config.conf"
echo Backup completed.
pause
goto Menu
:: Function to restore settings
:RestoreSettings
copy "%CONFIG_DIR%\backup_config.conf" "%CONFIG_DIR%\rclone.conf"
echo Restore completed.
pause
goto Menu
:: Function to handle stopping mirror
:StopMirror
echo Stopping mirror...
exit /b
:: Help function
:Help
echo Help section:
echo - Ensure Rclone is configured correctly.
echo - Check for USB connection before proceeding.
echo - Make log backups regularly.
pause
goto Menu
:: Function to display system info
:SystemInfo
echo System Info:
systeminfo
pause
goto Menu
:: About functionality
:About
echo About Rclone Mirror Utility Version 1.0
pause
goto Menu
:: Function to sound a bell
:SoundBell
echo ^G
exit /b
```
### Sample `rclone.conf` with multiple accounts
```ini
[gdrive]
type = drive
client_id = your_client_id
client_secret = your_client_secret
scope = drive
token = {"access_token": "your_access_token", "token_type": "Bearer", "refresh_token": "your_refresh_token", "expiry": "2023-11-01T00:00:00.000Z"}
[nextcloud1]
type = webdav
url = https://nextcloud.example.com/remote.php/dav/files/
vendor = nextcloud
user = your_username
pass = your_password
[nextcloud2]
type = webdav
url = https://nextcloud2.example.com/remote.php/dav/files/
vendor = nextcloud
user = another_username
pass = another_password
```
### Notes:
- Ensure that Rclone is properly installed in the specified directory (`RCLONE_DIR`).
- The script used here assumes that you store sensitive configurations (like passwords and tokens) securely and not directly in the `.conf` file since plaintext storage can expose sensitive data.
- This script is a basic template; you might need to enhance it further based on your use case and environment security policies.
- Remember to run this script as an administrator if required, depending on your system settings.
- Modify and adapt the paths as necessary for your setup.
- Regularly update your `rclone.conf` with valid credentials to avoid interruptions.


