Results 1 to 10 of 10

Thread: date manipulation!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Question date manipulation!

    A question for you...
    Well here are my scenario...

    User is suppose to enter starting date: dd-mm-yyyy into a textbox provided and enter ending date: dd-mm-yyyy into respective textbox. The program would have to trace the path, dir based on the date entered.

    My problems are how am I suppose to let my vb program to create folders consisting of all files from starting date to the ending date? pls advise..thanks

  2. #2

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Take a look at some of these functions:
    MsgBox Format(Now, "MM")
    MsgBox Format(Now, "MMM")
    MsgBox Format(Now, "MMMM")

    Using these you can verify that the entered date is valid.
    Also the IsDate(enteredDate) would be useful.


    You can add to the dates with:
    myDate = DateAdd("m", 1, Now)
    which adds 1 month. You can also add hours, days or whatever.


    Then the create a folder will need the MKDir function. And possibly the DIR() function to see if it exists already.



    --------OR-----------
    (if I understand your question another way around)
    try these functions
    VB Code:
    1. sFile = Dir$(strDirToLookInto)
    2.     Do
    3.         If FileDateTime(sFile) "in the range that you want" Then
    4.                 CopyFile sFile,strDestinationFolder & "\" & sFile
    5.         end if
    6.         sFile = Dir$
    7.     Loop




    When you have some code almost working, come back for some more specific questions.
    Last edited by JordanChris; Oct 7th, 2002 at 05:30 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    To wokawidget

    Not really...the user will have to enter 2 dates: one for start date and another for end date.
    The path and directory for both date folders are located at
    start date: C:\PG\CW\01-01-2001\ and
    end date: C:\PG\CW\20-01-2001\ respectively.

    I need to save all *.dat files from 01-01-2001 folder until 20-01-2001 folder into a A:\ drive. The A:\ will have to generate the sample output as shown below
    A:\PG\CW\01-01-2001\a.dat
    A:\PG\CW\01-01-2001\b.dat
    A:\PG\CW\01-01-2001\c.dat
    A:\PG\CW\02-01-2001\a.dat
    A:\PG\CW\03-01-2001\a.dat
    A:\PG\CW\04-01-2001\a.dat
    ......until
    A:\PG\CW\20-01-2001\a.dat
    A:\PG\CW\20-01-2001\b.dat
    A:\PG\CW\20-01-2001\c.dat

    Is it clear now? How am I suppose to tackle this problem?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Exclamation To JordanChris

    I manage to create A:\PG\CW folder but unable to create date folder that the user specified.

    Fyi Bck means backup...i need to do backup for the existing folders and files. Below are my snippet for creating folder.

    ---------------------------------------------------------------------------------
    Public Sub PG_Md()

    Dim InitPath, BckDir, DirFlag As String

    '---------------------------------------------------------
    'Initialise Variable
    DirFlag = "N"

    'Define Search Directory
    InitPath = "A:\PG\"
    BckDir = Dir(InitPath, vbDirectory)

    'Search for PG\CW directory
    Do While BckDir <> ""
    If BckDir <> "." And BckDir <> ".." Then
    If GetAttr(InitPath & BckDir) = vbDirectory Then
    If BckDir = "CW" Then
    DirFlag = "Y"
    End If
    End If ' it represents a directory.
    End If
    BckDir = Dir
    Loop

    ' Create CW if not found
    If DirFlag = "N" Then
    MkDir "A:\PG\CW"
    End If

    End Sub
    --------------------------------------------------------------------------------
    Thanks!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    To create the folders with the dates...

    VB Code:
    1. '(min_date is the lowest of the two dates, max_date is the highest)
    2.  
    3. Dim tmp_date As Date
    4.  
    5.   tmp_date = min_date
    6.   Do While tmp_date <= max_date
    7.     MkDir "A:\PG\CW\" & Format(tmp_date, "dd-mm-yyyy")
    8.     tmp_date = DateAdd("d", 1, tmp_date)
    9.   Loop

    For the error handling on creating the folders there are two main methods - the one you have, and ignoring all errors (by having an extra line just before this code: On Error Resume Next ). In this case I think that your method would be better. (you would be better of making a function to do the Dir bit and MkDir if needed).

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    You will probably need to make use of the FSO, so add a reference to Microsoft Scripting Runtime.
    You will need a process something like:
    Code:
    Set up and validate YourStartDate
    Set up and validate YourEndDate
    Set up the FSO
    Set FolderBase to "C:\pg\cw"
    For each thisFolder in FolderBase.Folders
      If thisFolder > YourStartDate
        If thisFolder < YourEndDate
            Check thisFolder is present on A:\PG\CW
            For each file in thisFolder.Files
                CopyFile file to A:\PG\CW & thisFolder
            Next
        End If
      End If
    Next
    Set up routines in VB to:
    - enter and validate a date from the user.
    - check a folder exists
    - Create fodlers as required (see previous post)
    - check a date is before or after another date

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Dear JordanChris

    Thanks a lot..
    As a new programmer, I still have doubts for your instructions...
    What is FSO and what does it do?
    As for your previous code..(the earlier one), it doesn't output the way it should. perhaps you can give me any extra advice...thanks

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    FSO = File Scripting Object. Allows you to do things with files, like stepping through every file in a folder, or every sub-folder in a folder. Its not strictly needed, but using it will be easier for you than trying to do recursion with DIR() functions.

    As for your previous code..(the earlier one), it doesn't output the way it should.
    Yes it does. But it might not output the way you want it to! :-)
    Tell us which code, and what you need to be different, AND what you have tried so far so we don't duplicate your work.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Question Did something..need ur help now

    Hello here is the attachment of my program, i have highlighted my questions with remarks....the bottom part. The top portion generally works well...
    the bottom part, .... I can't seem to get the date right yet. please advise....thanks in advance
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width