Solved

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

Posted on 2007-03-28
7
172 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now