Creating a directory named with user's text in Visual Basic - vb.net

I'm fairly new to programming in general. I'm working on a simple app that could combine a couple of functions to automate or simplify some of the things I do at work. One of the functions I'm trying to build is to be able to create a folder. Now I have found an article on that on Microsoft's msdn resource and it's child's play. But the instructions there only show how to create a folder with a predefined name in the code. What I'd rather want is to have a textbox where I input the folder's name and the directory is named with that input. The msdn code looks like this:
My.Computer.FileSystem.CreateDirectory _
("C:\vb\")
I understand I should now add:
Dim txt As String
txt = TextBox1.Text
But what next? How do I tell VB to use as directory name the input "txt"?

Try this:
Dim txt As String
txt = TextBox1.Text
My.Computer.FileSystem.CreateDirectory("C:\" & txt & "\")

Using & is simple and often fine for most purposes, but for your two (including what you've added as a comment) concatenation examples there are other methods:
For pathname manipulation look at System.IO.Path:
My.Computer.FileSystem.CreateDirectory(Path.Combine("C:\", txt))
For (complex) string "formatting", consider String.Format:
Dim menu As String = String.Format("Today's main dish is {0}.", TextBox2.Text)

Related

How to declare a path without the drive letter?

Example i have picture folder located at
Dim path As String = "D:\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Something like that. now i want the the picture folder to just paste it in debug folder can i call it without the drive letter something like this ??
Dim path As String = "\bin\debug\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Its not working. The reason why i want to achieve this is example i use my program in another computer but the other computer don't have drive D: then my program will not work because in my code all of the student picture declared in drive D: thank you so much.
You can get the application startup path and create the folder in either the Debug or Release locations, depending which you're running. The folder would be created within the directory in which the end-user runs the compiled application.
Dim PicPath As String = Application.StartupPath & "\Student Picture
If Not My.Computer.FileSystem.DirectoryExists(PicPath) Then
My.Computer.FileSystem.CreateDirectory(PicPath)
End If
PicPath &= "\Student-" & textbox_Search.Text & ".jpg""
Small edit- I realised you wanted to include file name as well. Please do not use "+" when concatenating strings, the proper operator is "&" use the "+" operator only when youre doing math and some other unique cases, such as datatable expression column concatenation.

Using ParseQueryString in Winform

My program is writing URL-encoded strings into a text file like this one.
topic1=1&topic2=2&topic3=3&
However, I'm trying to reverse the process by opening the same file and breaking that file up in a manner that I can retrieve into name/value-like pairs.
' Final Challenge Category
Dim sr As StreamReader = New StreamReader("./Game" + nudGameNo.Value.ToString() + "/Final/category.txt")
strLine = WebUtility.UrlDecode(sr.ReadLine)
The closest function that I can find to help is HttpUtility.ParseQueryString but I can't seem to run it in a WinForms application. (Even if I use Imports System.Web)
I've also tried to do a .Split with & being the separator, however problems start up if a particular value contains an & of it's own.
Is it possible to break this form of string up?

VB.NET - search for directory and delete if it contains certain characters

I have searched high and low and cannot find a way of doing this. I am writing a program that will run at logon and delete a directory within another directory. Our company has a software application that contains a directory that sometimes becomes corrupted. The issue is that the directory contains some static words and then is appended with a randomly generated set of characters. Thus, the need for a search for the static words and delete any directory that contains them. This is kicking my butt. Thanks for any help!
Edit:
My apologies for not adding some or all of the code that I've written thus far. I can delete the static directories, but not the dynamic ones. Again, I'm teaching myself and I'm sure there's better ways of doing what I need, but I don't know them. I'm also relatively certain that my code is messy and such. I would love some constructive criticism, but please don't bash me for trying. Please see below. Thanks!
Imports System.IO
Module Module1
Public Sub Main()
'I'm wanting to see the user name output in the console
Dim user As String
user = Environment.UserName
Console.Write(user)
'new line
Console.WriteLine()
Dim path1 As String
path1 = "\appdata\local\DIRECTORY\APPLICATIONNAME.exe_Url_ny2thmvtmqmw4jiqk1yuytwfbddruu02"
Dim path2 As String
path2 = "\appdata\local\DIRECTORY\APPLICATIONNAME.exe_Url_r3joylqll52q54guz0002pxu4swqous0"
Dim fullpath As String
fullpath = "C:\Users\" & user & path1
Dim fullpath2 As String
fullpath2 = "C:\Users\" & user & path2
Dim toplevel As String
toplevel = "\appdata\local\APPLICATIONNAME\"
Dim toplevel1 As String
toplevel1 = "C:\Users" & user & toplevel
If Directory.Exists(fullpath) = True Then
Directory.Delete(fullpath, True)
ElseIf Directory.Exists(fullpath2) = True Then
Directory.Delete(fullpath2, True)
End If
'I would like to keep the window open until I work the kinks out
Console.WriteLine("Finished. You may now close this window.")
Console.ReadKey()
End Sub
End Module
This should do what you need. I've included parameter names to make it more readable. You can strip them out if you prefer the more concise approach...
' Will find all directories under C:\Root\Folder\
' (including subdirectories) with a name that starts
' with "SearchString", then delete them and their contents
System.IO.
Directory.
GetDirectories(
path:="C:\Root\Folder\",
searchPattern:="SearchString*",
searchOption:=System.IO.SearchOption.AllDirectories).
ToList().
ForEach(Sub(x) System.IO.Directory.Delete(path:=x, recursive:=True))
That said, This is simply joining together two tasks:
Finding a list of directories
Deleting each one in turn
There are many tutorials and examples on the internet (and numerous questions on Stack Overflow) relating to these topics.
Edit: The concise version
Imports System.IO
Directory.GetDirectories("C:\Root\Folder\", "SearchString*", SearchOption.AllDirectories).
ToList().ForEach(Sub(x) Directory.Delete(x, True))
try using something like
this code below deletes every folder thats contains the pattern listed on
the string array.
Dim Words() As String = {"Word1","Word3","Word4",.."Wordn"}
For Each iPatternWord as String In Words
For Each iDir As System.IO.DirectoryInfo In System.IO.Directory.GetDirectories(#"C:\",iPattern)
iDir.Delete(true);//===>Delete this folder.
Loop
Loop

How do I trim all text up to and including a \ in a VB.net string

I'm sure if I knew the correct terminology I may have been able to search an answer... But my Visual Studio VB skills are somewhat n00bish.
My VB runs a dos batch, that dumps the DOMAIN\USERNAME of the current logged on user for a given IP address to a text file (GETUSER.txt)
What I need to do with this string (taken from the txt file created above), is create two strings, one that is the DOMAIN, one that is the USERNAME and to drop the \ entirely.
The problem is that my work environment consists of multiple domains, and varying username styles. The only constant is the \ separating the DOMAIN and USERNAME.
I've looked at the REPLACE and various STRING functions, but I'm struggling folks.
The below is the section of VB code that runs when you click OK on the dialogue box (after entering an IP). "MAIN" is the name of the parent window that has the Public VARs in it.
Public Shared IPADDRESS As String
Public Shared USERNAME As String
^ are the Public VARS used.
Dim GETUSER As New ProcessStartInfo("TOOLS\GETUSER.bat")
Dim fileReader As System.IO.StreamReader
Dim stringReader As String
MAIN.IPADDRESS = NEW_IP_ADD.Text
MAIN.IP_DISPLAY.Text = MAIN.IPADDRESS
GETUSER.WindowStyle = ProcessWindowStyle.Hidden
GETUSER.Arguments = MAIN.IPADDRESS
Process.Start(GETUSER)
fileReader =
My.Computer.FileSystem.OpenTextFileReader("TOOLS\GETUSER.txt")
stringReader = fileReader.ReadLine()
MAIN.USERNAME = stringReader
MAIN.USER_NAME.Text = MAIN.USERNAME
I hope this makes sense, and isn't too impossible to decipher... I look forward to your responses....
Please remember, I'm VERY new at VB, and Visual Studio... also... I'm more than happy if people post links to places that know I can get my answer, or help me with function names or anything designed to help me find the answer myself... it's the only way to learn... I just think I need a nudge in the right direction...
Cheers,
Tim.
It sounds like what you want to do is to split the string. And handily, there's a Split method:
Returns a string array that contains the substrings...
So you'd do something like:
Dim parts = MAIN.USERNAME.Split("\"C)
Dim domain = parts(0)
Dim userNameWithoutDOmain = parts(1)

How to create a dynamic excel link in VB.NET? (not .dll)

VB.NET level: Beginner
I made a .exe using VB.NET. In this program there are many links to excel files (There are specific folders for excel files).
My problem:
Consider an excel file named as abc.xlsx, which is on my home pc. Link to this file is as follows,
D:\work\data\abc.xlsx
now for obvious reasons, this link will not be valid when I run the .exe on my work pc.
(Later I want to run this .exe on multiple pc's)
How to solve this issue?
My thinking is to create a dynamic link which will update itself based on pc in use or to create a constant link which is independent of pc in use.
Help will be really appreciated.
Thanks in advance
so make the path relative to the .exe
C:\myapp\myapp.exe
c:\myapp\data\abc.xslx
...
So no matter where you app is, you can get to your data like this
Dim dataFolder As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location)
dataFolder = System.IO.Path.Combine(dataFolder,"data")
Dim theFileIwant as String = System.IO.Path.Combine(datafolder,"abc.xslx")
Try something like this:
Public Function GetDynamicFilename(p_filename As String) As String
Dim tempPath As String
Select Case My.Computer.Name.ToUpper
Case "COMPUTER1"
tempPath = "c:\work\data"
Case "COMPUTER2"
tempPath = "d:\work\files"
End Select
Return String.Format("{0}\{1}", tempPath, p_filename)
End Function