Windows wildcard rename files


















The Microsoft technet XP online help isn't much better. Here is all it has to say regarding wildcards:. If you use wildcards in filename2, the characters represented by the wildcards will be identical to the corresponding characters in filename1.

I've managed to successfully use wildcards in the filename2 parameter on some occasions, but it has always been trial and error. I haven't been able to anticipate what works and what doesn't. Frequently I've had to resort to writing a small batch script with a FOR loop that parses each name so that I can build each new name as needed. Not very convenient. Of course knowing the rules would also benefit batch development. Yes - this is a case where I am posting a paired question and answer.

I got tired of not knowing the rules and decided to experiment on my own. I figure many others may be interested in what I discovered. These rules were discovered after extensive testing on a Vista machine.

No tests were done with unicode in file names. The behavior of the wildcards changes slightly between source and target masks. Note - REN can be used to rename a folder, but wildcards are not allowed in either the sourceMask or targetMask when renaming a folder. If the sourceMask matches at least one file, then the file s will be renamed and folders will be ignored.

If the sourceMask matches only folders and not files, then a syntax error is generated if wildcards appear in source or target. If the sourceMask does not match anything, then a "file not found" error results. Also, when renaming files, wildcards are only allowed in the file name portion of the sourceMask. Wildcards are not allowed in the path leading up to the file name. The sourceMask works as a filter to determine which files are renamed.

The wildcards work here the same as with any other command that filters file names. This wildcard is greedy - it always consumes the next character if it is not a. However it will match nothing without failure if at name end or if the next character is a. This wildcard is not greedy. It will match as little or as much as is needed to enable subsequent characters to match. Note - a valid Windows name cannot end with. The terminating.

The above rules are not that complex. But there is one more very important rule that makes the situation confusing: The sourceMask is compared against both the long name and the short 8. This last rule can make interpretation of the results very tricky, because it is not always obvious when the mask is matching via the short name. It is possible to use RegEdit to disable the generation of short 8. Any short names that were generated before disabling short names will remain.

Note - I haven't done any rigorous testing, but it appears these same rules also work for the target name of the COPY commmand. The targetMask specifies the new name. It is always applied to the full long name; The targetMask is never applied to the short 8. The presence or absence of wildcards in the sourceMask has no impact on how wildcards are processed in the targetMask. The targetMask is processed against the source name strictly from left to right with no back-tracking. Replaces the character that was in source with c , but never replaces.

If the next character is. If already at the end of source, then does nothing. If c is not found, then all remaining characters from source are appended, followed by c This is the only situation I am aware of where Windows file pattern matching is case sensitive. If already at end of source then does nothing. After the targetMask has been exhausted, any trailing. Substitute a character in the 1st and 3rd positions prior to any extension adds a 2nd or 3rd character if it doesn't exist yet.

Remove any extra extension after the initial extension. Note that adequate? Obviously could add an additional? Any name can be broken up into components that are delimited by. Characters may only be appended to or deleted from the end of each component. Characters cannot be deleted from or added to the beginning or middle of a component while preserving the remainder with wildcards.

Substitutions are allowed anywhere. If short names are enabled, then a sourceMask with at least 8? One slash is required for each character to be deleted. I've confirmed the behavior on a Windows 10 machine. So the technique cannot be used to remove a prefix that contains.

For example:. This technique only works if both the source and target masks are enclosed in double quotes. Reasonably easy in PowerShell. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet. This answer assumes your files are always of the form: [place] -stop-and-search- [year]. So for example, for "dallas-stop-and-search Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. Asked 3 years, 9 months ago. Active 3 years, 9 months ago. Viewed 2k times. I have a bunch of files in a folder, something like: file1-regular-exp-suffix1 file2-regular-exp-suffix Does anyone know how to do it? Improve this question. How do we transform it according to the destination pattern?

Simple: Stack the original file name and the destination pattern on top of each other. For each character of the output, take the corresponding character from the destination pattern, unless it is a question mark, in which case you take the corresponding character from the original file name. One way of thinking of this is that you treat the destination pattern as a stencil, with holes punched out where the question marks are.

You then overlay the stencil on top of the original file name. The characters from the original file name show through the holes, and what you see is the result. The asterisk which parses into question marks just copies the existing file name, which includes the FRED. Suppose there is a file called FRED After copying WILMA to the result, we reach the first question mark in the destination, which lines up with the second question mark in the source, not the first.



0コメント

  • 1000 / 1000