Solved

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

Posted on 2007-03-28
7
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

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 500 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

Technology Partners: 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!

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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