Finding hidden or system files containing ~ using Dir() in VBA - excel

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)

Related

Using Dir with legacy file suffixes

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

Using Directories in Excel

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

How to find the file path of a file in livecode

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

Proper Syntax for revZipAddUncompressedItemWithFile?

I recently found an error in the documentation for revZipAddItemWithData. Now I'm trying to do something similar, but this time using revZipAddUncompressedItemWithFile. I suspect I have a similar problem. I've tried every combination of quotes or no quotes around the arguments, with no luck.
I'm not having trouble with any other handlers reading or writing to the zip archive, just this one.
Any ideas?
command SaveIssue
put field "Archive Path" into tPath
ask file "Save as:" with "someimage.jpg"
put it into tFilePath
set itemDelimiter to slash
put item -1 of tFilePath into tFileName
revZipOpenArchive tPath, "update"
revZipAddUncompressedItemWithFile tPath, tFileName, "tFilePath"
revZipCloseArchive tPath
end SaveIssue
Using LiveCode 6.6.2 stable, Mac OSX 10.9.4
I get it working with no quotes around the arguments;
revZipAddUncompressedItemWithFile tPath, tFileName, tFilePath
However, it doesn't work with an archive that I create by right-clicking a file (on a Mac) and selecting 'Compress ...' (The archive is reported as 'damaged' if I try to open it afterwards!)
It does work with an archive created with LiveCode

Using applescript to move files whose names appear in an excel spreadsheet

I have a folder with about 4000 image files. I need to select some of those files, based on whether their names appear in an excel spreadsheet, and copy them to a new folder. If possible, I would also like to tag them with a color tag so I know which files in the original folder were moved and which were not. I got the code below from here.
tell application "Microsoft Excel"
tell worksheet 1 of active workbook
set fileList to value of used range
end tell
end tell
set rootFolder to "path/to/sourcefolder" as POSIX file
set filesToMove to {}
repeat with thisItem in fileList
try
set end of filesToMove to alias (rootFolder & thisItem)
on error
display dialog "File " & thisItem & " is not in the folder"
end try
end repeat
tell application "Finder"
move filesToMove to folder "path/to/destination" as POSIX file
end tell
When I run it, it's unable to find any of the files. I think the issue might have to do with using / in my file path instead of : but I don't know how to set a path to an external drive with the applescript syntax. Using the same variable declaration, set rootFolder to "path/to/sourcefolder" as POSIX file, and then telling finder to open rootFolder works fine.
I also need the script to have more flexibility - not all files have the same naming conventions, and so I need it to search for any files whose names contain the identifiers from the spreadsheet, but may not match it exactly. I.E. the spreadsheet entry may be "00103", but the file name is "PX00103KL.jpg."
In the meantime, I've created an automator workflow that moves and tags the files exactly the way I want, but only after I manually enter the search string. If I could combine those two ideas, of iterating through the spreadsheet entries in excel and using each in turn as input for the automator workflow, that would be ideal.
Thanks for any help!
I don't have Excel, but assuming that you can get a list of strings from it, here is a bit of code that will find all files in a target folder that contain the string(s), set a label to it and then copy it. Change appropriately.
It uses the unix command line tool for spotlight, mdlist to find the files. Trying to do it purely in applescript seems to be painful. The shell command when stitched together is something like this:
mdfind -onlyin ~/Documents/ "kMDItemDisplayName == '*somestring*'"
Here is the code:
set stringList to {"144", "g_"}
set rootFolder to "/Users/blah/Downloads/" as POSIX file
set destFolder to "/Users/blah/Downloads/test" as POSIX file
set filesToMove to {}
set mdAtr to "\"kMDItemDisplayName == '*"
repeat with thisItem in stringList
set sPath to quoted form of POSIX path of rootFolder
set sName to mdAtr & thisItem & "*'\""
set sCmd to "mdfind -onlyin " & sPath & " " & sName
set sResults to (do shell script sCmd)
set fList to (every paragraph of sResults) as list
repeat with i in fList
set f to (POSIX file i) as alias
tell application "Finder"
set label index of f to 3
#copy file f to folder (destFolder as alias)
end tell
end repeat
end repeat

Resources