Windows批处理:在文件大小写敏感的情况下replacestring

我花了很多时间编写脚本来使用“模板”(编码环境)项目生成新文件。

而在Unix上,shell脚本非常容易,我花了好几天在Windows上做同样的事情。

我目前的batch file几乎做我需要的一切,除了stringreplace是不区分大小写的…也就是说,它replace“EMPTYPROJECT”的“emptyproject”,是第一个语句…

码:

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION set argc=0 for %%x in (%*) do set /A argc+=1 if /I "%argc%" EQU "0" ( echo Usage: %0 PROJECT_NAME >&2 exit /B 1 ) set curr_dir=%~dp0 set project_name=%1 set project_name_lower=%1 set project_name_upper=%1 set project_file="vstudio\projects\%project_name%.vcxproj" set project_filters="vstudio\projects\%project_name%.vcxproj.filters" call :toupper project_name_upper call :tolower project_name_lower if not exist %project_file% ( copy /y "vstudio\projects\EmptyProject.vcxproj" %project_file% >nul copy /y "vstudio\projects\EmptyProject.vcxproj.filters" %project_filters% >nul ) set project_source_dir=..\projects\source\%project_name_lower% if not exist %project_source_dir% ( mkdir %project_source_dir% call :copy_and_replace_strings "..\projects\source\emptyproject\main.cpp" "%project_source_dir%\main.cpp" %project_name% %project_name_lower% %project_name_upper% call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject.h" "%project_source_dir%\%project_name_lower%.h" %project_name% %project_name_lower% %project_name_upper% call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject_main.cpp" "%project_source_dir%\%project_name_lower%_main.cpp" %project_name% %project_name_lower% %project_name_upper% call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject_eventhandlers.cpp" "%project_source_dir%\%project_name_lower%_eventhandlers.cpp" %project_name% %project_name_lower% %project_name_upper% ) endlocal goto :EOF REM functions :toupper for %%L IN (ABCDEFGHIJKLMNOPQRSTU VWXYZ) DO SET %1=!%1:%%L=%%L! goto :EOF :tolower for %%L IN (abcdefghijklmnopqrstu vwxyz) DO SET %1=!%1:%%L=%%L! goto :EOF :copy_and_replace_strings <source_file> <target_file> <name> <lower> <upper> for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< %1') do ( set token1=%%a set token2=%%b if defined token2 ( REM not case sensitive set token2=!token2:EMPTYPROJECT=%5! set token2=!token2:EmptyProject=%3! set token2=!token2:emptyproject=%4! echo !token2!>>%2 ) else ( echo[>>%2 ) ) goto :EOF 

我在这个论坛上阅读了很多关于这方面的内容,但找不到合适的解决scheme。 有人可以帮我吗?

 @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION FOR %%o IN (EMPTYPROJECT EmptyProject emptyproject EmPtYpRoJeCt) DO ( SET "token2=A line containing %%o in it" CALL :magic ECHO !token2! -^> !result! ) GOTO :EOF :magic SET "result=!token2!" ECHO !token2!|FINDSTR "EMPTYPROJECT" >NUL IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=capiTaLs!" ECHO !token2!|FINDSTR "EmptyProject" >NUL IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=Mixed!" ECHO !token2!|FINDSTR "emptyproject" >NUL IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=lower!" GOTO :eof 

也许这将是帮助。

说实话,保持纯批量的最快方法就是欺骗系统,并在后台使用powershell

 @echo off setlocal enabledelayedexpansion set "input=i want this upper case" for /f "usebackq delims=" %%a in (`powershell "\"%input%\".toUpper()"`) do set "lower=%%~a" set "input=I WANT THIS LOWER CASE" for /f "usebackq delims=" %%a in (`powershell "\"%input%\".toLower()"`) do set "upper=%%~a" echo !lower! echo !upper! 

结果

我想要这个上面的例子

我想要这个小写