KB-48678: Retrieving a file’s name and extension via scripts.

Question:

How can I retrieve a certain file’s name and extension via ShowFilePicker method? (Example: the path “C:\test\test.txt” must return only “test.txt”.)

Solution:

To do so, you can use the following methods: Len (to return the number of characters in a string), Right (to return a specific number of characters to the right), and InStrRev (to return the position of a given character on a string). With these methods, you can find out the string’s size and the position of the last backslash (“\”); this is deducted from the text’s full size, and thus the name of the file is returned.

Below is an example of script using the commands described above:

Dim FileName, size, pos, file
Application.ShowFilePicker true, FileName, “pdf”,128, “Files PDF|*.pdf|All Files” 
size = Len(FileName)
pos = InStrRev(FileName,“\”, size,0)
file = Right(FileName, size – pos)
MsgBox file

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *