We help IT Professionals succeed at work.

How to read in variables from command line into visual basic program

cooked
cooked asked
on
313 Views
Last Modified: 2012-05-05
I have read the notes on how to read in variables from a command line.
I have tried different versions of code and I cannot see to get it to work.
Please help - what is wrong with the code?  The orginal code came from Mark (?) to cleanup log files in Windows.  I would like one generic program and be able to feed variables in.
This question is urgent - I'm positive that it isn't hard.
Thank you

Here is the code . . .


Private Sub Form_load()

 Dim path As String
 Dim killextension As String
 Dim killdate As Integer

 Dim parameter as Variant
 parameter = Split(Command())
 path = parameter(0)
 killextension = parameter(1)
 killdate = date - parameter(2)

end sub


' folder to start search in...
' path = "c:\janet"

' delete files with this extension...
' killextension = "txt"  


' delete files older than 7 days...
' killdate = date() - 30


arFiles = Array()
set fso = createobject("scripting.filesystemobject")


' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true


nDeleted = 0
for n = 0 to ubound(arFiles)
  '=================================================
  ' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
  '=================================================
  on error resume next 'in case of 'in use' files...
  arFiles(n).delete true
  if err.number <> 0 then
    wscript.echo "Unable to delete: " & arFiles(n).path
  else
    nDeleted = nDeleted + 1
  end if
  on error goto 0
next


msgbox nDeleted & " of " & ubound(arFiles)+1 _
  & " eligible files were deleted"


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
  on error resume next
  'select files to delete and add to array...
  '
  set folder = fso.getfolder(sPath)
  set files = folder.files


  for each file in files
    ' uses error trapping around access to the
    ' Date property just to be safe
    '
    dtlastmodified = null
    on error resume Next
    dtlastmodified = file.datelastmodified
    on error goto 0
    if not isnull(dtlastmodified) Then
    if dtlastmodified < vKillDate and _
      lcase(fso.getextensionname(file)) = lcase(killExtension) Then

         count = ubound(arFilesToKill) + 1
         redim preserve arFilesToKill(count)
         set arFilesToKill(count) = file
       end if
     end if
   next

  ' if bIncludeSubFolders then
    ' for each fldr in folder.subfolders
      ' SelectFiles fldr.path,vKillDate,arFilesToKill,true
    ' next
  ' end if

end sub
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Hi cooked,
> parameter = Split(Command())

What do you have in Command() ?

What do you have in parameter after executing this line?


Cheers!
leclairm.Net Developer  ERP integration
CERTIFIED EXPERT

Commented:
<What do you have in Command() >

Command will contain the values passed to the executable in the command line.
ex: ShowMessage.exe Mike
in vb, Command will contain "Mike"


Can I see your actual command line and the parameters you are using??

Author

Commented:
At the command line, I entered

deleteoldfiles c:\janet txt 30

c:\janet  should be the path
txt should be the extension
30 should be  date - 30 days for deletion

Thank you
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
if you type
msgbox command() do you see these values?

what about
msgbox parameter(0)
msgbox parameter(1)
msgbox parameter(2)
leclairm.Net Developer  ERP integration
CERTIFIED EXPERT

Commented:
Go to the Project/Properties/Make tab in vb and put "c:\janet txt 30" in the Command Line Arguments section.  Put a break point on the " parameter = Split(Command())" line and step through the code telling us what each parameter value is.
Cooked,

Try something like this:

intCounter = 0
For Each strArgument in Wscript.Arguments
  intCounter = intCounter + 1
  strArgument = UCase(strArgument)
  Select Case intCounter
    Case 1
      strPath = strArgument
    Case 2
      strKillExtension = strArgument
    Case 3
      intKillDate = strArgument
    Case Else
       ' oops! too many parameters
       ' show help?
  End Select
Next
If intCounter = 0 Then
  ' oops! no parameters
  ' show help?
End If

Commented:
The above code looks good but you have missed out a parameter for the SPLIT function. You need to tell SPLIT what delimiter to do the spliting. In your case, the following should work:

parameter = Split(Command(), " ")           ' <-- Since your parameter is separated by SPACE


This code works fine as long as there is a space between each parameter. It parses it automatically without the need to use Split. I use it exactly as written in several of my VBScripts.

Author

Commented:
RyanAmsbury -
Would you mind showing me your script?  I cannot see the error in my ways in my code.
Thankyou
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
cooked,

if you type
msgbox command() do you see these values?

what about
msgbox parameter(0)
msgbox parameter(1)
msgbox parameter(2)

Author

Commented:
Emoreau -
I don't quite get VB.  And haven't had any training.  I'm trying to learn by trial and error and a book.
Please be specific about where the msgbox command should go.
I do not think, I want "Private Sub" around this code.  I take it, I need to call this routine in the code then.
With the Dim commands, I am receiving error messages Line: 2 Char: 11 Expected end of statement
 Pointing to "Dim path As string"
Thank you
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
I want these 4 msgbox statements in your Form_Load right after this line (that I copied from your initial question):
parameter = Split(Command())

Author

Commented:
It doesn't get to that line of code.  It has a problem with the very first Dim line
Windows Script Host message box, with an error message
 Line:4
 Char:11
 Error: Expected end of statement
 Code: 800A0401
 Source: microsoft VBScript compilation error

If I remove all DIM lines, it travels to line 80 as the problem.  Because there are no variables to tell the program what it is deleting.  The routine wasn't read.

I remove the Sub lines (prive sub form_load() and end sub) and I receive errors with the parameter = Split line.

I am not to the point to run the command you asked, from the way I am looking at this.

Thank you
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>> Because there are no variables to tell the program what it is deleting

Open your project properties, go to the Make tab and add your parameters to the "command line arguments" texbtox (or recompile your appilcation and run it to get the msgbox).

Author

Commented:
I do not have the Visual Studio installed yet.  I am creating and updating this VB code with Windows & Edit.
I didn't think VB would be that complicated.  I'm beginning to think I'm wrong.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Are you doing VBScript or VB applications?

If you are building a VB program (or application) like the title says, you need to have VB installed in order to compile it.

Author

Commented:
It has to be a VB Script then.
If I remove all code above 'Folder to start search in . . .
It runs as coded.  I didn't want to hard code the path, extension and delete files older than.  I will have multipule programs compared to one that accepts variables.
Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
From your comment, that means "private sub form_load" and "end sub" are VB,  not   vbscript

That would explain the errors with "command"

So does anyone know how to read in variables in a Visual Basic Script ?
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>So does anyone know how to read in variables in a Visual Basic Script ?

I have already answered that! See http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_ywsa.mspx

Author

Commented:
That was what I needed !    It works beautifully.    Run the following from command line.  Deletes files with a .txt extension that are older than 30 days in directory c:\janet (only)

deleteoldfiles c:\janet txt 30

---------------------------------------------------  deleteoldfiles code below --------------------------
 
 
 path = WScript.Arguments.Item(0)
 killextension = WScript.Arguments.Item(1)
 killdate = date - WScript.Arguments.Item(2)


' folder to start search in...
' path = "c:\janet"

' delete files with this extension...
' killextension = "txt"  


' delete files older than 7 days...
' killdate = date() - 30


arFiles = Array()
set fso = createobject("scripting.filesystemobject")


' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true


nDeleted = 0
for n = 0 to ubound(arFiles)
  '=================================================
  ' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
  '=================================================
  on error resume next 'in case of 'in use' files...
  arFiles(n).delete true
  if err.number <> 0 then
    wscript.echo "Unable to delete: " & arFiles(n).path
  else
    nDeleted = nDeleted + 1
  end if
  on error goto 0
next


msgbox nDeleted & " of " & ubound(arFiles)+1 _
  & " eligible files were deleted"


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
  on error resume next
  'select files to delete and add to array...
  '
  set folder = fso.getfolder(sPath)
  set files = folder.files


  for each file in files
    ' uses error trapping around access to the
    ' Date property just to be safe
    '
    dtlastmodified = null
    on error resume Next
    dtlastmodified = file.datelastmodified
    on error goto 0
    if not isnull(dtlastmodified) Then
    if dtlastmodified < vKillDate and _
      lcase(fso.getextensionname(file)) = lcase(killExtension) Then

         count = ubound(arFilesToKill) + 1
         redim preserve arFilesToKill(count)
         set arFilesToKill(count) = file
       end if
     end if
   next

  ' if bIncludeSubFolders then
    ' for each fldr in folder.subfolders
      ' SelectFiles fldr.path,vKillDate,arFilesToKill,true
    ' next
  ' end if

end sub
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.