?
Solved

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

Posted on 2007-03-28
7
Medium Priority
?
185 Views
Last Modified: 2010-04-16
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:\
0
Comment
Question by:arundelr
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18808094
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
0
 

Author Comment

by:arundelr
ID: 18808324
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.

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18808430
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:arundelr
ID: 18808518
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.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 total points
ID: 18808603
There was a syntax error in the if errorlevel statement:

@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
0
 

Author Comment

by:arundelr
ID: 18808656
A+ - Top of the class

Thanks Steve !
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18808663
Excellent! Sorry about the syntax error. That's what I get for not testing.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question