Avatar of arundelr
arundelr

asked on 

Moving files based on month and year created in an batch file or vb script

Hi,

I am looking for a way to move files based on file dates, this would need to be a batch file or some kind of script, I want to be able to do this by month/year so If the files were created on any day in Jan 2005 for example it would move only those and ignore the rest.

I have various folders full of files like

C:\Master_folder

Then under these some more etc

Folder1
Folder2

I want to move the files outside of the C:\Master_folder and into J:\Master_folder

But keeping the folders intact on the c:\ and also putting them into folders with the same name on j:\
Windows BatchVB Script

Avatar of undefined
Last Comment
SteveGTR
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Try this:

@echo off

setlocal enabledelayedexpansion

set srcDir=C:\Master_folder
set destDir=J:\Master_folder
set cutoff=200501

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /b /a-d /s 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS "%~2"

if not "%yy%%mm%"=="%cutoff%" goto :EOF

set rootName=%~1
set rootName=!rootName:%srcDir%\=!

echo Copying %~1 to %destDir%\%rootName%
echo F|xcopy "%~1" "%destDir%\%rootName%" >NUL

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

Good Luck,
Steve
Avatar of arundelr
arundelr

ASKER

Hi Mate,

I tried the script and its almost perfect, one problem is that it only copies the files and doesnt move them, is it possible to do this with a move or if not a delete on the source after sucsusful copy?

Many thanks

Rob.

Avatar of SteveGTR
SteveGTR
Flag of United States of America image

I didn't even see the move part. I inserted an if statement and delete the file if XCOPY returns a 0.

@echo off

setlocal enabledelayedexpansion

set srcDir=C:\Master_folder
set destDir=J:\Master_folder
set cutoff=200501

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /b /a-d /s 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS "%~2"

if not "%yy%%mm%"=="%cutoff%" goto :EOF

set rootName=%~1
set rootName=!rootName:%srcDir%\=!

echo Copying %~1 to %destDir%\%rootName%
echo F|xcopy "%~1" "%destDir%\%rootName%" >NUL
if "%errorlevel%"="0" del "%~1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
Avatar of arundelr
arundelr

ASKER

Hi,

I tried the revised script and it seems like its removing all the source files for the specified month but only copying one of them into the target folder.  Very odd.
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of arundelr
arundelr

ASKER

A+ - Top of the class

Thanks Steve !
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Excellent! Sorry about the syntax error. That's what I get for not testing.
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo