ITRSupport
asked on
Check if folder exists VB Script
Hello,
I am looking for a solution to create a vbscript that will do the following:
Check if a particular folder exists on the target pc. If that folder exists, exit and do nothing. If the folder does not exist, rename another folder in that same directory and then run an .msi installer.
To be more specific...
I want to see if C:\Program Files\Microsoft Business Solutions\Great Plains Old 3 exists...if so do nothing.
If not, I want to rename C:\Program Files\Microsoft Business Solutions\Great Plains to C:\Program Files\Microsoft Business Solutions\Great Plains Old 3 and then execute a packaged .msi file. Also, I would like to do this via a group policy object if possible.
Thanks for any help on this one.
I am looking for a solution to create a vbscript that will do the following:
Check if a particular folder exists on the target pc. If that folder exists, exit and do nothing. If the folder does not exist, rename another folder in that same directory and then run an .msi installer.
To be more specific...
I want to see if C:\Program Files\Microsoft Business Solutions\Great Plains Old 3 exists...if so do nothing.
If not, I want to rename C:\Program Files\Microsoft Business Solutions\Great Plains to C:\Program Files\Microsoft Business Solutions\Great Plains Old 3 and then execute a packaged .msi file. Also, I would like to do this via a group policy object if possible.
Thanks for any help on this one.
You can Also put this A GPO \ Computer config \ Startup
and enjoy
and enjoy
This VB Script should do the job for you. Hope it helps:
Option Explicit
Dim oWMI, oFolder, oFCheck
Dim ColFolders, colFCheck
Dim errResults
Dim strComputer, strFname, strFRename, strFCheck
strComputer = "TESTPC" ' Name of remote computer ... name does not need \\
strFCheck = "C:\\temp\\not here" ' Name of folder to check exists ... needs double \\
strFname = "C:\\temp\\test" ' Folder of which to rename ... needs double \\
strFRename = "C:\temp\test rename" ' New folder name ... standard path format
' ============= Connect to remote machine ================
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
' ============= Check if folder exists ===================
Set colFCheck = oWMI.ExecQuery ("Select * from Win32_Directory where name = '" & strFCheck & "'")
IF colFCheck.Count = 0 Then
'============= Folder does not exist ===================
Set colFolders = oWMI.ExecQuery ("Select * from Win32_Directory where name = '" & strFname & "'")
For Each oFolder in colFolders
errResults = oFolder.Rename(strDrive & strFRename)
Next
Else
'============ Folder Exists ... Do Nothing ===============
End IF
Option Explicit
Dim oWMI, oFolder, oFCheck
Dim ColFolders, colFCheck
Dim errResults
Dim strComputer, strFname, strFRename, strFCheck
strComputer = "TESTPC" ' Name of remote computer ... name does not need \\
strFCheck = "C:\\temp\\not here" ' Name of folder to check exists ... needs double \\
strFname = "C:\\temp\\test" ' Folder of which to rename ... needs double \\
strFRename = "C:\temp\test rename" ' New folder name ... standard path format
' ============= Connect to remote machine ================
Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=imper
' ============= Check if folder exists ===================
Set colFCheck = oWMI.ExecQuery ("Select * from Win32_Directory where name = '" & strFCheck & "'")
IF colFCheck.Count = 0 Then
'============= Folder does not exist ===================
Set colFolders = oWMI.ExecQuery ("Select * from Win32_Directory where name = '" & strFname & "'")
For Each oFolder in colFolders
errResults = oFolder.Rename(strDrive & strFRename)
Next
Else
'============ Folder Exists ... Do Nothing ===============
End IF
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
You can
I Do it using a BAT file
easier to understand
Here is the code
@echo off
Echo I am going to do a Folder check
if exist "C:\Program Files\Microsoft Business Solutions\Great Plains Old 3" goto hell
ren "C:\Program Files\Microsoft Business Solutions\Great Plains" "Great Plains Old 3"
C:\MyMSI.MSI
:hell