Batch file 替换输出中的字符串

Batch file 替换输出中的字符串,batch-file,Batch File,我有文件C:/test.txt,其内容如下 05/13/2017 07:29:34 Value= \\america.com\efpf_share\efpf\ipm_files 05/13/2017 07:29:41 Value= \\america.com\efpf_share\efpf\ipm_files 05/17/2017 08:31:54 Value= \\america.com\efpf_share\efpf\ipm_fil

我有文件C:/test.txt,其内容如下

05/13/2017 07:29:34 Value=           \\america.com\efpf_share\efpf\ipm_files
05/13/2017 07:29:41 Value=           \\america.com\efpf_share\efpf\ipm_files
05/17/2017 08:31:54 Value=           \\america.com\efpf_share\efpf\ipm_files
05/17/2017 08:32:03 Value=           \\america.com\efpf_share\efpf\ipm_files
我想提取“epfp”或此处出现的任何字符串,并将其转换为upaercase,如果其附加了测试(作为epfptest),则它应该拆分epfp-test。为了提取,我运行下面的代码并重定向temp1.txt文件中的输出

现在,temp1.txt文件的内容如下:

com\efpf_share\efpf\ipm_files
for /f "tokens=3 delims=\" %a in (c:\temp1.txt) do @echo %a
epfp
现在,我最终从下面的代码中提取efpf,它给我的输出如下:

com\efpf_share\efpf\ipm_files
for /f "tokens=3 delims=\" %a in (c:\temp1.txt) do @echo %a
epfp
我希望将此输出或转换为EPFP(在uppercare中),如果此输出未附加test字符串,则应仅将其拆分为EPFP-test


注意:最终输出可以是任何内容(在本例中为epfp),如果此输出包含附加的“test”字符串,则我希望此转换为大写,然后应将其拆分为“string-test

此测试文件修改任务绝对不能使用批处理文件和纯Windows命令处理器命令来完成。对于这项任务,有更好的脚本语言

使用功能强大的文本编辑器(如UltraEdit)或任何其他支持Perl正则表达式的文本编辑器修改文件内容也会更有用。搜索
(\\[^\\]+\\)(?=ipm\U文件)
并将其用作替换字符串
\U$1\E
ipm\U文件的左目录名更改为大写并搜索
(?如果还没有连字符且整个文件夹名称不是
TEST
,则使用as replace string
-TEST
将连字符插入
TEST

但是,以下是此任务的注释批处理文件解决方案:

@echo off
if not exist "%~dp0Test.txt" goto :EOF
setlocal EnableExtensions DisableDelayedExpansion

set "Modified=0"
set "DataFile=%~dp0Test.txt"
set "TempFile=%TEMP%\%~n0.tmp"
del "%TempFile%" 2>nul

for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%DataFile%" 2^>nul') do (
    set "Line=%%I"
    call :ProcessLine
)

if %Modified% == 1 move /Y "%TempFile%" "%DataFile%"
del "%TempFile%" 2>nul

endlocal
goto :EOF

rem The subroutine ProcessLine removes first line number and colon inserted
rem by FINDSTR at beginning of each line to process correct also empty lines
rem in data file. The subroutine jumps to output of line in case of current
rem line is an empty line.

rem Next the line is split up into substrings using backslash as delimiter.
rem Of interest are only the fourth and fifth substrings. The fifth substring
rem should be ipm_files to identify the current line as a line to process.
rem A jump to writing the line into temporary file is done if this condition
rem is not true. Otherwise the fourth substring is assigned to a variable
rem because that string is the folder name to modify by this batch file.

rem Each ASCII character in the folder name is replaced by its upper case character.

rem If the entire new folder name is TEST, just do the replace and don't change
rem the folder name to -TEST. If the new folder name ends already with -TEST,
rem just do the replace. But if new folder name ends with only TEST, replace
rem just TEST by -TEST with hyphen.

rem A case-sensitive comparison of current and new folder name is done before
rem running the folder replace on line to determine if the replace is really
rem necessary at all. The modification information is saved in an environment
rem variable which is passed over local environment of subroutine to main code
rem above. This information is used finally to determine if the data file must
rem be replaced at all by the temporary file because of a modification is made
rem or the temporary file can be simply deleted as being equal with data file.

:ProcessLine
setlocal EnableDelayedExpansion
set "Line=!Line:*:=!"
if not defined Line goto WriteLine

for /F "tokens=4,5 delims=\" %%A in ("!Line!") do (
    if /I not "%%B" == "ipm_files" goto WriteLine
    set "CurFolderName=%%A"
)

set "NewFolderName=%CurFolderName%"
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 "NewFolderName=!NewFolderName:%%C=%%C!"

if "%NewFolderName%" == "TEST" goto DoReplace
if "%NewFolderName:~-5%" == "-TEST" goto DoReplace
if "%NewFolderName:~-4%" == "TEST" set "NewFolderName=%NewFolderName:~0,-4%-TEST"

:DoReplace
if "%CurFolderName%" == "%NewFolderName%" goto WriteLine
set "Modified=1"
set "Line=!Line:%CurFolderName%\ipm_files=%NewFolderName%\ipm_files!"

:WriteLine
echo(!Line!>>"%TempFile%"
endlocal & set "Modified=%Modified%"
goto :EOF
%~dp0Test.txt
必须被具有相对或绝对路径的数据文件的真实文件名替换两次

顶部主代码中第一个FOR循环的目的在我的回答中描述:

其他命令行由主代码和子例程之间的注释解释

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • echo/?
  • endlocal/?
  • findstr/?
  • goto/?
  • 如果/?
  • 移动/?
  • rem/?
  • setlocal/?

您的任务没有得到很好的解释,也不清楚,这不是一项免费的代码编写服务。如果您提供代码并提出与此相关的特定问题,我们将帮助您解决代码中的特定可复制问题。您对脚本语言的理解水平并不排除您编写自己的代码,请根据ly.@COMO对最后一个问题表示抱歉。我已经编辑了我的问题,并在最后一个问题上做了家庭作业。你现在能帮我吗?请在重温你的问题之前阅读和相应地;谢谢你。在编辑你的问题之前半小时,有人要求你提供前后示例。这样做的人,@Mofi,已经带走了蒂姆为了帮助您,请通情达理并听从他们的建议。@Mofi epfp是我的输出最终输出数据,我希望对其进行所有转换(大写,如果字符串包含test,则拆分为string-test)