How to declare a path without the drive letter? - vb.net

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.

Related

Merge pdf files with VBA and Foxit

I use Foxit Phantompdf, the complete version and ACCESS.
In our program, we have to save multiple pdf files, some of them should be merged in single files when saved.
Here is the code I use;
Dim phApp As PhantomPDF.Application
Dim n1 As String
Dim n2 As String
n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"
n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***
Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)
phApp.Exit
When I try it with the complete files names (in bold) the code works perfectly.
However, when I try to use variables, I get a
"Argument not optional"
error.
Can somebody help me ?
Thanks
Your string definitions in call line is incorrect.
You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.
Furthermore I do not know the actual needed syntax for this phcreator.combine()
But is it not possible using only:
call pHcreator.combine(n1 & "|" & n2, …
The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?
But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?
I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.
I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:
Private Sub Command4_Click()
Dim addi As String 'file to be merged to main file
Dim princi As String 'main file
Dim phApp As PhantomPDF.Application
'A temporary folder, in this case c:\t2, should be present
'In this example c:\Temp is the working folder
addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged
princi = "c:\Temp\mainfile.pdf" 'full path of main file
'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function
FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder
FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder
'Merge action
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)
phApp.Exit
'Save merged file in working folder under main file name
Kill princi
FileCopy "c:\t2\fmain.pdf", princi
'delete temporary files
Kill "c:\t2\fadd.pdf"
Kill "c:\t2\fmain.pdf"
End Sub

Solidworks VBA save as new name

I am currently generating a solidworks part file name from a text document for a batch "rename" of the documents. This assigns sequential numbers to them which i then save each part in a folder as. I have been trying to figure out a method for the new file names to be referenced in the assembly they were a part of.
For saving the document I am using :
swModel.SaveAs3 "" & FileName & "", 0, 0
I have no issue creating all the files in a batch but I could not seem to find a method of apply this save in such a way that any open documents that it is referenced in change to referencing the new part name.
Edit: I figured out a serviceable solution using some of the information held in this link:
help.solidworks
Using the pack and go functionality in solidworks avoids having to deal with the references as they are already handled by the process itself Hope this helps.
Try the below approach:
Dim FileName As String
FileName = "blablabla" & Variable & "blablabla"
swModel.SaveAs3 ("C:\User\Username\Desktop" + FileName + ".SLDPRT", 0, 1)
Variable = Variable + 1
'continue with your loop

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 to use String name in StringBuilder.AppendLine()?

I'm trying to use string name in appendline() function. The string named "programFiles" gets the program files location correctly, but I need to include it in append line (to write the path to the file) Tried this,
Dim programFiles As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Dim sb As New StringBuilder()
sb.AppendLine("Your " & programFiles & "is working properly.")
File.WriteAllText(Environment.GetEnvironmentVariable("appdata") & "\mytext.txt", sb.ToString())
But it doesn't write to a file. What is the correct way to do that ?
When I tried your code on my machine, it worked as I would have expected, the file c:\users\[myusername]\Appdata\Roaming\mytext.txt was created and contains one line
"Your C:\Program Files (x86)is working properly."
so obviously your VB is correct. Maybe you're looking in the wrong subdirectory?

Creating a directory named with user's text in Visual Basic

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)