Save file without specifying Drive Location - vba

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

Related

Show files only (not folders) in a Office.FileDialog window?

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),")

Read File name and put it into Variable

I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If

Finding paths to files using vba

I have written a function which opens a dialogue box and lets the user select a folder. This function returns a string which is simply the path of the folder selected.
FolderPath = BrowseForFolder()
Now the idea is that this FolderPath refers to a special folder which has tons of excel (.xml) files in it. What I wish to do is get specific location (which has their names) for each of these .xml files in this special folder. I don't want the user to select all the filer, since there will be a lot of them. So in short, i want lots of strings denoting paths to these .xml files in FolderPath
Sahil, I would do this with a scripting object.
Dim FileCollection as Collection
Dim FSO as Scri[ting.FileSystemObject
Dim FLD as Scripting.Folder
Dim FIL as Scripting.File
You have to set a reference to the Microsoft Scripting Runtime library, or declare them all as Objects and use CreateObject to build them on the fly. Use Set FLD = FSO.GetFolder(FolderPath) to connect the object with your path string. Then use For Each FIL in FLD.Files to loop through the contents looking for xml files, and put them into the collection with FileCollection.Add File.Path.

VB.NET | Get current user profile folder

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"

VBscript for packing files

I am very rusty in VB since it has been two years I have used it. I will soon be diving back into it because I have more of a reason now to use it than ever. Right now I am trying to create a VB script that will help me modify some files. I currently need a way to take a folder and package them a certain way. What I am trying to do is this:
Take a folder with client name
Create a zip file with said client name
Add a certain file to it first which will not change (ship.xml)
Take contents of client folder and add to zip folder.
Rename .zip to .tar format
Also if you know any good site tutorials on VB please let me know. I am using Visual Basic 2010 ultimate.
Don't know exactly what you mean with clientname (computername ?), but this should get you started, you can add the rest yourself otherwise you'll stay rusty 8>)
zip = "c:\myzip.zip"
source = "G:\script\zip"
set fso = createObject("Scripting.FileSystemObject")
set shell = createObject("shell.application")
'make empty zip
set file = fso.CreateTextFile(zip, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close
set objFolder = shell.NameSpace(source)
set oZip = shell.NameSpace(zip)
if not oZip is nothing then
'add files to zip
oZip.CopyHere objFolder
wait_until_zipped(zip)
oZip.CopyHere "c:\ship.xml"
wait_until_zipped(zip)
'rename the zip to tar
fso.MoveFile zip, "c:\myzip.tar"
end if
'cleanup
set oZip = Nothing
set shell = Nothing
set fso = Nothing
function wait_until_zipped(zip)
set handle = fso.getFile(zip)
do
wscript.sleep 500
max = handle.size
loop while handle.size > max
end function