|
-
May 16th, 2013, 07:19 AM
#1
Thread Starter
Hyperactive Member
[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.
-
May 16th, 2013, 07:33 AM
#2
Re: How to convert VBScript to VB.Net
If you want to move a directory then call Directory.Move. It's that simple.
-
May 16th, 2013, 07:44 AM
#3
Thread Starter
Hyperactive Member
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?
-
May 16th, 2013, 08:01 AM
#4
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
-
May 16th, 2013, 08:03 AM
#5
Thread Starter
Hyperactive Member
Re: How to convert VBScript to VB.Net
 Originally Posted by techgnome
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
-
May 16th, 2013, 08:08 AM
#6
Re: How to convert VBScript to VB.Net
-
May 16th, 2013, 08:47 AM
#7
Thread Starter
Hyperactive Member
Re: How to convert VBScript to VB.Net
 Originally Posted by techgnome
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
-
May 16th, 2013, 09:01 AM
#8
Re: How to convert VBScript to VB.Net
 Originally Posted by Jo15765
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.
-
May 16th, 2013, 09:07 AM
#9
Thread Starter
Hyperactive Member
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?
-
May 16th, 2013, 09:33 AM
#10
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
-
May 16th, 2013, 09:37 AM
#11
Thread Starter
Hyperactive Member
Re: How to convert VBScript to VB.Net
 Originally Posted by techgnome
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?
-
May 16th, 2013, 09:41 AM
#12
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
-
May 16th, 2013, 09:54 AM
#13
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.
-
May 16th, 2013, 09:57 AM
#14
Re: How to convert VBScript to VB.Net
 Originally Posted by Jo15765
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.
-
May 16th, 2013, 10:08 AM
#15
Thread Starter
Hyperactive Member
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'
-
May 16th, 2013, 11:51 AM
#16
Re: How to convert VBScript to VB.Net
Almost, look again... ParseExact... not Parse ...
-tg
-
May 16th, 2013, 12:20 PM
#17
Thread Starter
Hyperactive Member
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.
-
May 16th, 2013, 02:45 PM
#18
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
 
-
May 16th, 2013, 02:55 PM
#19
Thread Starter
Hyperactive Member
Re: How to convert VBScript to VB.Net
 Originally Posted by Shaggy Hiker
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.
-
May 16th, 2013, 04:24 PM
#20
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
-
May 16th, 2013, 06:38 PM
#21
Re: How to convert VBScript to VB.Net
 Originally Posted by techgnome
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.
-
May 17th, 2013, 07:27 AM
#22
Thread Starter
Hyperactive Member
Re: How to convert VBScript to VB.Net
 Originally Posted by techgnome
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)
-
May 17th, 2013, 12:13 PM
#23
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!
-
May 22nd, 2013, 01:51 PM
#24
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|