extract data from csv and rename files [closed] - vb.net

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have files in a folder.
Filenames in this folder are available in a .csv file's column B.
Column A contains new file name.
Eg,
Attached pic.
How do I rename files in the folder based on column A using VB.NET ?

First you must read the your CSV.. i suppose that is not too big, so do something like
Dim MyCSV() As String = IO.File.ReadAllLines("c:\my file.csv")
Then go ahead with a "For Each" line in your CSV
For Each Line In MyCSV.ToArray
Next
In this For Each you must split the current line.. and i suppose is a standard CSV so must be the "," (i think)
Dim MySplitLine() As String = Line.Split(","c)
Finally (also in the For Each) you can rename your file with the new name
If IO.File.Exists("c:\folder 1\folder 2\" & MySplitLine(1) & ".extension") Then
FileSystem.Rename("c:\folder 1\folder 2\" & MySplitLine(1) & ".extension",
"c:\folder 9\folder 8\" & MySplitLine(0) & ".extension")
End If
Dont forget to set your extension.

Related

vb.net how can I make get the file name of text file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
the name of textfile change daily, so how can I make the name of my textfile dynamic in the system?
Dim fileNames = My.Computer.FileSystem.GetFiles("C:\K3K3Pro\Ini\",
FileIO.SearchOption.SearchTopLevelOnly, "*.ini")
Dim i = File.ReadAllLines("C:\K3K3Pro\Ini\" + fileNames + ".ini").Last
Try this
Dim fileEntries As String() = Directory.GetFiles("C:\K3K3Pro\Ini\", "*.ini")
' Process the list of .txt files found in the directory. '
Dim fileName As String
For Each fileName In fileEntries
If (System.IO.File.Exists(fileName)) Then
'Read File and Print Result if its true
'MsgBox(fileName)
End If
Next

Creating a hyperlink from a file name read from input file to write to a output text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I would like to convert the text name of each of the several files listed on the input text file (compatible with NotePad) to a hyperlink and would like to use the file name as the text display associated with each hyperlink and write the result with the hyperlinks to an output text file (also compatible with NotePAd).
I would lilke to use either VB Script or VB.Net as the programming language. I cannot find instructions for creating the hyperlink from a character string using either VB Script or VB.Net. Can someone point me to some relevant documentation?
Let's say you have a file named input.txt with this in it
filename1.txt
filename2.txt
filename3.txt
then you could use this code to read the lines and write a new file output.txt
Dim lines = File.ReadAllLines("input.txt")
File.WriteAllLines(
"output.txt",
lines.Select(Function(line) line.Replace(".txt", "") & vbTab & Path.Combine("\\domain\", line)))
which has the filename and a path to the file separated by a tab
filename1 \\domain\filename1.txt
filename2 \\domain\filename2.txt
filename3 \\domain\filename3.txt
This deals with a txt file as input and output. The terms compatible with NotePad and use the file name as the text display associated with each hyperlink are conflicting because notepad will display plain text, not a web page for example. But if you do want something which will render links as titles, you may want to try to write html with HtmlTextWriter
Dim lines = File.ReadAllLines("input.txt")
Using sw = New StringWriter()
Using writer As New HtmlTextWriter(sw)
For Each line In lines
writer.AddAttribute(HtmlTextWriterAttribute.Href, Path.Combine("\\domain\", line))
writer.RenderBeginTag("A")
writer.Write(line.Replace(".txt", ""))
writer.RenderEndTag()
writer.WriteBreak()
Next
End Using
File.WriteAllText("output.html", sw.ToString())
End Using
This would be what you'd see in notepad
filename1<br />
filename2<br />
filename3<br />
and in a web browser

Excel VBA code that searches multiple documents for a text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking for help, I have a very basic knowledge of VBA and can't perform complex tasks, would someone be able to provide the base of the code and I'll be able to study it and expand on it?
The code should be able to perform the following task:
Using a list of document names in excel, open those documents (word documents) and search the first page for a specific text. If the text exists return TRUE, else FALSE.
Please note the documents are all stored in sub folders.
Thank you,
There are tree different question you have to ask yourself before posting:
I will try to answer it in the way you asked:
1 - Read documents name from the excel file
Dim fileNames as new Collection
For Each name in Range("names")
fileNames.Add name
Next name
2 - Open Word documents
Add the Microsoft Word 12.0 Object Library to your references, and use the Word API to open and read the document.
Dim doc As Word.Document
Set doc = Word.Documents.Open(Filename:=DocumentPath, Visible:=False)
3 - Find text in the active document
Dim myRng as Word.Range
Set myRng = doc.Content
then use the find object for your purpose, take a look at the documentation
https://msdn.microsoft.com/en-us/vba/word-vba/articles/find-object-word

VBA Conditional Save As with Date Add [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using Excel 2010 and am trying to use a macro for the following:
Open the Save As dialogue box
Take the initial file name and check IF there is an underscore followed by 8 consecutive integers (ie. _12345678) before the file type (ie. .xlsx)
IF that DOES EXIST remove and replace it with an underscore followed by today's date in "yyyymmdd" format (ie. _20130730) before the file type (ie. .xlsx)
IF that DOES NOT EXIST simply add an underscore followed by today's date in "yyyymmdd" format (ie. _20130730) before the file type (ie. .xlsx)
The new file name based on criteria above would be present in the File Name field in the open Save As dialogue box but the file will require user to actually save it (just naming and opening Save As. Not actually saving with VBA)
Maintain whatever the original file type is
Assuming today's date is 7/30/2013, the macro would work as follows for the following beginning files:
1.) Test File A_20130615.xlsx would become Test File A_20130730.xlsx
2.) Test File B.xlsx would become Test File B_20130730.xlsx
Any and all help is appreciated!
Thanks
I modified a routine I have that does the same type of thing that you are trying to do, but uses the current name of the file, instead of having 2 save dialog boxes.
Option Explicit
Function SaveIt()
Dim CurrentFile As String
Dim FileExt As String
Dim GetFileName
CurrentFile = Left(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, ".") - 1)
FileExt = Mid(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, "."))
If InStr(CurrentFile, "_") Then
'has underscore
If InStrRev(CurrentFile, "_") = Len(CurrentFile) - 8 Then
' underscore 8 from end
If Right(CurrentFile, 8) = CStr(Val(Right(CurrentFile, 8))) Then
' and it's 8 digits at the end
CurrentFile = Left(CurrentFile, Len(CurrentFile) - 9)
'strip the end off
End If ' if it fails any of these tests,
End If 'then it's not got the underscore and date
End If ' and we don't touch the filename
CurrentFile = CurrentFile & "_" & Format(Now, "yyyymmdd")
GetFileName = Application.GetSaveAsFilename(CurrentFile & FileExt)
If GetFileName <> False Then 'Cancel returns false, otherwise it returns the filename
ActiveWorkbook.SaveAs GetFileName
End If
End Function
This also allows for people to have files named test_1.xlsx and What_a_lot_of_underscores.xlsm without having to worry about something destructing the name

Read and write to the registry with VB.NET [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
I made a game and I would like to store the high score and other values in the windows registry. It's made in VB.NET. Could someone give me a sample code example of simple reading and writing to the registry.
Thanks
You must open Registry sub Key before read or write. Then you can read or write
Dim regKey As RegistryKey
Dim Value As Object
regKey =My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Software\VB_and_VBA_Program_Settings", True)
'Here u can read value of AppName
Value = regKey.GetValue("AppName", "Default Value")
'Or u can write the value
value=regkey.setValue("AppName", "myApp")
regKey.Close()
Simply...
Imports Microsoft.VisualBasic
Dim s As String
SaveSetting("(AppName)", "(SectionName)", "(Key)", "(Your Value)")
s = GetSetting("(AppName)", "(SectionName)", "(Key)", "(Default Value)")
Replace (AppName), (SectionName), (Key) with appropriate values. The data will be saved in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\(AppName)
http://www.vbdotnetheaven.com/UploadFile/mahesh/WindowsRegistry04262005045814AM/WindowsRegistry.aspx
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx
I'm more comfortable with C#, but it's pretty straightforward with VB.NET too. Here's an example of how to write to the registry, and another example of how to read from the registry. Don't forget to import the Microsoft.Win32 namespace.
You can use registry.getvalue and registry.setvalue. Here are a couple of examples used for default file types:
Registry.GetValue("HKEY_CURRENT_USER\software\classes" & "\" & fileFormatExt(i), "", "error")
Registry.SetValue("HKEY_CURRENT_USER\software\classes\" & FileType, "", appTag) ' set new value, overwrite any other, creates key if not there.