Home All Groups Group Topic Archive Search About
Author
8 Dec 2006 5:20 PM
DorkyGrin
Hi,

Couple of questions:

1. I need to rename a PDF based on some information inside a text file.
Someone gave me some vbs code (see below) that I'd like to convert into
VB.net. I want GUI and the vb.net app to watch a folder for a file to
show up - I think I have good 'filewatcher' code for that. However, I
tried to use the vbs code logic to make a new vb.net executable with no
success.

What is the best way to convert the code to run in vb.net? Is there a
better way to do this in vb.net?

2. For someone that wants to do occasional file manipulation, is vb.net
the right programming language to use? Would anyone recommend C, Perl,
VB6 or something I'm not even thinking of? I'm willing to learn, take
classes, self study etc.

Thanks for any help or ideas!

************


Sub  ReadFiles
' Variables needed to initiate process

   Dim fso, f1, ts, s
   Dim sIndex, sIndex2, sIndex3, sTemp
   Dim sIndexArray(1000)
   Dim sIndexArray2(1000)
   Dim I, N

   Const ForReading = 1
   I = 0
    Set fso = CreateObject("Scripting.FileSystemObject")

   ' Read the contents of the file.

   Set ts = fso.OpenTextFile("C:\Output\NewNames.txt", ForReading,
False)

       ' Loop thru Index text records.

    Do While ts.AtEndOfStream <> True

        ' Initialize sIndex variables

        sIndex = ""

        sIndex2 = ""

        sIndex3 = ""

        sTemp =  ""

        ' Read Records in Index text file.

        s = ts.ReadLine


        If Mid(s,5,1) <> "C" Then

             sIndex = Mid(s,2,8)      ' New Index.


             sIndex2 = Mid(s,27,8)    ' Old PDF Name.


             sIndex3 = Mid(s,27,12)   ' Old PDF Name with pdf
extension.


             sTemp =  Replace(s,sIndex2, sIndex)  'Replaces Old PDF
Name with Index information.

             I = I + 1                                  ' Increment
counter.

             sIndexArray(I) = Mid(sTemp,27,12)        ' Add new PDF
Name and pdf file to array for moving and renaming.

             sIndexArray2(I) = sIndex3            ' Add old PDF Name to
array for moving and renaming
        Else
             N = N + 1
        End If
   Loop

   ' Garbage collection

   ts.Close

    Do While I => 1

       ' Move and rename old PDF file to new folder.  Rename old PDF
file to new PDF file.

       fso.MoveFile "C:\Output\" & sIndexArray2(I) & "",
"C:\completed\" & sIndexArray(I) & " "

         I = I - 1

    Loop

    ' Delete output file.

    fso.DeleteFile("C:\Output\NewNames.txt")

End Sub

ReadFiles

Author
9 Dec 2006 4:27 AM
Bruce W. Darby
DorkyGrin,

Go to the MSDN webpage and enter 'Convert vbs to vb.net' in the search box.
Lots of good information there.

Show quoteHide quote
> What is the best way to convert the code to run in vb.net? Is there a
> better way to do this in vb.net?
>
> 2. For someone that wants to do occasional file manipulation, is vb.net
> the right programming language to use? Would anyone recommend C, Perl,
> VB6 or something I'm not even thinking of? I'm willing to learn, take
> classes, self study etc.
>
> Thanks for any help or ideas!
Author
10 Dec 2006 4:02 PM
DorkyGrin
Maybe I'm looking in the wrong spot on MSDN. I didn't find much.

Can I just call a VBS file from a VB.net window? I read something like
this should work:

Process.Start("C:\test.vbs")

Or, is there anyone out there that can get me started on vb.net code to
do what the posted vbs code is doing?

Thanks





Bruce W. Darby wrote:
Show quoteHide quote
> DorkyGrin,
>
> Go to the MSDN webpage and enter 'Convert vbs to vb.net' in the search box.
> Lots of good information there.
>
> > What is the best way to convert the code to run in vb.net? Is there a
> > better way to do this in vb.net?
> >
> > 2. For someone that wants to do occasional file manipulation, is vb.net
> > the right programming language to use? Would anyone recommend C, Perl,
> > VB6 or something I'm not even thinking of? I'm willing to learn, take
> > classes, self study etc.
> >
> > Thanks for any help or ideas!
Author
10 Dec 2006 5:03 PM
Branco Medeiros
DorkyGrin wrote (inline):
<snip>
> 1. I need to rename a PDF based on some information inside a text file.
> Someone gave me some vbs code (see below) that I'd like to convert into
> VB.net. I want GUI and the vb.net app to watch a folder for a file to
> show up - I think I have good 'filewatcher' code for that. However, I
> tried to use the vbs code logic to make a new vb.net executable with no
> success.

Well, just as in VB.Classic, you'd have to disable Option Explicit (and
Option Strict also, in VB.Net) for the sample code to compile.

The particular code you posted seem to compile and run without
glitches, here. Notice, however that a few things might get wrong in
the long term, mainly the memory management of the COM objects you
create (fso and ts).

So, if you want to use the code as-is, I'd recommend you add, at the
end of the Sub:

  System.InteropServices.Marshall.ReleaseComObject(ts)
  System.InteropServices.Marshall.ReleaseComObject(fso)

> What is the best way to convert the code to run in vb.net? Is there a
> better way to do this in vb.net?

I don't know of a good way of converting VB/VBA code to VB.Net, sorry.
Personally, I'd try to recreate it from scratch, as suggested by aaron.
A possible, direct translation (with no error checking, mind you),
could be:

<code>
  Sub RenameFiles()
    Const PATH_SOURCE As String = "C:\Output\"
    Const PATH_DEST As String = "C:\Completed\"
    Const FILE_NAMES As String = "C:\Output\NewNames.txt"

    Dim OldNames As New List(Of String)
    Dim NewNames As New List(Of String)

    For Each Line As String _
    In System.IO.File.ReadAllLines(FILE_NAMES)
      If Line(4) <> "C"c Then
        Dim OldName As String = Line.Substring(26, 12)
        Dim NewName As String = Line.Substring(1, 8)
        OldNames.Add(OldName)
        NewNames.Add(NewName & OldName.Substring(8))
      End If
    Next
    For I As Integer = 0 To OldNames.Count - 1
      System.IO.File.Move(PATH_SOURCE & OldNames(I), _
      PATH_DEST & NewNames(I))
    Next
    System.IO.File.Delete(FILE_NAMES)
  End Sub
</code>

> 2. For someone that wants to do occasional file manipulation, is vb.net
> the right programming language to use? Would anyone recommend C, Perl,
> VB6 or something I'm not even thinking of? I'm willing to learn, take
> classes, self study etc.
<snip>

I don't see why not using VB.Net for what you suggest.

HTH.

Regards,

Branco.
Author
11 Dec 2006 3:20 AM
DorkyGrin
Branco, thanks. I'll mess around with your example code sometime in the
next couple of days.

Thanks again for the reply!

Branco Medeiros wrote:
Show quoteHide quote
> DorkyGrin wrote (inline):
> <snip>
> > 1. I need to rename a PDF based on some information inside a text file.
> > Someone gave me some vbs code (see below) that I'd like to convert into
> > VB.net. I want GUI and the vb.net app to watch a folder for a file to
> > show up - I think I have good 'filewatcher' code for that. However, I
> > tried to use the vbs code logic to make a new vb.net executable with no
> > success.
>
> Well, just as in VB.Classic, you'd have to disable Option Explicit (and
> Option Strict also, in VB.Net) for the sample code to compile.
>
> The particular code you posted seem to compile and run without
> glitches, here. Notice, however that a few things might get wrong in
> the long term, mainly the memory management of the COM objects you
> create (fso and ts).
>
> So, if you want to use the code as-is, I'd recommend you add, at the
> end of the Sub:
>
>   System.InteropServices.Marshall.ReleaseComObject(ts)
>   System.InteropServices.Marshall.ReleaseComObject(fso)
>
> > What is the best way to convert the code to run in vb.net? Is there a
> > better way to do this in vb.net?
>
> I don't know of a good way of converting VB/VBA code to VB.Net, sorry.
> Personally, I'd try to recreate it from scratch, as suggested by aaron.
> A possible, direct translation (with no error checking, mind you),
> could be:
>
> <code>
>   Sub RenameFiles()
>     Const PATH_SOURCE As String = "C:\Output\"
>     Const PATH_DEST As String = "C:\Completed\"
>     Const FILE_NAMES As String = "C:\Output\NewNames.txt"
>
>     Dim OldNames As New List(Of String)
>     Dim NewNames As New List(Of String)
>
>     For Each Line As String _
>     In System.IO.File.ReadAllLines(FILE_NAMES)
>       If Line(4) <> "C"c Then
>         Dim OldName As String = Line.Substring(26, 12)
>         Dim NewName As String = Line.Substring(1, 8)
>         OldNames.Add(OldName)
>         NewNames.Add(NewName & OldName.Substring(8))
>       End If
>     Next
>     For I As Integer = 0 To OldNames.Count - 1
>       System.IO.File.Move(PATH_SOURCE & OldNames(I), _
>       PATH_DEST & NewNames(I))
>     Next
>     System.IO.File.Delete(FILE_NAMES)
>   End Sub
> </code>
>
> > 2. For someone that wants to do occasional file manipulation, is vb.net
> > the right programming language to use? Would anyone recommend C, Perl,
> > VB6 or something I'm not even thinking of? I'm willing to learn, take
> > classes, self study etc.
> <snip>
>
> I don't see why not using VB.Net for what you suggest.
>
> HTH.
>
> Regards,
>
> Branco.