I need help for this:
Check a file exist, in a predefined folder ..
My example:
DIM fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("file1.exe")) Then
If (fso.FileExists("secondfile.exe)) Then
MsgBox "file esxist", 0+16, "---"
WScript.Quit()
Else
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("a file")
WScript.Sleep(1000)
objShell.SendKeys "{ENTER}"
End If
Else
MsgBox "Not founded file1.exe"
WScript.Quit()
End If
If founded file1.exe,then search a second file .. if this too founded,then send a message "file exist" if not founded, then run a program and exit wscript.
What i need?
the files in the folder, locate the script that we provide before launch!
For example, I'll give it to C: \ folder trololo locate the file, it looks for and if you have to perform the operations.
How can I solve for that search before ask which folder to locate the file? There is no way I could figure out: (
Sorry for bad english :/
Related
Currently with the VBA code below, I'm able to successfully verify if a file, with the name "Test", exists or not. If it exists, it returns the entire name of the file. However, what I am trying to do is open that file I found. So far, the forums I have searched for provide explanations on opening a workbook or an excel file, but I am trying to open any type of file (such as .docx, .pdf or .txt and etc.)
Sub findFile()
Dim strFileName As String
Dim strFileExists As String
'name of the file I would like to find in C:\.
strFileName = "Test"
strFileExists = Dir(strFileName)
If strFileExists = "" Then
MsgBox "The file doesn't exist"
Else
MsgBox "The file does exist"
End If
End Sub
(Just so the answer doesn't stay blank, I'm expanding on my comment.)
In VBA, to launch a file, use the ShellExecute function. You can use this to start an application (exe) directly or open a file (pdf\txt\doc) using the default handler.
Here is sample code to open a PDF file using the default PDF application.
Dim objShell
Set objShell = CreateObject("shell.application")
objShell.ShellExecute "C:\Files\Project_V1.pdf", "", "", "open", 1
Set objShell = Nothing
Documentation can be found here:
https://learn.microsoft.com/en-us/windows/win32/shell/shell-shellexecute
You could try:
Sub test()
Dim strFile As String, strExist As String
Dim wb As Workbook
strFile = "C:\Users\pppp\Desktop\xxx\xxx.xlsx"
strExist = Dir(strFile)
'Check if exist
If strExist <> "" Then
'Open file
Set wb = Workbooks.Open(strFile)
End If
'Close file
wb.Close
End Sub
I keep on receiving
"Run-time Error 58 File Already Exists"
when trying to run the below code. I've double and triple checked and the file definitely doesn't exist.
I run a different Macro first that gets me the location of the folder I want to create this new folder in. The location is displayed in ActiveWorkbook.Sheets(1).Range("A1"). Master File is the name of the new folder I want to create.
Sub CreateFolder
Dim Bname As String
Bname = ActiveWorkbook.Sheets(1).Range("A1").Text & "\Master File"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder "Bname"
End Sub
I've also tried the following:
Sub CreateFolder
Dim Bname As String
Bname = ActiveWorkbook.Sheets(1).Range("A1").Text & "\Master File"
MkDir "Bname"
End Sub
You create folder 'Bname', not '..\Master File'. Do not use quotation marks around the variable.
fso.CreateFolder Bname
I need to print all documents inside a folder (whether they are PDF, JPG, PNG or TXT), but all I can read about is how to convert/print specific files types to PDF.
Is it possible to have a macro to print all the PDF, JPG, PNG or TXT files inside a folder?
Maybe this should helps
Sub printf()
TargetFolder = "c:\test\"
If TargetFolder > "" Then
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem In colItems
objItem.InvokeVerbEx ("Print")
Next
msgbox "All files in folders being printed"
else
msgbox "Please specify a folder name"
End If
End Sub
I was trying to write a code to check for a specific file if it is existing in a folder and subfolder in any subfolder \DESKTOP in c:\users*.* (= all users directories). And if the file is existing in any folder the script will delete the file.
Option Explicit
Dim Shell, FSO, DesktopPath
Dim objShortcutFile, objDesktopFolder, objDesktopSubFolder, Folder, strSysDrive
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
strSysDrive = Shell.ExpandEnvironmentStrings("%SystemDrive%")
Set Folder = FSO.GetFolder(strSysDrive & "\Users")
msgbox Folder & "\sample1.lnk"
For Each objDesktopFolder in Folder.SubFolders
If FSO.FileExists(Folder & "\sample1.lnk") Then
FSO.DeleteFile Folder & "\sample1.lnk"
msgbox "success"
Else
msgbox "not existing"
End If
Next
Folder is the C:\Users folder object; objDesktopFolder is the folder object for each folder directly in C:\Users, e.g. C:\Users\user1 - not further levels of subfolders e.g. C:\Users\user1\Desktop (so it's a misleading name as it is not the desktop folder).
If you only want to look directly on the desktop, then just change this line (and any other line that uses that path):
If FSO.FileExists(Folder & "\sample1.lnk") Then
to:
If FSO.FileExists(FSO.BuildPath(objDesktopFolder.Path, "Desktop\sample1.lnk")) Then
If you also want to look through each folder that may exist on the desktop, then you'll have to perform the same sort of logic, e.g.
Option Explicit
Dim Shell, FSO, DesktopPath
Dim objShortcutFile, objDesktopFolder, objDesktopSubFolder, Folder, strSysDrive
Dim filepath, userfolder, desktop, subfolder, filename
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
strSysDrive = Shell.ExpandEnvironmentStrings("%SystemDrive%")
Set Folder = FSO.GetFolder(strSysDrive & "\Users")
msgbox Folder & "\sample1.lnk"
filename = "sample1.lnk"
For Each userfolder in Folder.SubFolders
desktop = FSO.BuildPath(userfolder.Path, "Desktop")
filepath = FSO.BuildPath(desktop, filename)
If FSO.FolderExists(desktop) Then
' Delete file on desktop
If FSO.FileExists(filepath) Then
FSO.DeleteFile filepath, True
MsgBox "Success: deleted " & filepath
Else
MsgBox filepath & " doesn't exist"
End If
' Check folders on desktop
For Each subfolder In FSO.GetFolder(desktop).SubFolders
filepath = FSO.BuildPath(subfolder.Path, filename)
If FSO.FileExists(filepath) Then
FSO.DeleteFile filepath, True
MsgBox "Success: deleted " & filepath
End If
Next
End If
Next
That will only look for the file in folders directly on the desktop (as well as the file on the desktop, of course). If you want to look through further levels of subfolders then it's really best to create a separate sub that uses recursion to go through all levels of subfolders.
I want to know if it's possible to rewrite this piece of code:
Private Sub PrepareDir(ByVal dir As String)
Dim fso As New FileSystemObject
If fso.FolderExists(dir) Then Call fso.DeleteFolder(dir, True)
Call fso.CreateFolder(dir)
End Sub
With VBA statements: Kill, MkDir, etc. Most "difficult" part of this - remove non-empty directory. With FSO it can be done easily, but how it can be done without FSO?
The OP said they want to rewrite their code "without FSO" but it doesn't make sense.
If the goal is to reduce the amount of code, simply make it a one-liner:
CreateObject("Scripting.FileSystemObject").DeleteFolder "x:\myFolder"
As requested, this permanently removes the folder and it's contents.
More Information:
Microsoft Docs : DeleteFolder Method
Ron de Bruin : Delete files and folders
This piece of ccode uses RmDir to remove the Folder. AFAIK, RmDir cannot delete the folder unless it is empty, so we first clear the content in the folder then remove the directory.
Private Sub PrepareDirModified(dirStr As String)
On Error Resume Next
If Right(dirStr, 1) <> "\" Then dirStr = dirStr & "\"
Kill dirStr & "*.*"
RmDir dirStr
MkDir dirStr
On Error GoTo 0
End Sub
Hope this helps.
No need to delete files for Deleting folders.
Take the path and search for the sub folders in a loop and that sub folder can be deleted.
below is the example :copy both procedures and paste on module
Public Function Delete_Folder(ByVal FldrName As String)
Dim fso, FSfolder As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set FSfolder = fso.GetFolder(Application.DefaultFilePath)' This is My Documents folder path
'You can replace with your original folder path
For Each Folder In FSfolder.SubFolders
'Debug.Print Folder.Name
If Folder.Name = FldrName Then
Folder.Delete
Exit For
End If
Next
End Function
Sub test()
Delete_Folder "Sub_Folder_Name"
End Sub