Copy Files and Folder Structure Recursively between a Date Range
You can use PowerShell and Get-ChildItem to get the LastWriteTime
attribute values of files recursively in a starting source directory.
For the source files that have the LastWriteTime
attribute value within the date range, you can replace the source folder value with the destination folder value, and then use Copy-Item with the -Recurse -Force
parameters to create the matching structure and copy over the files.
Note: I wrote this in both batch script logic and PowerShell script logic below for you.
Batch
Important: Just set the topmost variable values (src=
and dest=
) for the source and destination folders, and also for the to and from
date values for the range you need (Date_From=
and Date_To=
) in MM/DD/YYYY
format as set in those values in the below script example.
SET "src=c:\source\original"
SET "dest=c:\temp\new"
SET "Date_From=01/01/2017"
SET "Date_To=02/26/2017"
CALL :PSScript "%src%" "%dest%" "%Date_From%" "%Date_To%"
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
EXIT
:PSScript
SET PSScript=%temp%\~tmp%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO $src ^= "%~1"; >>"%PSScript%"
ECHO $dest ^= "%~2"; >>"%PSScript%"
ECHO $Date_From ^= "%~3"; >>"%PSScript%"
ECHO $Date_To ^= "%~4"; >>"%PSScript%"
ECHO $Date_From ^=[datetime]$Date_From; >>"%PSScript%"
ECHO $Date_To ^=[datetime]$Date_To; >>"%PSScript%"
ECHO $srcFiles ^= ^(Get-ChildItem -Path $src -Recurse^); >>"%PSScript%"
ECHO $srcFiles ^| %% {If^(^($_.LastWriteTime -ge $Date_From^) -or ^($_.LastWriteTime -le $Date_To^)^)>>"%PSScript%"
ECHO { >>"%PSScript%"
ECHO $dPath ^= ^($_.FullName.Replace^($src,$dest^)^); >>"%PSScript%"
ECHO Copy-Item $_.FullName -destination $dPath -Recurse -Force; >>"%PSScript%"
ECHO } >>"%PSScript%"
ECHO }; >>"%PSScript%"
GOTO :EOF
PowerShell
Important: Just set the topmost variable values ($src=
and $dest=
) for the source and destination folders, and also for the to and from
date values for the range you need ($Date_From=
and $Date_To=
) in MM/DD/YYYY
format as set in those values in the below script example.
$src = "c:\source\original";
$dest = "c:\temp\new";
$Date_From = "01/01/2017";
$Date_To = "02/26/2017";
$Date_From = [datetime]$Date_From;
$Date_To = [datetime]$Date_To;
$srcFiles = (Get-ChildItem -Path $src -Recurse);
$srcFiles | % {If(($_.LastWriteTime -ge $Date_From) -or ($_.LastWriteTime -le $Date_To))
{
$dPath = ($_.FullName.Replace($src,$dest));
Copy-Item $_.FullName -destination $dPath -Recurse -Force ;
}
};
Further Resources
How can I copy 10 files to a new directory and rename them with a pattern?
Use the following batch file (test.cmd):
@echo off
setlocal EnableDelayedExpansion
set "source_dir=f:\test\jpg"
set "target_dir=f:\test\target"
for /f "tokens=*" %%f in ('dir /b %source_dir%\*.jpg') do (
set /a "count+=1"
set /a "target_count=!count!+100"
copy "%source_dir%\%%f" "!target_dir!\e!target_count!.jpg" > nul
if !count! EQU 10 goto :done
)
rem finished
:done
endlocal
Notes:
- Change
source_dir
and target_dir
as appropriate
Example:
> dir jpg
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test\jpg
12/03/2019 11:39 <DIR> .
12/03/2019 11:39 <DIR> ..
12/03/2019 11:34 4,429 Test_image (01).jpg
12/03/2019 11:34 4,429 Test_image (02).jpg
12/03/2019 11:34 4,429 Test_image (03).jpg
12/03/2019 11:34 4,429 Test_image (04).jpg
12/03/2019 11:34 4,429 Test_image (05).jpg
12/03/2019 11:34 4,429 Test_image (06).jpg
12/03/2019 11:34 4,429 Test_image (07).jpg
12/03/2019 11:34 4,429 Test_image (08).jpg
12/03/2019 11:34 4,429 Test_image (09).jpg
12/03/2019 11:34 4,429 Test_image (10).jpg
12/03/2019 11:34 4,429 Test_image (11).jpg
12/03/2019 11:34 4,429 Test_image (12).jpg
12/03/2019 11:34 4,429 Test_image (13).jpg
12/03/2019 11:34 4,429 Test_image (14).jpg
12/03/2019 11:34 4,429 Test_image (15).jpg
12/03/2019 11:34 4,429 Test_image (16).jpg
16 File(s) 70,864 bytes
2 Dir(s) 1,005,493,501,952 bytes free
> test
> dir target
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test\target
12/03/2019 12:07 <DIR> .
12/03/2019 12:07 <DIR> ..
12/03/2019 11:34 4,429 e101.jpg
12/03/2019 11:34 4,429 e102.jpg
12/03/2019 11:34 4,429 e103.jpg
12/03/2019 11:34 4,429 e104.jpg
12/03/2019 11:34 4,429 e105.jpg
12/03/2019 11:34 4,429 e106.jpg
12/03/2019 11:34 4,429 e107.jpg
12/03/2019 11:34 4,429 e108.jpg
12/03/2019 11:34 4,429 e109.jpg
12/03/2019 11:34 4,429 e110.jpg
10 File(s) 44,290 bytes
2 Dir(s) 1,005,493,379,072 bytes free
Further Reading
Best Answer
Iterate over the text file:
Copy the files:
Done: