locked
VB Script to move a file with date in name RRS feed

  • Question

  • I'm so stuck but I know Im close.  So its friday and I'm brain dead and I'm asking for some help please.

    I have FILES starting with dates

    140729HDF0.835

    140730HES0.835

    140731UHG0.835

    and I need to move the files that are dated in the file's 2 days back.

    Dim ObjFso 
    Dim SourceLocation 
    Dim DestinationLocation 
    Dim FileName
    Set ObjFso = CreateObject("Scripting.FileSystemObject")  
    dteYesterday = DateAdd("d", -1, Date) 
    SourceLocation = "c:\835old" 
    DestinationLocation = "c:\835new" 
    FileName = (Year(dteYesterday), 2) & Right("0" & Month(dteYesterday), 2) & Right("0" & Day(dteYesterday), 2) & ".835"
    ObjFso.MoveFile SourceLocation & "\" & FileName, DestinationLocation & "\"

    It's something in my FileName= because if I simply change it to

    FileName = "*.835"
    its works.  Can anyone help please??

    Friday, August 1, 2014 8:17 PM

Answers

  • Here is how to get the digits of yesterdays date using "Now"

    strFileDate = Right(Year(Now-1),2) & Right("0" & Month(Now-1),2) & Right("0" & Day(Now-1),2)

    Now is today date/time.  It adds and subtracts by days so just subtract 1 and you will have yesterday.

    Now you need to loop through all files in the folder and match the filename.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, August 1, 2014 8:56 PM
    • Marked as answer by Thailog001 Wednesday, August 6, 2014 1:47 PM
    Friday, August 1, 2014 8:56 PM

All replies

  • What is your problem?
    What error message do you get?
    Which line does it occur in?
    Hint: How do you calculate the two-digit year number?
    • Proposed as answer by Frederik Long Monday, August 4, 2014 6:38 PM
    Friday, August 1, 2014 8:43 PM
  • Here is how to get the digits of yesterdays date using "Now"

    strFileDate = Right(Year(Now-1),2) & Right("0" & Month(Now-1),2) & Right("0" & Day(Now-1),2)

    Now is today date/time.  It adds and subtracts by days so just subtract 1 and you will have yesterday.

    Now you need to loop through all files in the folder and match the filename.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, August 1, 2014 8:56 PM
    • Marked as answer by Thailog001 Wednesday, August 6, 2014 1:47 PM
    Friday, August 1, 2014 8:56 PM
  • This approximates what you need to do:

    Set fso = CreateObject("Scripting.FileSystemObject")
    strFileDate = Right(Year(Now-1),2) & Right("0" & Month(Now-1),2) & Right("0" & Day(Now-1),2)
    source = "c:\835old" 
    destination = "c:\835new" 
    folder = fso.GetFolder(source)
    For Each fi In folder.Files
        If Left(fi.Name,6) = strFileDate Then
            fso.MoveFile fi, destination
        End If
    Next
    


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Friday, August 1, 2014 9:48 PM
    Friday, August 1, 2014 8:59 PM
  • Here is how to get the digits of yesterdays date using "Now"

    strFileDate = Right(Year(Now-1),2) & Right("0" & Month(Now-1),2) & Right("0" & Day(Now-1),2)


    ¯\_(ツ)_/¯



    The OP did it in a similar way but he forgot the "right()" function for the year!

    Friday, August 1, 2014 9:04 PM
  • Here is how to get the digits of yesterdays date using "Now"

    strFileDate = Right(Year(Now-1),2) & Right("0" & Month(Now-1),2) & Right("0" & Day(Now-1),2)


    ¯\_(ツ)_/¯



    The OP did it in a similar way but he forgot the "right()" function for the year!

    Now - 1 is much easier.


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 9:16 PM
  • Hey - if you guys really like DateAdd then go for it.  I think it is ugly and, since it is Friday, I nominate "DateAdd" for my peeve of the week.

    today = Now
    tomorrow = Now + 1
    yesterday = Now - 1

    etc...


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 9:23 PM
  • Now - 1 is much easier.


    ¯\_(ツ)_/¯


    Indeed but this was not his question!
    Friday, August 1, 2014 9:24 PM
  • Now - 1 is much easier.


    ¯\_(ツ)_/¯


    Indeed but this was not his question!

    His question was answered a bit of time ago.  Have a good weekend.


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 9:26 PM
  • His question was answered a bit of time ago.  Have a good weekend.


    ¯\_(ツ)_/¯


    The OP knew how to calculate yesterday's date, although perhaps not in the most elegant way. His question was not which is your peeve of the week but where he went wrong in his code.
    Friday, August 1, 2014 9:47 PM
  • Works perfect now!

    Thank you!!!!!!!!!!!!!!!!!!!!!, jrv.  Thats exactly what I needed!!!!!!! 

    Sorry for the late reply and have an awsome day!!

    Wednesday, August 6, 2014 1:49 PM
  • Even easier now that you see how it works:

    ' set any date value first
    dt=Now-1
    strFileDate = Right(Year(dt),2) & Right("0" & Month(dt),2) & Right("0" & Day(dt),2)
    


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 2:07 PM