Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
jwoegerbauer
- Posts: 33
- Joined: 01 Jan 2013 12:09
#1
Post
by jwoegerbauer » 22 Feb 2015 10:49
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
Last edited by
Squashman on 22 Feb 2015 12:09, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#3
Post
by Compo » 22 Feb 2015 11:56
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%
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 22 Feb 2015 12:10
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
)
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#5
Post
by Compo » 22 Feb 2015 12:28
@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
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 22 Feb 2015 12:59
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?
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#7
Post
by Compo » 22 Feb 2015 13:40
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)
-
jwoegerbauer
- Posts: 33
- Joined: 01 Jan 2013 12:09
#8
Post
by jwoegerbauer » 22 Feb 2015 14:52
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
Last edited by
Squashman on 22 Feb 2015 17:52, edited 1 time in total.
Reason: MOD EDIT: please use code tags
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#9
Post
by Compo » 22 Feb 2015 14:56
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)
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#10
Post
by foxidrive » 22 Feb 2015 15:45
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.
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#11
Post
by Compo » 22 Feb 2015 16:59
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.
-
jwoegerbauer
- Posts: 33
- Joined: 01 Jan 2013 12:09
#12
Post
by jwoegerbauer » 22 Feb 2015 23:36
@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.
Last edited by
Squashman on 23 Feb 2015 07:23, edited 1 time in total.
Reason: MOD EDIT: Third time I have asked you to use code tags.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#13
Post
by foxidrive » 23 Feb 2015 02:17
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.