Page 1 of 1
Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 10:49
by jwoegerbauer
Hi there,
with these lines of a batch file I create a text file where I am interested only in the left trimmed contents of its last line: the last line I get with MORE +!offset! <%fn% which works as expected. But the red tinted line (which should do the trimming) doesn't work!
Code: Select all
SET "must_defrag =0"
SET "fn=%work_root%\defrag-analysis-report.txt"
Defrag %backup_disk% -a > %fn%"
IF EXIST %fn% (
FOR /f "delims=" %%a IN ('find /C /V "" ^<%fn%') DO (SET /A offset=%%a -1)
rem left trim last line of text file
[color=#FF0000]FOR /f "tokens=* delims= " %%a IN ('MORE +!offset! <%fn%') DO SET defrag_analysis_result=%%a[/color]
IF "%locale%"=="ENGLISH" (
SET "search=You do not need to defragment this volume."
) ELSE (
SET "search=Dieses Volume muss nicht defragmentiert werden."
)
CALL SET "test=%%defrag_analysis_result:%search%=%%"
IF "%test%"=="%defrag_analysis_result%" (
SET "must_defrag =0"
) ELSE (
SET "must_defrag =1"
)
etc,pp
Any hints welcome! TIA
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 11:37
by ShadowThief
Left trim?
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 11:56
by Compo
jwoegerbauer wrote:FOR /f "tokens=* delims= " %%a IN ('MORE +!offset! <%fn%') DO SET defrag_analysis_result=%%a
What happens if you escape the input character?
^<%fn%
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 12:10
by Squashman
Also looks like you have unbalanced parentheses. You don't have a closing parentheses for the IF EXIST.
I also don't see where you are enabling delayed expansion. You are inside a code block so you need to use delayed expanions with your last comparision.
Indentation also helps with readability.
Code: Select all
SET "must_defrag =0"
SET "fn=%work_root%\defrag-analysis-report.txt"
Defrag %backup_disk% -a > %fn%"
IF EXIST %fn% (
FOR /f "delims=" %%a IN ('find /C /V "" ^<%fn%') DO (SET /A offset=%%a -1)
rem left trim last line of text file.
rem You need to escape the redirection.
FOR /f "tokens=* delims= " %%a IN ('MORE +!offset! ^<%fn%') DO SET defrag_analysis_result=%%a
IF "%locale%"=="ENGLISH" (
SET "search=You do not need to defragment this volume."
) ELSE (
SET "search=Dieses Volume muss nicht defragmentiert werden."
)
CALL SET "test=%%defrag_analysis_result:%search%=%%"
REM You are inside the IF EXIST code block. You need to use Delayed Expansion for the next comparision
IF "%test%"=="%defrag_analysis_result%" (
SET "must_defrag =0"
) ELSE (
SET "must_defrag =1"
)
REM added closing parentheses
)
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 12:28
by Compo
@Squashman, I've a feeling that they've just given us a section of the code, hence the code you think they're missing.
However what isn't necessary in the line they highlighted is to state a delimiter whilst at the same time asking for all tokens:
Code: Select all
FOR /f "tokens=*" %%a IN ('MORE +!offset! ^<%fn%') DO SET defrag_analysis_result=%%a
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 12:59
by Squashman
Compo wrote:@Squashman, I've a feeling that they've just given us a section of the code, hence the code you think they're missing.
However what isn't necessary in the line they highlighted is to state a delimiter whilst at the same time asking for all tokens
Isn't that what is left trimming string?
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 13:40
by Compo
To 'left trim' the last line, (effectively getting the last space delimited token):
Code: Select all
FOR /f %%a IN ('find /C /V "" ^<"%fn%"') DO SET/A offset=%%a-1
FOR /f "tokens=*" %%a IN ('MORE +!offset! ^<"%fn%"') DO (
FOR %%b in (%%a) DO SET defrag_analysis_result=%%b)
Or optionally:
Code: Select all
FOR /f "tokens=*" %%a IN ('findstr . "%fn%"') DO FOR %%b in (%%a) DO (
SET defrag_analysis_result=%%b)
Then:
Code: Select all
IF NOT [%defrag_analysis_result%]==[volume.] (
IF NOT [%defrag_analysis_result%]==[werden.] SET must_defrag=1) ELSE (
SET must_defrag=1)
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 14:52
by jwoegerbauer
Thanks for your inputs, guys.
Because the file to be read only consists of 20 lines, and the problem I have to solve is not of type "time-critical", in between I decided to use FINDSTR to search for the string needed
Code: Select all
IF EXIST %defrag_analysis_report% (
IF "%locale%"=="ENGLISH" (
SET "search=You do not need to defragment this volume."
) ELSE (
SET "search=Dieses Volume muss nicht defragmentiert werden."
)
FINDSTR /m "%search%" %defrag_analysis_report% > NUL 2>&1
IF NOT %ERRORLEVEL%==0 SET "must_defrag=1"
DEL "%defrag_analysis_report%" > NUL 2>&1
SET "search="
)
and it works well.
I'll come back here to report whether @Compo's solution works, ASAP
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 14:56
by Compo
Here's a direct replacement for exactly what you posted in the opening message.
Code: Select all
SET "fn=%work_root%\defrag-analysis-report.txt"
Defrag /a %backup_disk%>"%fn%"
IF NOT EXIST "%fn%" EXIT/b
FOR /f "tokens=*" %%a IN ('findstr . "%fn%"') DO FOR %%b in (%%a) DO (
SET defrag_analysis_result=%%b)
SET "must_defrag=0"
IF NOT [%defrag_analysis_result%]==[volume.] (
IF NOT [%defrag_analysis_result%]==[werden.] SET must_defrag=1) ELSE (
SET must_defrag=1)
however, if all you wanted to know was if the string ended in volume. or werden. then you could probably replace everything in your opening post with this
Code: Select all
Defrag /a %backup_disk%|FindStr/c:"werden." /c:"volume.">Nul&&(
Set must_defrag=0)||(Set must_defrag=1)
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 15:45
by foxidrive
Left trimming normally means removing the whitespace on the left hand side of a string. That's spaces and tabs.
jwoegerbauer, do you want to do something different?
There are no examples of what you are attempting to do.
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 16:59
by Compo
I agree with your assessment of left trimming, foxidrive, however based upon the perceived intent, it was only necessary to check the last word of the defrag output, hence my examples.
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 22 Feb 2015 23:36
by jwoegerbauer
@Compo,
this works:
Code: Select all
Defrag %backup_disk% -a | FINDSTR /c:"werden." /c:"volume.">NUL && (
SET must_defrag=[color=#FF0000]1[/color])||(SET must_defrag=[color=#FF0000]0[/color])
Thanks again for this most elegant solution!
Have a nice day.
Re: Left trim a string got with 'MORE +!offset! <%fn%'
Posted: 23 Feb 2015 02:17
by foxidrive
Compo wrote:I agree with your assessment of left trimming, foxidrive, however based upon the perceived intent, it was only necessary to check the last word of the defrag output, hence my examples.
You're helpful and that's a great trait compo.
I'm just getting old and cranky. People ask a question, post code that doesn't work, don't describe what they are after.
I no longer even look at the code if it's broken,and especially if there is no description.
If it's not well described then I'm more likely to waste my time these days trying to coax the question and data out of the people.