Why does calling FindFirstFile with the pattern *.* match a name like Windows?
Edit: I guess I can also guess what's happening, but is there any documentation on the reason as well?
In the blog post 'How did wildcards work in MS-DOS?' Raymond Chen describes how the original DOS wildcard matching was implemented. At the end of the post he points out how *.* is handled as a special case in the Win32 wildcard matching algorithm.
A quote from the post
For example, if your pattern ends in .*, the .* is ignored. Without this rule, the pattern *.* would match only files that contained a dot, which would break probably 90% of all the batch files on the planet, as well as everybody's muscle memory, since everybody running Windows NT 3.1 grew up in a world where *.* meant all files.
*.* matches everything in the target directory.
This is because *. matches up to the final period; if there is no period in the name then that name is treated as if it ended in a period; so *. matches only names beginning with a period and names containing no period (.afile / adirname) if you add a * on the end for *.* then it also matches beyond the final period so includes file names containing a period, this covers all possible file names.
Related
I have a regex that I am using to search for files .ipa files in my '_inbox' folder. It works for files that are directly under that folder. But now I need to modify it to find files in subfolders.
current regex
%r{_inbox/[^/]+.ipa}i
matches
'_inbox/NewApplication.ipa'
does not match
'_inbox/Test/1/NewApplication.ipa'
I don't have the rep required to comment so I'll do my best to answer without. I think ruby supports lookahead, if it doesn't, this answer is partially invalid.
I think this regex should cover what you need, or at least be a good starting point:
_inbox/[^/](?!//)[\w\d\_\-\./]+?\.ipa
This RegEx will match a file path starting with _inbox/, unless there is another slash afterwards.
Next, it uses a negative lookahead (?!//) to ensure that the rest of the subject string doesn't contain two consecutive slashes. If it doesn't contain that, it makes sure that the rest of the string is made entirely of upper/lower case letters: \w, digits: \d, underscores, dashes, dots or forward slashes: \_\-\./. Finally, it checks that the path ends with the file extension: \.ipa.
Hope this helps.
You can match subfolders with (?:[^/]+/)*:
%r{_inbox/(?:[^/]+/)*[^/]+\.ipa}i
Please see demo.
Also, you'd better escape the dot to match the literal dot.
Why use a regex rather than Dir#glob? If the current directory contains the "_inbox" subdirectory, the following will return the array you want:
Dir.glob(File.join("**","_inbox","**","*.ipa"))
I have many text files, and I need to add some text (e.g. MNP) to the beginning of the first line in each file.
How can I do this in Notepad++?
(I'm using v6.6.9)
Make sure to backup your work beforehand, and set proper extension of files to affect and folder to search through before you do this.
You can use regular expressions. Several places around the internet claim that the regex \A works, but it wasn't working for me, it was cycling byte by byte through. I found that \A^ sticks to 0 position of the file.
Oddly, I additionally found that I couldn't replace \A or \A^ and have it take effect. This is what worked for me.
Find: \A^(.*?)
Replace MNP\1
Truthfully, the \1 in Replace isn't even necessary since I'm cheating and basically telling notepad to look for 0 characters.
This should work just as well.
Find: \A^.*?
Replace MNP
Please backup your work beforehand though.
Alternatively, this also seems to work.
Find: .{0}(.*)
Replace: MNP\1
It effectively looks for 0 characters followed by the whole document/line (depending on whether . matches newline is checked, this choice won't matter for the outcome however).
In dos, when I wanted to get a list of files matching a specific filter, I could use something along the lines of:
*.* - Means return everything
*.txt - Means return all files with a .txt extension
*_abc.* - Means return every file that ends with _abc
My question is, using that dos filter structure, how could I return all files NOT matching *_abc.* (In other words, return all files whos name does NOT end in _abc)?
I don't remember if this is possible and I need this since a company I'm working with is using a very old program that still uses that form of command filtering for selecting files for the program to work on - Also, unfortunately, I can't do it via a batch command... It has to be a single command line statement.
Thanks!!!
Pipe the results of your listing to FINDSTR with an appropriate regex search string and the /V option to filter out matching lines.
dir /b /a-d * | findstr /v /r "_abc\.[^.]*$"
Take a look at this answer to a sort of unrelated question. The answer does show though how to do a REGEX search on file names, and of course if you use a REGEX you can easily search for things that don't match the expression.
https://stackoverflow.com/a/7381962/1246574
Hope that helps.
EDIT
Sorry, didn't see you needed it to be a single line statement and not a batch. I'll leave the above answer though in case it helps anyone else, and I'll also see if I can look up how to do it in a single statement.
I'm using this Windows application to batch rename a bunch of images. The application supports Regex, so I'm looking for an expression that will match everything (letters, numbers, hyphens, anything) before my file extension.
Thanks!
Not quite enough information given in the question, but this is probably what you want:
([^/\\]+)(\.[^/\\]+?)?
The first capture group will contain your file's basename and the second capture group will contain the extension, including the '.' character, if it exists.
You can reference the two capture groups in the 'Replace' section with $1 and $2.
I'm looking for a character to use a filename delimiter (I'm storing multiple filenames in a plaintext string). Windows seems not to allow :, ?, *, <, >, ", |, / and \ in filenames. Obviously, \ and / can't be used, since they mean something within a path. Is there any reason why any of those others shouldn't be used? I'm just thinking that, similar to / or \, those other disallowed characters may have special meaning that I shouldn't assume won't be in path names. Of those other 7 characters, are any definitely safe or definitely unsafe to use for this purpose?
The characters : and " are also used in paths. Colon is the drive unit delimiter, and quotation marks are used when spaces are part of a folder or file name.
The charactes * and ? are used as wildcards when searching for files.
The characters < and > are used for redirecting an application's input and output to and from a file.
The character | is used for piping output from one application into input of another application.
I would choose the pipe character for separating file names. It's not used in paths, and its shape has a natural separation quality to it.
An alternative could be to use XML in the string. There is a bit of overhead and some characters need encoding, but the advantage is that it can handle any characters and the format is self explanatory and well defined.
Windows uses the semicolon as a filename delimiter: ;. look at the PATH environment variable, it is filled with ; between path elements.
(Also, in Python, the os.path.pathsep returns ";", while it expands to ":" on Unix)
I have used * in the past. The reason for portability to Linux/Unix. True, technically it can be used on those fileysystems too. In practice, all common OSes use it as a wildcard, thus it's quite uncommon in filenames. Also, people are not surprised if programs do break when you put a * in a filename.
Why dont you use any character with ALT key combination like ‡ (Alt + 0135) as delimiter ?
It is actually possible to create files programmatically with every possible character except \. (At least, this was true at one time and it's possible that Windows has changed its policy since.) Naturally, files containing certain characters will be harder to work with than others.
What were you using to determine which characters Windows allows?
Update: The set of characters allowed by Windows is also be determined by the underlying filesystem, and other factors. There is a blog entry on MSDN that explains this in more detail.
If all you need is the appearance of a colon, and will be creating it programatically, why not make use of a UTF-8 character that just looks like a colon?
My first choice would be the Modifier Letter (U+A789), as it is a typical RTL character and appears a lot like a colon. It is what I use when I need a full DateTime in the filename, such as file_2017-05-04_16꞉45꞉22_clientNo.jpg
I would stay away from characters like the Hebrew Punctuation Sof Pasuq (U+05C3), as it is a LTR character and may mess with how a system aligns the file name itself.