質問

I'm cleaning up thousands of filenames, most have a structure like: "xxxx xxxx - xxxxx xxxx xxxx.yyy" The problem is that some are capitalized and some are not. (Actually there are other problems too, but I can fix those easily.)

I would like to take a string of any characters, and capitalize all of it's words before any period.
Preferably using a single line of code, using pipes, rather than using files, like you can do with FIND.
Lastly I would prefer to just capitalize the entire string at once, but I'm not opposed to handling each word separately.

Example:

Input:  "this - string IS an EXAMPLE 4 u"
Output: "This - String IS An EXAMPLE 4 U"

I want to ignore all upper-case and non-alphabetic characters (dashes, underscores, numbers, etc), just pass them through unaffected.

So I would like to be able to use code like this (the 2nd line is the relevant one):

for /f "tokens=*" %%x in ('dir /b *.zzz') do ( 
    for /f "tokens=*" %%y in ('echo %%x ^| findstr /r "s/\s\w/\U$&/g;"') do (
        rename "%%x" "%%y" ))

If possible, I would like to be able to test it on the command-line like this:

echo hello to you | findstr /r "s/\s\w/\U$&/g;"

My problem is that the above example does not provide any output.

As you can see, I have searched for examples of regular expressions, but can't get them to plug-and-play with findstr.

I'd rather not capitalize the file extention, but this is easily fixed, so I don't really care.

Please no sed, grep, or any other 3rd party software suggestions, though I would be open to other MS-based solutions (something that comes with Enterprise or Ultimate or downloadable from Microsoft).

Thanks in advance!

役に立ちましたか?

解決

FINDSTR support for regex is extremely limited. Type HELP FINDSTR or FINDSTR /? from the command line and you will see what I mean. To make matters worse, the regex features that are supported are implemented in a non-standard way. Have a look at What are the undocumented features and limitations of the Windows FINDSTR command? - regex limitations are described toward the bottom of the answer. The end result is FINDSTR will not be of any help in your case (no pun intended).

I believe it should be fairly easy to do what you want with PowerShell, but I haven't learned that language as of yet. You could also use VBScript or JScript. All three have robust regex support.

It is not difficult to implement a batch solution without using regex.

If you have a FOR variable %%F that contains a file name (possibly with path info), then %%~nF will give you just the name of the file, without extension. %%~xF will give you the extension of the file. Type HELP FOR from command line to get full documentation.

There is no built in command to convert a string to upper case, but it is trivial to implement using another FOR loop. The SET command supports search and replace, and the search portion is case insensitive. So it just needs a search and replace for each letter.

EDIT
I've fixed the code to only capitalize the first letter of each word as was requested. The code only recognizes space as a word boundry. You will have to add an additional search and replace op for each character that you want to support as a word boundry.

@echo off
setlocal enableDelayedExpansion
for %%F in (*.yyy) do (
  set "name= %%~nF"
  for %%C in (
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
  ) do set "name=!name: %%C= %%C!"
  set "name=!name:~1!"
  echo ren "%%F" "!name!%%~xF"
)

The above works great except for one potential problem: It will corrupt any filename that contains the ! character because delayed expansion is enabled when %%F is expanded. The fix is to toggle delayed expansion on and off within the loop.

@echo off
setlocal disableDelayedExpansion
for %%F in (*.yyy) do (
  set "original=%%F"
  set "name= %%~nF"
  set "ext=%%~xF"
  setlocal enableDelayedExpansion
  for %%C in (
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
  ) do set "name=!name: %%C= %%C!"
  set "name=!name:~1!"
  echo ren "!original!" "!name!!ext!"
  endlocal
)

Both scripts above will simply ECHO the rename command that would execute. Simply remove the ECHO that precedes REN and the script will actually rename your files.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top