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