• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10179
  • Last Modified:

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
0
Danny Kon
Asked:
Danny Kon
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now