Solved

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

Posted on 2007-03-28
7
176 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.

856 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