A recently asked question includes the code:
strFilename = Dir$(fPath & "*.xls")
which is (successfully) used to find files ending xls and xlsx.
My question is, why does this find xlsx files? It doesn't follow the normal rules of wildcard pattern matching. If I had not seen this used this way I would (and have, repeatedly) use the following for files ending xls and xlsx :
strFilename = Dir$(fPath & "*.xls*")
I thought perhaps that pattern matching using Dir$ automatically added the equivalent of a * to the end of the search pattern - but after some testing with "*.xl" I can rule that out. Also, to further muddy things, "*.xls" will not pick up a xlsm or xlsb file.
So this would suggest that perhaps Dir$ has some inbuilt recognition of legacy vs current file suffixes, or an inbuilt list of similarities/synonyms that it should include. But it appears to be one way - as "*.xlsx" will not find a xls file.
Does anyone know if this is the case? Are there others? Are they documented anywhere? When did it change?
Further to that, if I wanted to craft a Dir$ that only found xls files, how might I do that without further checks?
P.S. Dir and Dir$ are interchangeable in this question.
Dir in VBA behaves like the Dir-Command on the command shell. And the command shell Dir matches both the long name (that is the name you see in Windows) and the short name. If you never heard about that: see for example https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names . Best documentation that I found is https://ss64.com/nt/syntax-wildcards.html - where it simply states "Wildcards match both the Short and Long filename"
The extension for short name of a xlsx-file is xls, that's the reason your Dir finds the files.
As far as I know, there is nothing you can do, you will have to check the real file name manually. See also https://stackoverflow.com/a/66674917/7599798
Related
I'm writing some code to parse a zip file headers. I've read through the specification and can't seem to find an answer for telling apart directory names from file names. C# seems to use the convention that directories end with a trailing slash but the spec doesn't say anything about it, so I'm wondering if this is just convention or if there is a better way to do it.
Recently a large number of security updates were pushed for Microsoft Office at work. We have an Excel tool with a Visual Basic backbone which worked prior to the updates, and now does not work.
I have done some isolated testing and have found that we see the same 'error 75' when using the FileCopy method on .BAT and .EXE files. Other less 'risky' file types seem to be ok. This is also an issue with the 'Open' command.
Does anybody know of a workaround for this issue when copy/pasting .EXE and .BAT files, or maybe this is a known issue with a certain security update? I have searched everywhere, but the Google is failing me. I am hoping either:
Someone knows which security update is the culprit and I can try
uninstalling it to see if that fixes the issue
There is a different way to copy/open/manipulate .BAT and .EXE files that doesn't violate the new security patch.
Some background:
We are using Microsoft Office Professional Plus 2010 and the tool uses Excel as the interface.
Here is the testing code I am using. I have verified that .TXT and .DAT files run just fine while .EXE and .BAT files cause 'error 75' indicating a permissions error. And before you ask, yes, the file path is correct, I have just omitted my employee identification information.
Sub Macro1()
' Declare variables
Dim Filename, SourcePath, DestPath As String
' Pathname variables
Filename = "test.dat"
SourcePath = "C:\Users\<REDACTED>\Desktop\working_copy\" & Filename
DestPath = "C:\Users\<REDACTED>\Desktop\working_copy\test\" & Filename
' Copy file from Source and paste in Destination
FileCopy SourcePath, DestPath
End Sub
Here is an image with the updates that were installed. The tool worked prior to the 6/14/2017 dump, so anything before can be disregarded.
Sorry for the image, too many to enter them manually, no easy way to copy from the update list:
MS Office 2010 Security Updates
Nothing with the tool or file structure changed. The only thing different is the updates. Also, we tested the same tool with Microsoft Office Home 2016 and it worked without a hitch.
Any and all help will be appreciated. Thanks in advance!
While I never found a real fix for this issue, I did come up with a slightly 'hacky' work-around.
The MS security patch prevented me from copy/pasting .BAT and .EXE files, I but could still save .BAT files.
So, I ended up having my script create a text file called "copyExe.txt" which had a single line:
ECHO F|xcopy <source> <destination>
This was then renamed to be "copyExe.bat" and run.
By creating a .txt file, I circumvented the errors when using the VBA Open function on .BAT files (so I could actually create this new batch file). This batch file has the sole purpose of copy/pasting a .EXE file from one folder to another, which circumvented errors when trying to copy/paste .EXE files.
After the copy/paste script is run, it is deleted.
All-in-all it feels like a very silly solution, but it works and is very non-invasive for the user.
I'm trying to get some data out of excel files. Never used excel before but I understand VB. So I have a file with a button my boss implemented as well as his code he used. Here is the code:
(you can see the "Get Data" button in there)
My exact problem is that I don't know a lot about excel so if someone could help me figure out what is going on as well as figuring out how to correctly use directories that would be awesome! For more context when you click the button it gets all files in the directory and then the user clicks a file then excel follows a macro to get some data out of sed file.
Right now it returns nothing but there is 5 files in that directory?
Here is what the default directory looks like after edit:
And here is the files in my directory:
As #Tim Williams says ... Insert the back slash after the folder name
DirNow = Dir(Range("DefaultDirectory") + "\WFP*", vbDirectory)
EDIT
Basic troublseshooting
Does it it compile?
Looks like "UserFormDataa" - should maybe be "UserFormData"
Add Debug.Print DirNow after it's set and show us the display
If that's not right add line
Debug.Print Range("DefaultDirectory") to makes sure range is defined properly
Remove vbDirectory - unless you're going to handle processing files under the matching sub-folders
You are missing the terminal backslash on your default directory.
C:\Users\CUCCOMTT\Desktop\Excel Project\
Otherwise you're looking for files named
C:\Users\CUCCOMTT\Desktop\Excel ProjectWFP
So, I'm incredibly new to LiveCode and I have an external file in the same directory as the .livecode file called 'words.txt', with english words, each on a new line. I plan to read this file into a variable and then pick a random word from that variable. However, I am stumped as to how I must find the file path and insert this into the syntax required for me to do this. My code is as follows:
put url ("binfile:" & filePathGoesHere) into dictionary
replace crlf with lf in dictionary
replace numToChar(13) with lf in dictionary
put any line of dictionary into randomword
The file path is supposed to be inserted into the code at filePathGoesHere. Once the program is compiled I will be moving it and its resources around a bit (from computer to computer), so, beyond the text file staying in the same folder as the compiled program, the file path will change. What extra code would I need to add to make this work, if the folder the compiled program and the txt file is in is called "MyProgram"?
Help is much appreciated, and if further specification is required I can provide it. I also have a folder called "resources" if moving it there can help.
If the stack you're building is for your own use, you can place external files anywhere, but if you're going to deliver your stack to other users, you need plan where you external files are going to be placed, and how.
An easy way to determine the path to a file that sits immediately outside your stack is using the stack's filename:
put the fileName of this stack into theFilePath
set the itemDel to "/"
put "words.txt" into the last item of theFilePath
Now theFilePath variable will an absolute path reference to your external file. If the file is placed inside a folder "TextFiles" you can do this:
put the fileName of this stack into theFilePath
set the itemDel to "/"
put "TextFiles/words.txt" into the last item of theFilePath
If you're going to deliver your stack to other people, you should write your external file/s into a common system folder, or you need to use an installer to define where your files/folders will be placed. Common folder paths are found using the specialFolderPath function:
put specialFolderPath("Documents") into the theFolderPath
A somewhat recent addition to LiveCode is a "Resources" folder -- specialFolderPath("Resources") -- which can be handy for delivering on desktop and mobile platforms. Also, keep in mind that few of these folders allow writing to existing files contained in them for security reasons. "Preferences" and "Documents" are two examples of folders where you can change the contents of files.
The LC dictionary contains details of each of the folders.
If you use the file: scheme instead of bindle: LiveCode will automatically convert end of line characters to LF, so that step may not be necessary. (Although you might need it if you are reading a text file produced in native Windows encoding on a Mac.) You don't even necessarily need to read it into a variable. You could do this:
put any line of URL ("file:" & specialFolderPath("resources") & "/words.txt") \
into tRandomWord
I'm trying to rename a series of files for SharePoint that contain illegal characters such as #, &, ~, etc. I modified the code from this site to find the files/folders to rename. When I run the following command
strTemp = Dir(strFolder & "*~*", vbHidden)
(or with any other attributes for that matter) I get a huge list of files in the directory that do not contain a ~, and yet it still does not seem to find some files that contain an initial ~ (such as temporary files caused by editing a document that were never deleted).
Anybody out there know how to locate those files?
Apologies for posting this as an answer but I don't seem able to add a comment.
ecksc has answered his own question, but I just want to point out that you don't need to replace the constants with the number 6. To combine attributes you can add them using +, for example:
strTemp = Dir(strFolder & "*~*", vbHidden + vbSystem)