Save a textfile as current time and date - vb.net

So I am using VB.NET and I got this problem where I wanna save something (this works) but the one thing I can't get to work is that is automatically saves (title) as current time and date (like this as title: 06/04/2015 10h30min25s)
I am using the SaveFileDialog to save my text.

Rather than using the file path as returned by the dialog, you can simply build the path yourself using the current date and time. For instance:
Dim filePath As String = Date.Now.ToString("MM-dd-yyyy HH\hmm\minss\s")

Change your slashes to dashes. You can't have slashes in a file name.

Related

.txt file to Rich text box with changes

Im going to try to make this as simple as i possible can. I work in a shipping company, where we email our clients if there is any issues with their orders. I have recently made a vb.net program that standardizes these emails. Now i am rewriting the program so that the templates can be edited from a text file. right now they are hard coded into the program itself.
SO, this is what im wanting to do
Template.txt -> Good afternoon %NAME% your order has shipped.
I want the program to open this text file, Change %NAME% to variable CustName which is entered from a text box, Then show the updated text in a RichTextBox. But i dont want the template.txt to save the changes.
Was wondering if you fine people could help me out with this.
Thanks a lot!
The first answer is on the right track. You just need to store the template text in your file. With this in your text file:
Good afternoon {0} your order has shipped.
you can then do something like this:
Dim name = "someone"
Dim template = File.ReadAllText("TextFile1.txt")
RichTextBox1.Text = String.Format(template, name)
The first argument to String.Format is the template, containing the numbered placeholders. The remaining arguments are the values to replace those placeholders. The first of those arguments will replace every instance of "{0}" in the template, the second will replace "{1}" and so on. You need to make sure that your numbered placeholders always match the order of the values you pass to String.Format.
If you don't want the person editing that template to have to remember what each number means, you can write your own replacement code. For instance, you might store this in the file:
Good afternoon {name} your order has shipped.
and then do this:
Private Function FillTemplate(template As String, name As String) As String
Return template.Replace("{name}", name)
End Function
and this:
Dim name = "someone"
Dim text = FillTemplate(File.ReadAllText("TextFile1.txt"), name)
RichTextBox1.Text = text
You need to add a parameter and a Replace call to FillTemplate for each value you want to insert, e.g.
Private Function FillTemplate(template As String,
name As String,
address As String) As String
Return template.Replace("{name}", name).
Replace("{address}", address)
End Function
I think the syntax you're looking for is something like
String.Format("Good afternoon {0} your order has shipped", name_variable)
If you wanted to change two pieces of information, you could use
String.Format("Good afternoon {0} your order has {1}", name_variable, "shipped")

VBA loop through non-patterned files in folder (seek for opinions) [EDIT]

I am here for seeking any advice or opinion as I want to loop through every excel files in folder. As you can see from my attached picture, my excel files are different both in file types (.xls <> .xlsx) and filename (especially on 2018). I also need to loop through "Revised" or "revised" files as well since it is possibly that any file will be revised next time.
And yes, I also did some research on this. My understanding is I need to modify all of the file names into the pattern one before build up a VBA to loop. At first, I thought about decomposing all filenames and put it back in pattern form, but it sound too idealistic. Another way is using the date in each file to label the workbook name, but again I found that those date had different styles. Some files label the date by using string such as "January 2012" or "March 2014", while the others using the date form such as "19/08/2013".
Therefore, I would be appreciated if anyone could suggest me on;
How can I handle with the different file name (.xls and .xlsx) within the same VBA?
How should I deal with these different file names (some files have "revised" at the back; some do not have "-" between "Cons" and date; and some use month name instead of number)? Are they any pattern that I overlook?
Please noted that I am just a newbie VBA coder, so it would be great if you left your answers with an explanation or any kind of examples.
Many thanks.
--------------------------------------------------------[EDIT]-------------------------------------------------------------------
First of all sorry for my poor explanation before. I provided too few information to understand overall picture. Let's start this over again.
My data are about steel consumption which release from the authority
every month. My task is to gather all of these data (such as
production, import, export and consumption of every data in each
row) and generate into time series pattern (please see attached
excel screen)
As it is possibly that these data will be revised anytime, I thus
decide to download all of these file every time in every month (one
file per one month). In addition to those revised file, the
authority will unexpectedly rename those file for example, from
"Cons 201601.xlsx" into "Cons 201601 - revised.xlsx)". This make me
more difficult to work on this (please see attached folder for
reference).
Moreover, this authority seems to have a problem with file naming as
they had different pattern of filename in the past compare to the
present ones. Example is per below table; Cons 201701-Revised.xlsx
Cons 201710-Revised1.xlsx
Cons 201711.xlsx
Cons-200902.xls
Cons-201212_revise.xls
Cons-201401-revised.xls
I mention above file name in order to create a VBA to loop through
these file, select some content and paste into another workbook in
chronological order. This means that I cannot use "Loop while or Do
while function" in my VBA. At first I decided to use two integer
variables, both of which were set for years and months
(e.g. For i = 2009 to 2018 and For j = 1 to 12) in order to created the system of filename (such as filename = "Cons" & "-" & i & j). But,
as I stated before, non-patterned name by the authority had
prevented me from creating this kind of loop.
I also tried to use the date in cell B2 in figure 1 to label the
date in order to create the loop which I already explained before.
However, again, the authority did not use the same pattern to date
month and year. After I checked with many file, these are example of
the date style in cell B2 January 2012 (string)
February 2009 (string)
Jan-16 (1/1/2016 date in custom format)
Given above limitations, could you guys again suggest me any possible
way to create chronological loop so as to copy and paste data to another
workbook to form a set of time series data for each product?
Thank you for your kind help :)
Firstly, use FileSystemObject (include a reference to Microsoft Scripting Runtime in your VBA project) which has some helpful functions within it. You could always code your own, but why reinvent the wheel in this case?
Don't have time to codes something this morning, so here is the pseudocode:
Open a Folder using your known filepath
Loop through all the files in the Folder (For each f in Folder.Files
extract the date code from your filename (e.g. using RegEx)
Add to a collection (e.g. array or Dictionary item) of the filename and the extracted date code (your key).
(end loop)
Sort your collection based on the extracted date code
This now gives you an ordered list of files, which you can open in turn and extract the data. An added bonus is that the key in the collection gives you a consistent date representation which you can use as an index in your collated information.
If you just want to loop through all files in folder use this:
dim file as variant
file = Dir("<PathToFolder>")
While (file <> "")
'Your logic here
file = Dir
wend

How does workbooks.open in vba differ from opening the file manually?

I have a .clb file that can be opened with excel through windows explorer with no issues. When I try to open the same .clb file through VBA with workbooks.open the dates in the file get messed up.
For example 11/05/16 becomes 5/11/16. I must point out that this is not a format change, the format stays the same (d/mm/yy) but the date goes from 11th Mar to 5th Nov.
I assume the issue has something to do with format confusion somewhere along the way because the dates that cannot be confused with US format like 18/06/16 for example get converted to string format for some reason.
These issues do not occur when vba is not used to open the workbook and I have reduced my code down to a single line to make sure there are no unwanted interactions:
Workbooks.Open Filename:="G:\C_001S01.clb"
and the issue still persists.
Is there some other function I can use to open the .clb in vba that will not mess with the dates? or a way to prevent workbooks.open from messing them up?
Assuming the .clb format is a text file, you need to use the Workbooks.OpenText method (instead of Workbooks.Open), and specify your preferences for the delimiters, date formats and other settings.

VBA Word - Save As dialog with initial filename

I have a vba macro that makes some changes to the current document and determines a filename that should be used for it - if the document isn't saved as that filename yet the user should be prompted to do so (but should be able to alter the default setting).
I found two possibilities that both are not perfect (I'd need a mix of those two).
First approach:
Application.Dialogs(wdDialogFileSaveAs).Show
Opens the Save As dialog and lets you change the format and name of the file, but the default file name is the old filename or the title (up to the first special character like blank or -) of the document (in case it wasn't saved yet - changing the title of the document is of little help as the suggested filename will contain -). Is it possible to change the initial filename shown in the Save As dialog?
Second approach:
Application.FileDialog(msoFileDialogSaveAs).InitialFileName = filename
Dim choice As Integer
choice = Application.FileDialog(msoFileDialogSaveAs).Show
If choice <> 0 Then
filename = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
Call ActiveDocument.SaveAs(filename:=filename, FileFormat:=wdFormatDocumentDefault)
End If
The FileDialog will choose a filename only, so we have to save it explicitely.
This approach will show the filename I want, but if the user changes the suffix to e.g .pdf the file will still be saved in the .docx format (using the suffix .pdf). I didn't plan to have huge distinction of cases here for the rare case the user needs a different format than .docx. Is there an easy way to save the file in the correct format using this second approach?
Have you tried replacing the Call ActiveDocument.SaveAs line with
Application.FileDialog(msoFileDialogSaveAs).Execute
Window's setting "View tab - File name extension" maybe affecting this.
Turn it on to see if it will fix it.

Creating a file with name given in code in LiveCode

I'm trying to build a program that gets users name at first. This user names are kept in a text file. After user logins, according to the user's name, I want user to be lead his/her specific informations. I figured out that I can only do it with a file that is created when he sign up for an account which I direct him with my sign up button in Livecode. While he/she create his account I want to create a specific file for his/her. Can you help me with it please?
PS: I don't want to do it with a database right now. I just want to learn how to create a file without a specific name like
put specialFolderPath("documents")&"/userLoginCridentials.txt" into tFile put URL("file:"&tFile) into myFile
Instead of this "userCridentials.txt" I want something user can create with his own name :)
Having a little problem to understand your question. Are you targeting mobile or desktop? Are you having problem saving or reading the data?
If saving is you problem
On desktop you can use:
ask file "Save file as:"
then you get the filename back in it so you can use:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put tData into url ("file:" & tFile)
end if
If you targeting mobile and would like to save into the specialFolderPath("Documents")you can get the filename from a field and then save to that file. E.g. if you have a field named 'fileName' you can use something like:
put tData into url("file:" & specialFolderPath("Documents") & "/" & field "fileName"
Of course you should do some error checking to ensure that a user don't overwrite existing files without at least asking for permission, etc.
You can of course use a variable instead of a field...
If reading data is your problem
On desktop you can use:
answer file "Open File:"
Same as above but you now read data instead:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put url ("file:" & tFile) into tData
end if
on mobile you probably would like to present a list with the user-created files. In LiveCode you can list all files in the defaultFolder with the files. But you must set the defaultFolder to the folder you want to list.
set the defaultFolder to specialFolderPath("Documents")
put the files into tFiles
Now tFiles contains every file in that folder and you can filter it, display it in a list etc. E.g:
filter tFiles with "*.txt"
put tFiles into
If your problem is how to remember the "current" file name
Whenever you restart your app every variable is reset. So everything you want to remember between runs needs to be saved before your app quits. And for that you need a predefined filename. SO then your procedure will be:
Read in the predefined file.
Grab the file name from within that file
Read the file
If your problem is something else
Sorry, then I misunderstood your question...