| 1. |
Solve : Moving files based on their created date? |
|
Answer» Hi, 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. 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. 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. |
|