Windows批处理:敏感地替换文件中的字符串

时间:2017-11-17 10:58:20

标签: windows batch-file

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

虽然在Unix上,shell脚本很容易实现,但是我在Windows上花了好几天的时间......

我当前的批处理文件几乎可以完成我需要的所有工作,除了字符串替换不区分大小写......也就是说,它取代了" 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 (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 %1=!%1:%%L=%%L!
 goto :EOF

:tolower
 for %%L 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 %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

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

2 个答案:

答案 0 :(得分:2)

@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

也许这会有所帮助。

答案 1 :(得分:0)

说实话,保持纯批次的最快方法是欺骗系统并在后台使用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!

<强> RESULT

  

我想要这个大案例

     

我想要这个小写