Input box check if file exist and then run batch

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
LVL 6
Danny KonAsked:
Who is Participating?
 
jimbobmcgeeCommented:
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
 
Danny KonAuthor Commented:
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
 
Noel_CastilloCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Noel_CastilloCommented:
for the copying part

add a reference to "Windows Script Host Object Model"

Dim objShell As New WshShell

objShell.Exec "XCopy " & inputFilename
0
 
jimbobmcgeeCommented:
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
 
Danny KonAuthor Commented:
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
 
Danny KonAuthor Commented:
Jimbobmcqee,

I posted a new question about this because now it seems the code is not working 100%
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.