- 1 year ago
Copy specific files within known time range using batch file
To copy specific files within a known time range using a batch file, you can use the FOR
loop to iterate through the files in a directory and the IF
condition to check their creation or modification time. Here's a sample batch script that copies files within a specified time range:
batch@echo off
set "source_directory=C:\path\to\source\folder"
set "destination_directory=C:\path\to\destination\folder"
set "start_time=12:00:00" :: Specify the start time here
set "end_time=18:00:00" :: Specify the end time here
for %%F in ("%source_directory%\*.*") do (
for /F "tokens=1-3 delims=:" %%A in ("%%~tF") do (
set "hour=%%A"
set "minute=%%B"
set "second=%%C"
setlocal enabledelayedexpansion
if !hour! geq 0 (
if !hour! leq 9 set "hour=0!hour!"
)
set "file_time=!hour!:!minute!:!second!"
if "!file_time!" geq "%start_time%" if "!file_time!" leq "%end_time%" (
echo Copying "%%~nxF"...
copy "%%F" "%destination_directory%"
)
endlocal
)
)
Replace C:\path\to\source\folder
with the path of the source folder containing the files you want to copy, and C:\path\to\destination\folder
with the path of the destination folder where you want to copy the files.
Set the start_time
and end_time
variables to specify the time range. The script will copy files created or modified between the specified start and end times.
Save the above script as a .bat
file and run it to copy the files within the specified time range from the source folder to the destination folder.