How can I use 'path' to go to the user's current profile?
For example, I have this code:
Dim fso, fldr
fso = CreateObject("Scripting.FilesystemObject")
fldr = fso.GetFolder("C:\Documents and Settings\%UserProfile%\Local Settings\TEST")
'delete subfolders
For Each subf In fldr.SubFolders
subf.Delete(True)
Next
'delete subfiles
For Each fsofile In fldr.Files
fsofile.Delete(True)
Next
I've tried this way and the path is unknown.
How can I make C:\Documents and Settings\???\Local Settings\TEST
to go to the current user's folder?
Use the 'userprofile' environment variable...
MsgBox(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
On my Windows 8.1, I cannot access Local Settings folder. It's right protected. As far as getting the right folder path is concerned, I think the answer is already posted above. Just append your custom folder path to the UserProfile Folder path returned by Environment of DotNet.
Something like:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Local Settings\TEST"
Get the Local AppData folder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
And then concatenate it with your "TEST" folder using Path.Combine method.
See SpecialFolders and Combine msdn pages.
This worked for me, using VB6.0 Sp6
Dim myDocuPath As String
myDocuPath = Environ$("USERPROFILE") & "\My Documents"
Related
The following code checks to see if a folder exists and, if not, creates it. The code works, but a handle that points to the folder is left open if it already exists, which prevents the folder from being deleted or renamed until Outlook.exe closes. I do not understand why this is happening or what to do about it, but a handle should not be open after the folder is checked and potentially created.
Sub Test()
Folder = Environ("USERPROFILE") & "\Desktop\NewFolder\"
Result = Dir(Folder, vbDirectory)
If Result = vbNullString Then
MkDir Folder
End If
End Sub
The first time through the code, the folder is successfully created and no file handles are open:
However, the second time through the code, the folder already exists. MkDir does not execute, but a file handle is left open presumably after Dir executes:
I have tried everything I could find to close all open file handles, but nothing has worked. Any help would be greatly appreciated.
Based on the comments from braX, I was able to prove that the Dir call was somehow responsible for the leftover handle. Calling Dir again on a non-existent folder caused the handle to be released, which solved my problem.
A better solution, which was also suggested by braX, is to use a File System Object. Here is a working solution:
Sub Test()
Dim FSO As FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
Folder = Environ("USERPROFILE") & "\Desktop\NewFolder\"
If Not FSO.FolderExists(Folder) Then
MkDir Folder
End If
End Sub
Thanks, braX!
I would like to specify where my new Excel file is saved in terms of the folder (e.g. the folder name is Input Data). However, I do not want to specify the drive that it is contained in (e.g. C:\Input Data), how should I go about it?
I saw that other people typically would specify the full file path to where the folder is e.g. C:\Input Data. However, I would like to drop the C:\ portion.
The expected result would be where the output file is saved in the folder called "Input Data"
Rather than trying to specify an unknown location you could use a system location. Not only will this be consistent (for each user) but it should be a location where files can be saved. Many systems have been set up not to allow folders to be created in the root of the C:\ drive.
Dim fso As Object
Dim pth As String
Set fso = CreateObject("Scripting.FileSystemObject") 'create a filesystemobject
pth = fso.buildpath(Environ("userprofile"), "Input Data") 'get the path
If Not fso.FolderExists(pth) Then fso.CreateFolder pth 'create the folder if required
pth = fso.buildpath(pth, "My Workbook Name.xlsm") 'get the save path
ThisWorkbook.SaveAs pth 'save
Set fso = Nothing
To see the other system folders see https://pureinfotech.com/list-environment-variables-windows-10/ You might prefer to use %TEMP%.
You will need to change the Save statement to suit
I have to find the rscript.exe that exists in a folder called bin but I do not have the full reference to this folder.
Full Reference would be:
C:\Program Files\R\R-3.5.3\bin
but I do not know the version part beforehand. So I must go to
C:\Program Files\R and search the subfolders for the rscript.exe and recieve the full path to it.
I have already searched but all I could find was how to find out the path of the running application via reflections and tried out to rewrite the code without any solution. Could anyone help me here?
You can use the Directory.GetDirectories method with the searchPattern argument and loop throught the subdirectories:
Dim rootPath As String = "C:\Program Files\R"
If Directory.Exists(rootPath) Then
Dim currPath As String
For Each subFolder As String In IO.Directory.GetDirectories(rootPath, "R-*", SearchOption.TopDirectoryOnly)
currPath = IO.Path.Combine(subFolder, "bin", "rscript.exe")
If IO.File.Exists(currPath) Then
MessageBox.Show("File found at " & currPath)
End If
Next
End If
I have a procedure using the Office.FileDialog object that goes to a remote folder, sets a FileDialog.Filter to search for a specific file in that folder (i.e., .Filter = "\\PATH\MYFILE*.pdf"), and then uses FileDialog.Show to display a window containing only that target file. When the window is displayed, it contains not only my target file but also all of the sub-folders in the target folder. How can I display only the files, and not the sub-folders?
As filtering applies only to files, and there's nothing of the kind in fileDialogType - I don't think what you're asking for is possible in VBA.
This will show only .pdf Files in the FileDialog. Just Add the specific path to the folder you want to open the FileDialog to in the quotations after ChDir "YourFile_Path".
Dim myFile As String
ChDir "C:\Users\Documents"
myFile = Application.GetOpenFilename(Title:="Import pdf File", FileFilter:="Pdf Files *.pdf (*.pdf),")
in VBA there is a ChDir function that allows to set/change the current directory.
That Current Directory is also affected by a user browsing through his drives/folders from Access (or Excel), even if he clicks Cancel in the end without opening anything.
My question is: in the later case, how do I retrieve the Current Directory ?
Note: the CurrentDirectory has nothing to do with the folder of the CurrentDb!
You want CurDir$:
?CurDir$
c:\temp
ChDir "c:\windows\"
?CurDir$
c:\windows
The complement to the ChDir command in VBA is the CurDir function.
on VBS
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory