Solved

Input box check if file exist and then run batch

Posted on 2004-10-22
10,159 Views
Last Modified: 2007-12-19
First i dont know anything about VBS.
I need to have a vbs what will come with an input box where the user copy past a file in.
The file have to be checked in a specific directory (something like batch dir /s c:\windows\filename)
if this file not exist i want to run a batch file
if this file exist I want a message saying "file exist please first uninstall"

Thanks in advance

Dan
0
Question by:dannykon
    7 Comments
     
    LVL 6

    Author Comment

    by:dannykon
    And i forget something
    If its possible than I want to set the file what is in the input box to set as a windows viariable
    That if the file not exist i can say something like xcopy %variable%
    Dan
    0
     
    LVL 3

    Expert Comment

    by:Noel_Castillo
    Add a reference to Microsoft Scripting Runtime

    Dim FSys As New FileSystemObject

    inputFileName = InputBox("Enter the complete filename here")
    If FSys.FileExists(inputFileName) Then
       'do ur copying here
    End If

    :)
    0
     
    LVL 3

    Expert Comment

    by:Noel_Castillo
    for the copying part

    add a reference to "Windows Script Host Object Model"

    Dim objShell As New WshShell

    objShell.Exec "XCopy " & inputFilename
    0
     
    LVL 16

    Accepted Solution

    by:
    The following will do the trick:

          Dim objFSO, objWSH, strFileName, strCmd

          set objFSO = CreateObject("Scripting.FileSystemObject")
          set objWSH = CreateObject("WScript.Shell")

          strCmd = "x:\mypath\mybatchfile.bat"      'CHANGE FOR YOUR PATH/FILE

          strFileName = InputBox("Enter the complete filename here")
                                        'GET USER INPUT

          objWSH.Environment.item("VARIABLE") = strFileName
                                        'SET TO ENV VARIABLE CALLED %VARIABLE%

          If objFSO.FileExists(strFileName) Then

                MsgBox "File exists! Please uninstall first..."
                                        'IF FILE EXISTS, WARN
                
          Else
          
                objWSH.Run strCmd            'OTHERWISE RUN COMMAND

                for i = 1 to 5000000
                next            'USE THIS LOOP TO HALT PROGRAM UNTIL BATCH IS FIRST RUN
                            'AS ENVIRONMENT VARIABLE IS CLEARED WHEN SCRIPT FINISHES

          End If

    HTH

    J.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    BTW, the above is a VBS script that uses late binding to create the file system object and the shell.  You cannot use the early binding method as described by Noel_Castillo if you are writing a VBS.  

    However, Noel's code is a cleaner if you are using VB6.

    Rgds

    J.
    0
     
    LVL 6

    Author Comment

    by:dannykon
    Jimbobmcqee,

    Thanks does exacly what I need but I dont understand what you say about the early binding method but doesnt matter because this have to be written in VBS and not in VB

    Again thanks, quick respons and perfect interperted <-- if you wright it like this

    Dan
    0
     
    LVL 6

    Author Comment

    by:dannykon
    Jimbobmcqee,

    I posted a new question about this because now it seems the code is not working 100%
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    Title # Comments Views Activity
    macro for email automation 16 96
    Mysql vs Oracle 10 86
    VBA to format 3 55
    VBA: retain first and second level based on "." 6 49
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now