3

My previous question was to extract multiple lines from a text files, which is working fine now but it still needs some improve to trim leading and trailing(tricky part) whitespaces/tab spaces and from searching thru Stackoverflow i found many answers and let me combine my final batch-script but from all putting it together it could be not that right.

Details.txt (source file)

line not needed, Copy and help with these command prompt:
line not needed:
(whitespace)(whitespace)some text not needed Copy "c:\.." a b c(white space)
line not needed:
line not needed, Copy and help with these command prompt:
(whitespace)Copy "d:\.." a c c(tab space)
line not needed
(tab space)(tab space)Copy "e:\.." a a c(whitespace)
line not needed

op.txt (Output file)

Copy "c:\.." a b c
Copy "d:\.." a c c
Copy "e:\.." a a c

Let's see the first batch-script. (without trim lead and trail spaces)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
  FIND /i "copy " <%source% |FIND "\" >op.txt
) ELSE (
  Exit
)

The combined script to trim lead and trail spaces which gives me two outputs(not big deal). But if we can shorten it both in output and code would be great.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
  FIND /i "copy " <%source% |FIND "\" >op.txt
  >op2.txt (
    FOR /f "delims=" %%A IN (op.txt) DO (
      SET "ln=%%A"
      SET str=!ln:*Copy=Copy!
      FOR /l %%b IN (1,1,31) DO (
        IF "!str:~-1!"==" " SET str=!str:~0,-1!
        IF "!str:~-1!"=="   " SET str=!str:~0,-1!
      )
      ECHO !str!
    )
  )
) ELSE (
  Exit
)

It actually works in my most situation of source files but I'm sure that this could be not well coded. So i would to ask to improve this batch-script in some ways like output or maybe a shorter/easier version.

Thanks, foxidrive for the start off and helping to solve at my first problem.

Updated version from David (Now with all output)

@ECHO OFF
SETLOCAL
SET "sourceFile=details.txt"
SET "outputFile=opDavid.txt"
@ECHO OFF > "%outputFile%"
IF EXIST "%sourceFile%" FOR /f "delims=" %%A IN ('FIND /i "copy " ^<%sourceFile% ^|FIND "\"') DO CALL :Trim %%A >>%outputFile%
ENDLOCAL
EXIT /b 0

:Trim
ECHO(%*
EXIT /b 0

Sorry for my English, it's poor like my coding

Community
  • 1
  • 1
Double Quotes
  • 125
  • 1
  • 2
  • 11

3 Answers3

5

This will do what you want, but with restrictions.

  • The following "poison" characters must not be in the string: | < > & ^ %

The Trim routine works by using the call command to trim any leading and trailing white spaces.

@ECHO OFF
SETLOCAL
SET "source=details.txt"
SET "output=op.txt"
@echo off > "%output%"
IF EXIST "%source%" FOR /f "delims=" %%A in ('FIND /i "copy " ^<%source% ^|FIND "\"') do call :Trim %%A >> "%output%"
ENDLOCAL
EXIT /b 0

:Trim
echo(%*
EXIT /b 0

Update to answer questions from the comments

  • Restriction Reasons:
    • | < > & will cause the script to crash unless inside quotations or escaped correctly with ^.
    • ^ will be removed from the string unless inside quotations because it is the escape char unless it itself is escaped.
    • % will be removed from the string even inside quotations because it is the variable char unless it itself is escaped %%.
    • Note that each of these character has to be escaped for each time cmd parses them which can become a major pain in the.
    • Batch Special Characters
  • There are other complicated situations which require an understanding of how cmd works. Read These Answers!

NOTE that most of these restrictions can be handled but it requires several more lines of code and then there might still be some restrictions. See My FindAndReplace.bat proof of concept for a look into what it would take.

The quotations surround the variable and value set "var=value" are there to provide special character protection without having to put quotations in the value itself.

  • My General Batch String Rule (And I believe the Batch Community Agrees): Avoid surrounding quotations within variable values and quote variables when used.

This prevents scenarios where a variable has quotations and is in quotations. This below will fail since the

set var="value with spaces"
if "%var%"="value with spaces"
:: this becomes
if ""value with spaces""="value with spaces"

ENDLOCAL closes the scope from the SETLOCAL command, therefore, cleaning up all the changes made to the environment. See This Reference

Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • This is awesome, the script how you shorten it down so short. I tested your script and it worked but it shows only the last line, so may i change from ">op.txt" to ">>op.txt" and add ">nul copy nul %outputFile%" to create a clean file on start and to get all needed lines. Please take a look on my updated script. And thank you again David. :) – Double Quotes Oct 10 '13 at 05:32
  • I have three question. May i ask what the restrictions are be that you mention? And could you please explain me about the SET "source=details.txt" why it has double quotes around source=details.txt instead of only the path or filename ? And does ENDLOCAL from *IF EXIST* just work like that instead of *ELSE* ? – Double Quotes Oct 10 '13 at 05:50
  • @DoubleQuotes I updated my answer to address your questions. Also, I added another example of how to null a file. `@echo off > "%output%"` – David Ruhmann Oct 10 '13 at 14:20
  • On process to follow up your answers now. Thank you very much, much appreciated. – Double Quotes Oct 11 '13 at 14:48
2

If you put two helper batch files in your path, then this command will work without needing to install any third party software:

type "source.txt" |repl "^[ \t]*" "" |repl "[ \t]*$" "" |findrepl "copy \x22" /i >op.txt

The first two repl commands remove leading and then trailing whitespace of space/tab and the findrepl command will filter lines that have copy " in them.

The helper batch file called repl.bat can be found here
The helper batch file called findrepl.bat can be found here

Put them both in the same folder as the batch file, or in c:\windows\system32 or another directory that is on the PATH statement.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Prefer to use without a helper batch for this time.(it's a very effective script what i have read, nice). Thanks foxidrive, much appreciated. – Double Quotes Oct 10 '13 at 05:55
1

just try sed for Windows:

sed "s/^[ \t]*//;s/[ \t]*$//" details.txt
Endoro
  • 37,015
  • 8
  • 50
  • 63