ASP FileObject.Name converting to '?' - iis

I have a script that lists all files in a directory, then for each one it will Response.Write the name and how many downloads it has.
I have everything completed, but when I went for a test, the files that have "odd" characters in the name are replace with a ?
I'm guessing, that since some files have foreign languages as there name, and that some have the iPhone emoji icons in the name, that it doesn't recognize it and puts a ? instead, but this is a serious issue since I can't give the correct file name back to the user, then that incorrect name is fed back into the url to download. (Which doesn't work)
Any suggestions?
Edit:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("."))
for each file in fo.files
if fs.GetExtensionName(file.Path) = "plist" then
dim tempList, tempName, ...
tempList = split(file.Name, ".")
'Manipulate name and data ...
Response.write(name)
end if
next
The file names themselves have odd characters, and file.Name returns a ? instead of what is actually there.
18アイコン is one example.

Here's some code which works fine for me:
<%# Language="VBScript" CodePage="65001" %><%
Option Explicit
Response.CodePage = 65001
Response.CharSet = "utf-8"
Dim fs, fo, file
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(Server.MapPath("."))
For Each file In fo.files
If fs.GetExtensionName(file.Path) = "plist" Then
' Do whatever here...
Response.Write file.Name & "<br>"
End If
Next
%>
If you are using any variables that you didn't dimension beforehand, you'll need to remove the Option Explicit; otherwise, VBScript will complain that you didn't dimension them.
Edit: I copy & pasted the wrong code; this code works.

Related

Open a workbook without knowing the exact name

so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you
You can get the file name using the Dir function and multiple character (*) wildcard.
Const Path As String = "C:\Test"
Dim filename As String
filename = Dir(Path & "\*.xlsx")
If Len(filename) > 0 Then
' Do your work
' Remember 'filename' only holds the file name
' you will need to attach the rest of the path to get the full directory.
End If
Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.
An example:
File name: daily_report_20190404.xlsx
filename = Dir(Path & "\daily_report_*.xlsx")
Hope this helps.

Open a PDF from Excel with VBA in Google Chrome on a specific page

I am creating a macro in Excel that should open a PDF document on a specified page with chrome.
Generally, the opening part works. My problem is that when I add the page number (e.g. #page=15) to the url, the shell encodes the "#" symbol into "%23", which Chrome is not able to interpret correctly (file not found).
Here is my code
'The path to the file, replaces spaces with the encoding "%20"
Path = Replace((filePath& "#Page=" & iPageNum), " ", "%20")
Dim wshShell, chromePath As String, shellPath As String
Set wshShell = CreateObject("WScript.Shell")
chromePath = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe\")
shellPath = CStr(chromePath) & " -url " & Path
If Not chromePath = "" Then
'how I first tried it:
Shell (shellPath)
'for testing purposes, led to the same result though:
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
End If
Is there a different way to do this with Chrome? I haven't found anything that reads the installation path dynamically.
You just need to specify the protocol file:/// if you want to load files from the local hard disk. Then # gets not translated into %23.
Shell ("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" ""file:///C:\Users\t.weinmuellner\Desktop\Evon\PDF Opening\PDFDocument.pdf#page=17""")
If Adobe Acrobat Reader is installed on the system I would suggest to use the funcion openPDF from Daniel Pineault. This will open the file in Adobe Reader directly. You can find the source code of the function here
A test could look like that
Sub TestSO()
Dim fileName As String
Dim pageNo As Long
fileName = "Path and filename of PDF"
pageNo = 20
OpenPDF fileName, 20
End Sub

How to know numerically the last saved file name

I am creating and saving .ini files in Excel. The files need to follow a specific naming convention that increments by 1 each time a file is created. Not all the files will be created in Excel, some will be done in a separate program. Is there a way to read the saved files in their folder to know which number is next?
For example there are files named exampleFile1.ini through exampleFile63.ini. From Excel using VBA or other means can I see that exampleFile63.ini was the last file and name the next exampleFile64.ini?
Thank you. I'm very new if thats not obvious.
This function will return the next available .INI file name:
Private Function GetNextFile() As String
Dim i As Long
Dim fileName As String
For i = 1 To 1000
' Update Path and File Name prefix below to where your .INI files will be stored.
fileName = "d:\path\exampleFile" & i & ".ini"
If Dir(fileName) = "" Then
GetNextFile = fileName
Exit Function
End If
Next
End Function
Call it like this:
Dim NextIniFile As String
NextIniFile = GetNextFile()

Determine Image in Excel Comment

I have found plenty of vba for inserting images into a comment
Selection.ShapeRange.Fill.UserPicture "C:\Temp\Pictures\ewe.jpg"
How can you determine the image already used for an comment?
I would like to extract the embedded image names if possible.
Is there not a property to access that will give me this?
In the comment Fill Effects dialog box the image name somehow seems to be accessible.
Sorry, I didn't have the reputation to just comment on your question for clarification.
I made a test file, inserted a comment and image in that comment, and then extracted the base files. I then checked them all for the original file name. I also found the embedded JPEG and decoded it to get the metadata. As you've noted, the original file names are stored in xl\drawings\vmlDrawing1.vml (once you've extracted the xml files from the excel file by appending .zip to the filename and then running an unzip utility on it). I did find the file name, but not the path or file type, so I'm fairly certain that the path and file type aren't preserved.
If just the file name is sufficient for you, then that file contains information for each drawing that you have, and those will include the cell location, although they're 0 based, so you'd have to add one to get the actual row and column. My question is two part:
1) Is the file name alone sufficient, or did you need the entire path? If you needed the entire path, I think you're out of luck, since the paths are on a different computer and you can't even search for them if you do extract the file name.
2) If that is all you need, does the solution have to be VBA? In the past, I have programmatically unzipped and manipulated the xml base files, but it's a little tricky. It's simplified by the fact that you only have to read out the data, so that's a plus. I did it in .net before, but I'm sure that if it had to be VBA it could be done, but it would be simpler if you were open to the type of solution.
Let me know, I'd be happy to help you out.
====================================================================================
Try this: make a copy of the spreadsheet, append .zip (test.xlsm.zip), and then extract the files manually. Change vmlPath to the location of your xl\drawings\vmlDrawing1.vml file. Then run this. I did make some assumptions, for instance, I assumed that the order of the nodes and attributes would always be the same and so I used hardcoded indexes (shp.attributes(0), etc) instead of using logic to make sure I had the correct node or attribute, but you seem like you know your way around VBA, so I'm just going to code a barebones. This will need a reference to Microsoft XML 6.0.
Sub vmlParse()
Dim vmlPath As String: vmlPath = "C:\Users\Lenovo\Desktop\test - Copy.xlsm\xl\drawings\vmlDrawing1.vml"
Dim this As Worksheet: Set this = ActiveSheet
Dim doc As New DOMDocument, shps As IXMLDOMNodeList
Dim shp As IXMLDOMNode, n As IXMLDOMNode, a As IXMLDOMAttribute
Dim fileName As String, productID As String
Dim rng As Range, r As Long, c As Long
doc.Load vmlPath
Set shps = doc.getElementsByTagName("x:ClientData")
For Each shp In shps
If shp.Attributes(0).nodeValue = "Note" Then
r = 0: c = 0
For Each a In shp.ParentNode.FirstChild.Attributes
If a.nodeName = "o:title" Then
fileName = a.nodeValue
Exit For
End If
Next
For Each n In shp.childNodes
If n.nodeName = "x:Row" Then r = n.text
If n.nodeName = "x:Column" Then c = n.text
Next
Set rng = this.Cells(r + 1, c + 1)
productID = rng.Value
'now you have the productID, the fileName, and the cell location
End If
Next
End Sub
Let me know how that worked out for you.
If c4 contains your comment:
Set shp = Range("C4").Comment.Shape
if shp.Fill.TextureType = msoTextureUserDefined then
end if

Retrieving last line of a txt file with a VBscript

Before all, I want to say that I am not a programmer, so this may be basic for some people but surely not for me!!
The task that I want to accomplish is to retrieve some characters of a data file that is imported automatically from a server.
Data is stored in lines in a CSV or tabbed .txt file, each line consists of date and some numeric values. The format is always the same, only the file grows in one line each time a new value is entered.
What I need the script to do, is open that file (wich adress is known and constant) search for the last line, and then extract a string from that line and write it on a different .TXT file, from where I can import it to another specific software as a raw value.
The part in the middle (extracting string) is fairly simple, but opening and isolating the last line is far too much for me.
Thanks everybody for helping!
dim path
path = "fileName.txt"
otherOption(path)
function otherOption(fileName)
const read = 1
dim arrFileLines()
set objArgs = CreateObject("scripting.FileSystemObject")
if objArgs.FileExists(fileName) then
set objFile = objArgs.OpenTextFile(fileName,read)
i=0
do until objFile.AtEndOfStream
redim preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
loop
objFile.Close
end if
wscript.Echo arrFileLines(i-1)
end function

Resources