Connecting two path strings to get the final path?
I'm trying to save excel file into a specific path. So basically, when I click the button, I'm creating a folder, and want to save the file inside that folder. The created folder has the current month as name. I'm trying to save into that current month folder.
'Create folder as Month Name. Save filename as date inside "month". Dim sDate As String = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss") Dim sMonth As String = DateTime.Now.ToString("MMMM") Dim sFolder = Application.StartupPath & "\Resources\Excel\" My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth)) Dim sfinal = Path.Combine(sFolder, sMonth) xlSh.SaveAs(sfinal & Format(sDate) & ".xlsx") xlApp.Workbooks.Close() xlApp.Quit()
As it is, this code doesn't give me any errors. But instead of creating a folder named "March" <-current month and saving inside it, it saves the file in \Excel\ and it also creates folder in the same place.
After long hours of excruciating pain, I've finally did it! Apparently I was missing an "\" Since "sMonth" became dynamic name, which later I wanted to use as path, and save files in that folder. I needed to simply put that "\" after sMonth, to tell it to save inside it.
Before I realize this... I've broken down, simplified the code as much as I could so I can logically connect the pieces. What I ended up with, is something slightly different. Now the SaveAS properly saves the file inside the new folder.
Dim sDate As String sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss") Dim sMonth As String sMonth = DateTime.Now.ToString("MMMM") Dim sFileName As String sFileName = sDate + ".xlsx" Dim sFolder As String sFolder = Application.StartupPath & "\Resources\Excel\" Dim sfinal As String sfinal = (sFolder & sMonth & "\") '<- this thingie here o.O My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth)) xlSh.SaveAs(sfinal & Format(sFileName)) xlApp.Workbooks.Close() xlApp.Quit()
Thanks for the help.
Path.Combine Method (System.IO), Whatever is calling your code should not have to worry about this. I've never wanted to combine two path portions and end up with the first� Connecting two path strings to get the final path? Tag: excel-vba,excel-2010,vb.net-2010. I'm trying to save excel file into a specific path. So basically, when I
you could use the following function (similar to .NET System.IO.Path.Combine)
Function PathCombine(path1 As String, path2 As String) Dim combined As String combined = path1 If Right$(path1, 1) <> Application.PathSeparator Then combined = combined & Application.PathSeparator End If combined = combined & path2 PathCombine = combined End Function
Hope this helps!
Is there a conventional way to combine file path strings?, The Path class includes various methods that can be used to obtain information for matching the path string and methods for removing redundancies in a path. Converting a Path; Joining Two Paths; Creating a Path Between Two Paths foo, Returns the file name or the last element of the sequence of name elements. If the last path component to be joined is empty then a directory seperator (‘/’) is put at the end. If a path component represents an absolute path, then all previous components joined are discarded and joining continues from the absolute path component. Syntax: os.path.join (path, *paths)
You don't appear to actually be setting the save path to the created directory. Instead, I believe you're appending the month to the beginning of the file name in the xlSh.SaveAs(sFinal & Format(sDate) & ".xlsx"). Basically (though I'm not sure of the specific command) you need to navigate to the folder you created after you create it. Probably something to the format of
My.Computer.FileSystem.ChangeDirectory(sFolder & Format(sMonth))
though I don't know that that specific command actually exists as I wrote it.
Path Operations (The Java™ Tutorials > Essential Classes > Basic I/O), There are a few different ways to build up file paths in PowerShell which I'll go over below. Using a hard-coded string. It is very common to see file� Joining two or more Paths to generate a single Path. The Join-Path cmdlet is used to combine two or more paths into a single path. Syntax: NAME Join-Path SYNTAX Join-Path [-Path] <string> [-ChildPath] <string> [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>] ALIASES None. Example #10. Input:
To those who have been wondering wtf I was doing with all this, here is the full sub. And if anyone needs something similar. Thanks for the support. Problem has been resolved.
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click Dim xlApp As Excel.Application Dim xlSh As Excel.Worksheet xlApp = New Excel.Application xlApp.Workbooks.Add() xlSh = xlApp.Workbooks(1).Worksheets(1) 'Items from listbox1 to be exported into excel, second row, second column. Dim row As Integer = 2 Dim col As Integer = 2 For i As Integer = 0 To ListBox1.Items.Count - 1 xlSh.Cells(row, col) = ListBox1.Items(i) row = row + 1 Next row += 1 col = 1 'Items from listbox2 to be exported into excel, second row, third column. Dim row2 As Integer = 2 Dim col2 As Integer = 3 For i As Integer = 0 To ListBox2.Items.Count - 1 xlSh.Cells(row2, col2) = ListBox2.Items(i) row2 = row2 + 1 Next row2 += 1 col2 = 1 'Create folder as Month Name. Save filename as date inside that folder. 'Make filename be yyyy-MM-DD_HH-mm-ss Dim sDate As String sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss") 'This will be used as name for the new folder. Dim sMonth As String sMonth = DateTime.Now.ToString("MMMM") 'Filename + extension. Dim sFileName As String sFileName = sDate + ".xlsx" 'This is the path. Dim sFolder As String sFolder = Application.StartupPath & "\Resources\Excel\" 'This is the path combined with sMonth to make the final path. Dim sfinal As String sfinal = (sFolder & sMonth & "\") 'Check if folder with the name sMonth already exists. If Dir(sFolder, vbDirectory) = sMonth Then 'If it exist, then simply save the file inside the folder. xlSh.SaveAs(sfinal & Format(sFileName)) Else 'If it doesn't exist: 'This is the creation of sMonth folder, inside "\excel\. My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth)) 'This saves the excel file at path sfinal, with filename of sFileName xlSh.SaveAs(sfinal & Format(sFileName)) End If 'Close everything. xlApp.Workbooks.Close() xlApp.Quit() End Sub
Joining Paths in PowerShell, os.path.join() method in Python join one or more path components intelligently. If the last path component to be joined is empty then a directory seperator A path-like object is either a string or bytes object representing a path. Please Improve this article if you find anything incorrect by clicking on the� It takes two parameters, a coordinate (x) and coordinate (y) to move to. If the cursor was already somewhere on the page, no line is drawn to connect the two positions. The "Move To" command appears at the beginning of paths to specify where the drawing should start. For example: M x y (or) m dx dy. In the following example there's only a point at (10,10). Note, though, that it wouldn't show up if a path was just drawn normally.
Python, Remarks. This method is intended to concatenate individual strings into a single string that represents a file path. However, if an argument other than the first contains a rooted path, any previous path components are ignored, and the returned string begins with that rooted path component.
os.path.basename: Returns the final component of a pathname print os.path.basename(path) # 'test.txt' print os.path.dirname(path) # '/test1/test2/test3' print os.path.basename(os.path.dirname(path)) # 'test3' Method 2: os.path.split os.path.split: Return tuple (head, tail) where tail is everything after the final slash.
Any thread can change the current directory at any time. Starting with .NET Core 2.1, you can call the Path.GetFullPath(String, String) method to get an absolute path from a relative path and the base path (the current directory) that you want to resolve it against.
- Trust me, I've tried... I tried so many combinations while referencing to the line you posted, and I couldn't get it working...
- Can you run it again and have it print out the values of each variable at the time each variable is used? That way we can see what is being input into the system.
- Don't know. We do know where the problem is... It's this code: Dim sfinal = Path.Combine(sFolder, sMonth) xlSh.SaveAs(sfinal & Format(sDate) & ".xlsx") I just don't know of any other way to SaveAs
- Glad you got it working! I was asking for the output as I was wondering about that ending slash, but sounds like you got it on your own, congrats!
- Oh, on a another thingie.... How to check if directory exists simply save the file, if it doesn't, then create the folder and save the file inside it?