Results 1 to 24 of 24

Thread: [RESOLVED] How to convert VBScript to VB.Net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Resolved [RESOLVED] How to convert VBScript to VB.Net

    I am trying to convert a vbscript to VB.net. This is where I am currently, can someone help me convert this to vb.net?
    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Const FOF_CREATEPROGRESSDLG = &H0&
            Dim fName
            Dim objShell, objFolder As Object
            Dim TargetFolder As String
            Dim d As Date = Date.Today
            d = d.AddDays(-5)
            fName = Microsoft.VisualBasic.Right("00" & Month(d), 2) & "." & Microsoft.VisualBasic.Right("00" & Day(d), 2) & "." & Microsoft.VisualBasic.Right(Year(d), 2) & "_"
            TargetFolder = "N:\Daily Data\Production"
            objShell = CreateObject("Shell.Application")
            objFolder = objShell.NameSpace(TargetFolder)
            objFolder.MoveHere("Q:\Archive\\" & fName & "*", FOF_CREATEPROGRESSDLG)
        End Sub
    End Class
    The compile errors I get are:
    Day is a type and cannot be used in an expression
    Last edited by Jo15765; May 16th, 2013 at 07:40 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert VBScript to VB.Net

    If you want to move a directory then call Directory.Move. It's that simple.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Use Directory.Move to move the files....how would I iterate through each file to run my check?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    What check? I don't see any "check" in your code... you're grabbing an entire folder and coping it to a new location, using the date as the folder name...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by techgnome View Post
    What check? I don't see any "check" in your code... you're grabbing an entire folder and coping it to a new location, using the date as the folder name...

    -tg
    If the date in the format of mm.dd.yy is more than todays date - 5 away the code cuts that folder and pastes into the archive directory. The "Check" I was referencing was how would I do the same process using Directory.Move

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    Step 1 - Read the documentation

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by techgnome View Post
    I read the article...I am getting a debug error of illegal character in the file name.
    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Const FOF_CREATEPROGRESSDLG = &H0&
            Dim fName
            Dim objShell, objFolder As Object
            Dim TargetFolder As String
            Dim d As Date = Date.Today
            d = d.AddDays(-5)
            fName = Microsoft.VisualBasic.Right("00" & Month(d), 2) & "." & Microsoft.VisualBasic.Right("00" & Day(d), 2) & "." & Microsoft.VisualBasic.Right(Year(d), 2) & "_"
    		fullName = "Q:\Archive\\\" & fName & "*"
            TargetFolder = "N:\Daily Data\Production"
    		Directory.Move(fullName, TargetFolder)
        End Sub
    End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by Jo15765 View Post
    If the date in the format of mm.dd.yy is more than todays date - 5 away the code cuts that folder and pastes into the archive directory. The "Check" I was referencing was how would I do the same process using Directory.Move
    Probably a good idea to tell us what you're trying to do instead of expecting us to work it out from the code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    There are folders with the date in this format
    05.16.13 at the beginning. I am trying to take all folders that are more than 10 days old and move them to an archive location.

    Does that make sense?

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    well fullName is going to come out looking like this:
    Q:\Archive\\\05.16.13*


    which... yeah... is invalid...

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim SourceFolder as string = date.today.adddays(-5).ToString("MM.dd.yy")
            Dim TargetFolder As String = "N:\Daily Data\Production"
            Directory.Move(SourceFolder, TargetFolder)
        End Sub
    And that could probably be reduced down to a single line...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by techgnome View Post
    well fullName is going to come out looking like this:
    Q:\Archive\\\05.16.13*


    which... yeah... is invalid...

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim SourceFolder as string = date.today.adddays(-5).ToString("MM.dd.yy")
            Dim TargetFolder As String = "N:\Daily Data\Production"
            Directory.Move(SourceFolder, TargetFolder)
        End Sub
    And that could probably be reduced down to a single line...

    -tg
    Wow...GREATLY condensed, thank you!

    I am getting an IOException when it hits the Directory.Move line.

    It tells me that Source and destination path must have identical roots. Move will not work across network volumes.

    EDIT -- and yes Q is a network drive, am I setting it up incorrect to be using network drives?

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    "Move will not work across network volumes." -- hmmmm... that's a new one to me... guess I've only ever moved locally.... interesting...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert VBScript to VB.Net

    That would be something like:
    Code:
    Dim sourceFolderPath = "folder path here"
    Dim destinationFolderPath = "folder path here"
    Dim dateThreshold = Date.Today.AddDays(-10)
    
    For Each subFolderPath In IO.Directory.GetDirectories(sourceFolderPath)
        Dim subFolderName = IO.Path.GetFileName(subFolderPath)
        Dim subFolderDatePrefix = subFolderPath.Substring(0, 8)
        Dim subFolderDate = Date.ParseExact(subFolderDatePrefix, "MM.dd.yy", Nothing)
    
        If subFolderDate < dateThreshold Then
            IO.Directory.Move(subFolderPath, IO.Path.Combine(destinationFolderPath, subFolderName))
        End If
    Next
    That hasn't been tested so you may need to tweak it a bit but that's the general idea.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by Jo15765 View Post
    It tells me that Source and destination path must have identical roots. Move will not work across network volumes.
    Try My.Computer.FileSystem.MoveDirectory. It looks like it should work in that scenario. I think that it uses the same functionality as Windows Explore under the hood.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    It hits the subFolderDate = Date.Parse(subFolderDatePrefix, "MM.DD.yy", Nothing) and I get a debug error of
    Unable to cast object of type 'System.String' to type 'System.IFormatProvider'

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    Almost, look again... ParseExact... not Parse ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    The only options for Parse that Intellisense shows me are:
    Date.Parse
    Date.TryParse
    Date.TryParseExact

    If I use Parse I get the above error, if I use tryparseexact or tryparse, I get a compile error of overload resolution failed because TryParseExact accepts this number of arguments.

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How to convert VBScript to VB.Net

    Yeah, you'd have to write it differently for any variation on TryParse. Those functions return True or False. The converted date, if the conversion works, is returned in the second argument to the method. Therefore, you would pass subFolderDate as the second argument, and if the TryParse variant returns True, you'd have your converted value in the argument.
    My usual boring signature: Nothing

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, you'd have to write it differently for any variation on TryParse. Those functions return True or False. The converted date, if the conversion works, is returned in the second argument to the method. Therefore, you would pass subFolderDate as the second argument, and if the TryParse variant returns True, you'd have your converted value in the argument.
    Ay-Caramba! I am in over my head here on this one..

    The folders are in the format of 05.16.13_ ALWAYS this format, then after the underscore comes some sort of identifying name. It varies depending. What I am after is VB.Net code that will examine the date on each file, and if it is 5 days old (-5) from today's date I want to move it into the archive directory.

  20. #20
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to convert VBScript to VB.Net

    I'm jsut trying to figure out what was behind the use of the Parse in the first place... get the current date. lop off 10 days, then use the .ToString method to get the date in a string...

    then use System.Io.Directory.GetDirectories to get the list...

    Something like this:
    Code:
        Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
            Dim sourceFolder As String = ""
            Dim targetFolder As String = ""
            Dim folderPattern As String = Date.Today.AddDays(-10).ToString("MM.dd.yy") & "_*" '<-- the * is the wildcard... result is 05.16.13_*
            Dim folderList() As String = System.IO.Directory.GetDirectories(sourceFolder, folderPattern)
            'Now your folders are in an array, you can iterate through them:
            For Each folderToMove As String In folderList
                System.IO.Directory.Move(folderToMove, targetFolder)
            Next
        End Sub
    I *think* that might work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by techgnome View Post
    I'm jsut trying to figure out what was behind the use of the Parse in the first place.
    The OP wants to find folders that are more than 10 days old, which is going to require date comparison. You need to get the date from the folder name and compare it to the current date to see if there's more than 10 days between them. For that you need two Dates, not two Strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Quote Originally Posted by techgnome View Post
    I'm jsut trying to figure out what was behind the use of the Parse in the first place... get the current date. lop off 10 days, then use the .ToString method to get the date in a string...

    then use System.Io.Directory.GetDirectories to get the list...

    Something like this:
    Code:
        Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
            Dim sourceFolder As String = ""
            Dim targetFolder As String = ""
            Dim folderPattern As String = Date.Today.AddDays(-10).ToString("MM.dd.yy") & "_*" '<-- the * is the wildcard... result is 05.16.13_*
            Dim folderList() As String = System.IO.Directory.GetDirectories(sourceFolder, folderPattern)
            'Now your folders are in an array, you can iterate through them:
            For Each folderToMove As String In folderList
                System.IO.Directory.Move(folderToMove, targetFolder)
            Next
        End Sub
    I *think* that might work.

    -tg
    Using that coding produces the following error:
    IOException was unhandled
    Cannot create a file when that file already exists.

    On the line of code:
    Code:
    System.IO.Directory.Move(folderToMove, targetFolder)

  23. #23
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to convert VBScript to VB.Net

    Use the My.Computer.FileSystem.MoveDirectory method instead. The overloads include automatic overwriting of existing files and/or showing the Windows dialogs which allow you to choose actions.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: How to convert VBScript to VB.Net

    Sorry for my late response, but yes using the My.ComputerFileSystem.MoveDirectory knocked it out!

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