I need this script to look for a folder in
c:\windows\system32\foldername.
If this folder exists then i want the script to stop. If the folder does not exist then i want the script to run a .exe file from the server toinstall a piece of software. ie
\servername\folder\software.exe.
There will be file sin the folder in c:\windows\system32\foldername but i only wnat it to look at the folder not its contents.
Any suggestions please.?
you could try the following script
Option Explicit
Private Const Folder As String = "c:\windows\system32\foldername"
Private Const FileToRun As String = "\\servername\folder\software.exe"
Sub Run(ByVal sFile)
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & sFile & Chr(34), 1, False
Set shell = Nothing
End Sub
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(Folder) Then
Run FileToRun
End If
Related
I'm trying to create a VBA script that run RStudio script.
i tried using the Shell() command or the Run oShell at VBA, but the best thing i mange on doing is open the RStudio script, not making it to run automatically.
By the way, the RStudio script create a csv file which i will use.
this is the VBA script i use right now:
Sub RunRStudio()
Dim path As String
path = ThisWorkbook.path & "\Test.R"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "RStudio " & path
shell ("RStudio " & path)
End Sub
How can i run this RStudio script automatically from VBA?
Thanks.
I had the same problem. Thank you, because your code helped me to find the solution.
This code, with a little change, it works to open the window of Rstudio, but it doesn't work to run the script. You need to use "RScript", not "RStudio".
You can try this to run your code:
Sub RunRTest()
Dim path As String
path = """C:\Program Files\R\R-3.5.2\bin\RScript.exe""
""C:\Folder\YourScriptName.R"""
Set oShell = CreateObject("WScript.Shell")
oShell.Run path
End Sub
Or:
path_file = ThisWorkbook.path & "\YourScriptName.R"
path = """C:\Program Files\R\R-3.5.2\bin\RScript.exe"" " & path_file & ""
if you have the path of your file in other var.
I hope to help someone!
Heyho, I ran into a strange problem.
I execute a line like this in my VBA:
Shell "path_to_my_bat\batname.bat"
The content of the bat is very simple as well:
cd c:\some_path\
copy *csv newfiles.txt
What it does is simply take all csvs in the directory and merges them into one .txt that I use further down the line in the macro.
The problem is, since I have played around with some vbs scripts it seems I have changed something about how the shell command works.
The "cd" command seems to be skipped, or overwritten by something, the actual second part of the bat is executed in the directory my Workbook is placed in.
Luckily I had a .csv lying there or else I would not have noticed...
The bat on itself works fine if I don't run it from VBA.
The vbs script I played with looks like this (it should open a file and execute a macro in there) :
I think I overwrote some kind of default setting that I should not have tinkered with...
Apart from that I have done nothing I'm aware of to alter something. The macro was working fine this morning, but now because of the bat error it is useless.
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\YourWorkbook.xls"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\YourWorkbook" &
"!Sheet1.YourMacro"
on error resume next
' Run the calculation macro
myExcelWorker.Run strMacroName
if err.number <> 0 Then
' Error occurred - just close it down.
End If
err.clear
on error goto 0
oWorkBook.Save
myExcelWorker.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will
shut those down also
if myExcelWorker.Workbooks.Count = 0 Then
myExcelWorker.Quit
End If
Set myExcelWorker = Nothing
Set WshShell = Nothing
If it helps, I'm running Windows 8.1 64 bit with Excel 2010 64bit
Got full admin acces etc.
If your current directory is on the D: drive, then cd c:\some_path\ will set the directory for C:, but your current drive will remain somewhere on D:. You need to tell cmd.exe to actually change which drive you are working in.
Simply change your command to
cd /d c:\some_path\
or
pushd c:\some_path\
I'm using Dir function to get directory's content right now.
FileSpec = DirPath & "/GenerateID_*_*.zip"
FileName = Dir(FileSpec)
But Dir function is locking the folder and can't delete that folder until I close my VBA application. I have tried ChDir("C:\") to make this point to another directory after calling Dir function but it's not working.
Is it possible to filter using FileSystemObject like Dir function? Or get all files with FileSystemObject, loop each fileName and checking each one would be the only option?
You can certainly loop through the files with the FileSystemObject. From the MSDN documentation for the Files Property of the FileSystemObject:
Function ShowFileList(folderspec)
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & "<BR>"
Next
ShowFileList = s
End Function
This will return a string that is delimited by '<BR>'. You can replace this with something else, like vbCrLf or whatever you like then parse the string as required.
I currently have the following code to call a vbs file that is in a folder within Program Files. It was originally in a different location (within the same folder) and it would work, but now there is a padlock symbol next to the folder within Program Files, and after changing the location (VBA updated for this as well), it won't call the file. I'm wondering why this is happening.
Sub ChangeThemeBasic()
Dim filespe As String
filespe = "cmd.exe /c C:\Program Files\Theme Changer\ChangeTheme.vbs"
X = Shell(filespe, 1)
End Sub
EDIT:
This works for some reason, I don't know why though:
Sub Test()
Shell "Explorer.exe ""C:\Program Files\Theme Changer\ChangeTheme.vbs""",1
End Sub
From comments
Sub ChangeThemeBasic()
Dim filespe As String
filespe = "cmd.exe /c " & Environ("AppData") & "\Theme Changer\ChangeTheme.vbs"
X = Shell(filespe, 1)
End Sub
I'm using the code by Ron (http://www.rondebruin.nl/win/s7/win002.htm) to, in theory, unzip a bunch of zip files in a folder. I believe what I have below is the code that takes each zip file in my 'Downloads' directory, creates a new folder with the name of the zip file without the ".zip", and then extracts the files into the new folder. I am not getting any errors (many times people get the runtime error 91) but the only thing that happens is that it creates a bunch of correctly named folders but they are all empty.
Sub UnZipMe()
Dim str_FILENAME As String, str_DIRECTORY As String, str_DESTINATION As String
'Your directory where zip file is kept
str_DIRECTORY = "C:\Users\Jennifer\Downloads\"
'Loop through all zip files in a given directory
str_FILENAME = Dir(str_DIRECTORY & "*.zip")
Do While Len(str_FILENAME) > 0
Call Unzip1(str_DIRECTORY & str_FILENAME)
Debug.Print str_FILENAME
str_FILENAME = Dir
Loop
End Sub
Sub Unzip1(str_FILENAME As String)
Dim oApp As Object
Dim Fname As Variant
Dim FnameTrunc As Variant
Dim FnameLength As Long
Fname = str_FILENAME
FnameLength = Len(Fname)
FnameTrunc = Left(Fname, FnameLength - 4) & "\"
If Fname = False Then
'Do nothing
Else
'Make the new folder in root folder
MkDir FnameTrunc
'Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FnameTrunc).CopyHere oApp.Namespace(Fname).items
End If
End Sub
The problem is you are not giving windows enough time to extract the zip file. Add DoEvents after the line as shown below.
TRIED AND TESTED
oApp.Namespace(FnameTrunc).CopyHere oApp.Namespace(Fname).items
DoEvents