-1

I have file C:/test.txt which is having content as below.

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

I want to extract 'epfp' or any string comes at this place and convert this into upaercase also if its have test attached (as epfptest) then it it should split EPFP-TEST. For extracting I am running the below code and redirecting the output in temp1.txt file

findstr "Value=" C:\test.txt| findstr america > "C:\temp.txt" && for /l %l in (1,1,1) do @for /f "tokens=3* delims=." %a in ('findstr /n /r "^" "C:\temp.txt" ^| findstr /r "^%l:"') do @echo %b > c:\temp1.txt

now temp1.txt file having the content as below :

com\efpf_share\efpf\ipm_files

Now finally I am exracting efpf from below code it gives me the output as below :

for /f "tokens=3 delims=\" %a in (c:\temp1.txt) do @echo %a
epfp

I want this output or to be converted as EPFP (in uppercare) and if this output does not having test string attached then it should only split as EPFP-TEST

Note: Final output can be anything (in this case epfp) and I want this convert in uppercase also if this output containing 'test' string attached then that should be split in "STRING-TEST"

  • @Compo Sorry for the last question. I have edited my question and did a homework on the last one. Can you please help me now. – Abhishek Kumar Jul 30 '18 at 09:47
  • Please read [ask] and [Stack Overflow question checklist](https://meta.stackoverflow.com/q/260648) before revisiting your question and [editing it](https://stackoverflow.com/posts/51589568/edit) accordingly; thank you. – Compo Jul 30 '18 at 11:02
  • You were asked, half an hour before you edited your question, to provide before and after examples. The person who did so, @Mofi, has taken time out to help you, please be reasonable and follow their advice. – Compo Jul 30 '18 at 17:07
  • @Mofi epfp is my output final output data for which I want all conversion (uppercase, splitting the string if it contains test as STRING-TEST – Abhishek Kumar Jul 30 '18 at 17:25

1 Answers1

0

This test file modification task should be definitely not done using a batch file and pure Windows command processor commands. There are much better scripting languages for this task.

It would be also much more useful to do this file content modification with a powerful text editor like UltraEdit or any other text editor with Perl regular expression support. Searching for (\\[^\\]+\\)(?=ipm_files) and using as replace string \U$1\E changes the directory name left to ipm_files to upper case and searching for (?<!\\|-)TEST(?=\\ipm_files) and using as replace string -TEST inserts the hyphen character left to TEST if there is not already a hyphen and the entire folder name is not TEST.

However, here is a commented batch file solution for this task:

@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 must be two times replaced by real file name of data file with relative or absolute path.

The purpose of first FOR loop in main code at top is described in my answer on:
How to read and print contents of text file line by line?

The other command lines are explained by the remarks between main code and subroutine.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143