Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Input box check if file exist and then run batch

Posted on 2004-10-22
7
Medium Priority
?
10,178 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
Comment
Question by:Danny Kon
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Author Comment

by:Danny Kon
ID: 12378940
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
ID: 12379022
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
ID: 12379047
for the copying part

add a reference to "Windows Script Host Object Model"

Dim objShell As New WshShell

objShell.Exec "XCopy " & inputFilename
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 2000 total points
ID: 12379208
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
ID: 12379215
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:Danny Kon
ID: 12387725
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:Danny Kon
ID: 12398049
Jimbobmcqee,

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

916 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