1.

Solve : Moving files based on their created date?

Answer»

Hi,

I wanted to move files from one folder to another folder based on their created "MONTH". using batch script...

For example, assume the following

Source folder - c:\New_folder
Dest folder - C:\dest

New_folder has few files that were created on January month and it has few files created on FEBRUARY month. I wanted to move files that were created on January month ONLY from source to destination?

Can you please help on this?

Thanks,

Thiru

What do you have so far ?Code: [Select]@echo off
dir /TC *.bat | findstr "2010"

rem dir /TC *.bat | findstr "01/"

rem dir /TC *.bat | findstr "01/" > mvdate.txt

dir /TC *.bat | findstr "2010" > mvdate.txt

for /f "tokens=5" %%i in (mvdate.txt) do echo %%i

for /f "tokens=5" %%i in (mvdate.txt) do copy %%i c:\dest\

rem for /f "tokens=5" %%i in (mvdate.txt) do mv %%i c:\dest\

dir c:\dest\
Output:
C:\>mvdate.bat
01/02/2010 11:09 AM 505 100files.bat
01/15/2010 10:35 AM 256 casp.bat
01/11/2010 12:47 PM 467 ckzero.bat
01/10/2010 11:59 AM 195 contin.bat
01/10/2010 01:40 PM 130 go2.bat
01/11/2010 01:03 PM 238 listdir.bat
01/08/2010 09:46 AM 288 listDirec_astxt.bat
01/11/2010 08:15 PM 510 mp.bat
01/18/2010 11:36 AM 295 mvdate.bat
01/10/2010 09:25 AM 249 namepic5.bat
01/02/2010 11:44 AM 160 namfiles.bat
01/10/2010 07:56 AM 252 partial.bat
01/10/2010 03:40 PM 350 shutter.bat
01/04/2010 05:17 PM 335 st10.bat
01/10/2010 02:00 PM 366 twit.bat
01/14/2010 03:50 PM 739 Vincenator.bat
01/12/2010 08:51 AM 34 xmail.bat
01/06/2010 03:40 PM 902 yesterday.bat
100files.bat
casp.bat
ckzero.bat
contin.bat
go2.bat
listdir.bat
listDirec_astxt.bat
mp.bat
mvdate.bat
namepic5.bat
namfiles.bat
partial.bat
shutter.bat
st10.bat
twit.bat
Vincenator.bat
xmail.bat
yesterday.bat
file(s) copied.

Volume Serial Number is F4A3-D6B3

Directory of c:\dest

01/18/2010 11:47 AM .
01/18/2010 11:47 AM ..
01/02/2010 12:05 PM 505 100files.bat
01/15/2010 10:40 AM 256 casp.bat
01/11/2010 12:55 PM 467 ckzero.bat
01/10/2010 12:09 PM 195 contin.bat
01/10/2010 01:41 PM 130 go2.bat
01/11/2010 01:11 PM 238 listdir.bat
01/08/2010 02:55 PM 288 listDirec_astxt.bat
01/11/2010 08:17 PM 510 mp.bat
01/18/2010 11:48 AM 295 mvdate.bat
01/10/2010 09:29 AM 249 namepic5.bat
01/02/2010 11:47 AM 160 namfiles.bat
01/10/2010 08:01 AM 252 partial.bat
01/10/2010 04:29 PM 350 shutter.bat
01/04/2010 05:18 PM 335 st10.bat
01/10/2010 02:15 PM 366 twit.bat
01/14/2010 04:11 PM 739 Vincenator.bat
01/12/2010 08:52 AM 34 xmail.bat
01/06/2010 03:40 PM 902 yesterday.bat
18 File(s) 6,271 bytes
2 Dir(s) 303,166,734,336 bytes free

C:\>What happens if the script is not run until 1 Feb, will it not also move files created in Feb?

Quote from: Dusty on January 18, 2010, 02:14:39 PM

What happens if the script is not run until 1 Feb, will it not also move files created in Feb?


dir /TC *.bat | findstr "01/"

(not tested yet. I'm not on my computer.)But 01/ could be the date day as in mm/dd/yyyy 02/01/2010.

Also what happens if the users date format is not mm/dd/yyyy but is in some other permissable format, say dd-mm-yyyy?

Quote from: Dusty on January 18, 2010, 06:10:18 PM
But 01/ could be the date day as in mm/dd/yyyy 02/01/2010.

Also what happens if the users date format is not mm/dd/yyyy but is in some other permissable format, say dd-mm-yyyy?

I'm not Thiru, the Original Poster. I cannot answer your questions.

Thiru, the Original Poster needs a new design and new solution.

Dusty please provide your solution.BillRichardson :

I'm not Thiru, the Original Poster. I cannot answer your questions.

Thiru, the Original Poster needs a new design and new solution.

Dusty please provide your solution.

-------------------------------------------------------------------------------------------------------------

Hi all,

Thanks to all for your reply.

I have "MM/DD/YYYY" date format in my system.

And as discussed below, we have confusion between MM and DD... "01" can be month and it can be also date in february month.. So I do not know how to filter...

can we use "/01/" as finding string. If it has "/" at both the ends (begining and end), it should be DD only... If it has "/" on the right side end ONLY, it should be MONTH..

Am I making sense?
Please correct if it is wrong!!

Thanks again. Quote from: thiru
can we use "/01/" as finding string. If it has "/" at both the ends (begining and end), it should be DD only... If it has "/" on the right side end ONLY, it should be MONTH..

Think about the actual dates 01/01/2010 and 02/01/2010 in the format mm/dd/yyyy. If /01/ is used as the search argument the day will be found but that's not what you want, if the search argument 01/ is used the month will be found in the first date but again the day will be found in the second date because 01/ exists in that date too as 02/01/2010.

What has to happen is that the month part of the date must be identified. I think this is achieved this in the following reply. Fingers crossed.Quote from: Billrich
Dusty please provide your solution.
Aw darnit, do I have to?? Bit of a rush on this one..

In the following script no allowance is made for paths/filenames containing spaces. The permitted date formats require the month component to be numeric, if the month is expressed as Jan Feb etc the script will fail. The script is not fully tested. If/when the user is satisfied with the script's performance the 4th and 5th lines should be Rem'd and Copy in the ultimate For loop CHANGED to Move.

Code: [Select]@echo off
cls
setlocal enabledelayedexpansion

if exist c:\dest\*.* echo y | del dest\*.* > nul
if exist c:\dest\ rd c:\dest\

:: Establish date format..
for /f "skip=1 tokens=1-4 delims=(/.-)" %%1 in ('echo. ^| date') do (
set first=%%2
set secnd=%%3
set third=%%4
)

:: Set month position in date format..
if /i %first% equ mm set month=1
if /i %secnd% equ mm set month=2
if /i %third% equ mm set month=3

:: Create output directory if not exist..
if not exist c:\dest\ md c:\dest\

:: Examine file date & copy/move those which fit criteria specified..
pushd c:\new_folder\
for /f %%A in ('dir /b /tc') do (
set datime=%%~tA
for /f "tokens=1-3* delims=/.- " %%1 in ("!datime!") do (
if %%%month% equ 1 copy %%A c:\dest\ > nul
)
)
popd
echo.&echo.

:: Display output files for checking purposes..
dir c:\dest\*.*

exit /b

Hope this helps.

Hi Dusty,

Yep. You are correct!!

01/01/2010 and 02/01/2010 are the dates that we took for example here.

Shall we use IF statement using "AND".... Like this find "01/" but NOT "/01/"..

Is this correct?

Thanks,
Thiru
Hi Thiru.

Go ahead and try what you suggest if you prefer to use Find which is not one of my FAVORITE tools.

Quote from: Dusty on January 19, 2010, 03:30:36 AM
Hi Thiru.

Go ahead and try what you suggest if you prefer to use Find which is not one of my favorite tools.




Dusty,

I'm a beginner for Batch Script. However I have used the code which you posted here. Is it working fine? I did not GET any output from it.

Please help me.

Thanks,
Thirulearn how to use vbscript, which is better at date processing and many other stuff.

Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
strDestination = objArgs(1)
Set objFolder =objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName = strFile.Name
strDate = strFile.DateLastModified
mth=Month(strDate)
' move files in January
If mth = 1 Then
WScript.Echo "copying .."&strFileName & " " & strDate
objFS.MoveFile strFileName , strDestination & "\" & strFileName
End If
Next


save as mymove.vbs and on command line

Code: [Select]c:\test> cscript //nologo mymove.vbs c:\test c:\destination
Thiru - yes, it's working for me. Hate to ask you, did you set up the input directory with files created in Jan?

Found a bug, the dir command in my original script is failing and defaulting to C:\ - code amended.


Discussion

No Comment Found