| 1. |
Solve : Search for String in file and remove it? |
|
Answer» Hi i dont use REM to comment, i use ::. Using a broken label as comment starter in a loop or other parenthetical structure will break it. however... Code: [Select]@echo off % This is a comment % echo hello world for %%A in (a b c d e) do ( % This is a comment % echo %%A ) Quote from: Dias de verano on April 03, 2009, 04:50:07 AM Using a broken label as comment starter in a loop or other parenthetical structure will break it.here is a good explanation on double colon :: or using REM as comment: http://www.robvanderwoude.com/comments.php it seems to be common practice in batch to use ::., and i personally prefer :: double colon, because it makes my code more readable. ok, not really, in fact, i rarely put comment on my batch code. now, here is another case, which one would be executed faster: 1. for /f "tokens=*" - default delims is {TAB}{SPACE} 2. for /f "delims=" - default token is 1 since microsoft don't publish his source code, it looks like no.2 will be faster by a split nano-second because it don't do parsing operation. but my preference is no.1, because it seems code is more readable using no.1 unless microsoft pseudocode is: Code: [Select]s=readline if tokens=* or delims is empty then //do nothing else //do parsing on string s end if well, in batch, there is no need to follow the rule of correct syntax. here is an example to count number of words, with limitation Code: [Select]set s=sample test string set/a n=1+.%s: =&set/a n+=1+.%% echo words=%n%Quote from: Reno on April 04, 2009, 07:38:59 AM here is a good explanation on double colon :: or using REM as comment: Yes, I am very familiar both with Rob's pages and with the arguments for and against using :: as a comment marker. Quote it seems to be common practice in batch to use :: A bad practice in my opinion, (but only my opinion!) Quote ., and i personally prefer :: double colon, see above. Quote because it makes my code more readable. That is, again, a matter of opinion. batch code has different readability requirements compared to (e.g.) poetry. Quote ok, not really, in fact, i rarely put comment on my batch code. Like many sloppy programmers? Quote now, here is another case, which one would be executed faster: Doesn't matter. Nobody codes in batch for speed. Quote code is more readable using no.1 Code is more readable (to the coder) if it contains that coder's preferences, then (a circular argument)? Quote well, in batch, there is no need to follow the rule of correct syntax. So you say Quote here is an example to count number of words, with limitation That is a hack! When writing your own batch code, comment style and obeying rules of syntax are of course optional. However this is a forum where we give guidance to new and/or confused users, and in that situation, in my opinion, clarity and ease of understanding are more important than clever coding tricks. for comments, I prefer /*...*/ blocks. but every time I try and use it, my batch breaks! so I change it back to Code: [Select]REM now, use xcopy to copy the favourite goat pictures to a backup disk. xcopy "..\Pics\Stay out\Hey, I said stay out\OK, seriously, how many times do I have to tell you\ok fine, (wrapped) COME on in\well there goes my reverse psycology idea\more goat pics\Bessie" I:\BACKUPS\BESSIE /S I just counted my BCFile project, and it contains 9,416 (72%) Code lines, and 3,518 (27%) comment lines. and as for esoterics and readability, I'm probably at my worst in this section with string manip. I've become so practices with the VB string manipulation functions, I simply nest a bunch of them on one line,- a sample dug out from deep within my BASeParser evaluator LIBRARY: Code: [Select] SplMake(CurrArgument) = Mid$(FromString, ArgStart, (CurrPos - ArgStart) + 1) If right$(SplMake(CurrArgument), 1) = ARGUMENTSEP Then SplMake(CurrArgument) = Mid$(SplMake(CurrArgument), 1, Len(SplMake(CurrArgument)) - 1) End If Quote from: BC_Programmer on April 04, 2009, 10:52:46 AM I've become so practices with the VB string manipulation functions, I simply nest a bunch of them on one line I do this a lot in VB6, VBScript, QB etc. QB is a pain. I keep trying to use TRIM$... but there isn't one. so I end up doing LTRIM$(RTRIM$()) which is slightly longer. I just can't stand that technically VB is working with an immutable string and always creates a completely new string...I don't actually use QB any more. That Ltrim(Rtrim(string)) thing was the first thing that popped into my head when I read your remark about nesting string functions all on one line. you use Freebasic. I would too, but I managed to hack pretty good console support into Visual Basic, which would likely have been my only reason to use FreeBASIC.Code: [Select]@echo off set '=REM %'% this is a comment echo hello world Quote from: Dias de verano on April 04, 2009, 01:37:00 PM Code: [Select]@echo offhahaha, nice one, almost got me. C:\>set ' '=REM Quote from: Dias de verano on April 04, 2009, 09:17:07 AM That is, again, a matter of opinion. batch code has different readability requirements compared to (e.g.) poetry.yes i am one of them , i dont have the time to write any comment about the code, just too busy thinking more trick to cramp multiple lines of codes into one line. and i always like one liner code which do the same job as the for example 10 lines of if else if else for loop. only in batch though, not so easy to do with other programming language, such as vb, vbs, etc. Quote from: BC_Programmer on April 04, 2009, 11:37:58 AM I would too, but I managed to hack pretty good console support into Visual Basic, which would likely have been my only reason to use FreeBASIC.BC, how do you implement console support into VB aps? do you use reference to scrrun.dll Scripting.FileSystemObject, or using GetStdHandle API then later use link.exe or something else?Quote from: Reno on April 05, 2009, 01:13:38 AM hahaha, nice one, almost got me. Huh? Code: [Select]S:\Test\Batch>type remtest.bat @echo off set '=REM %'% this is a comment echo hello world S:\Test\Batch>remtest.bat hello worldQuote from: Dias de verano on April 05, 2009, 02:38:24 AM Huh?at first, i though you use vb commenting style, wait, how that's going to work? then i realize you just create a variable ' with value REM. lol. Quote from: Reno on April 05, 2009, 03:03:23 AM i realize you just create a variable ' with value REM. lol. Good old cmd.exe runtime variable expansion Code: [Select] @echo off set gosub=call set return=goto :eof echo in main %gosub% :sub1 goto :end :sub1 echo in subroutine %return% :end Quote from: Reno on April 05, 2009, 01:13:38 AM only in batch though, not so easy to do with other programming language, such as vb, vbs, etc.Don't be silly. Take this, for instance: Code: [Select]Public Function InStrCount01( _ String1 As String, _ String2 As String, _ Optional ByVal Start As Long = 1, _ Optional Compare As VbCompareMethod = vbBinaryCompare) As Long Dim lenFind As Long lenFind = Len(String2) If lenFind Then ' silently correct illegal Start value If Start < 1 Then Start = 1 End If Do Start = InStr(Start, String1, String2, Compare) If Start Then InStrCount01 = InStrCount01 + 1 Start = Start + lenFind Else Exit Function End If Loop End If End Function I say BAH to that procedure, This one is much better: Code: [Select]Public Function InstrCount(ByVal InString As String, ByVal StrFind As String, Optional ByVal Start As Long = 1, Optional ByVal compare As VbCompareMethod = vbBinaryCompare) As Long If StrFind = "" Then InstrCount = 0 Else InstrCount = (Len(InString) - Len(Replace$(InString, StrFind, "", Start, , compare))) / Len(StrFind) End If End Function Which ties in with the whole bunch of string functions/operations one line thing. Quote BC, how do you implement console support into VB aps? It's a secret ... sort of. But I'll share. Essentially the second method, using WriteFile,ReadFile on the Handles returned by GetStdHandle. It's wrapped in a neat module that I can drop into each Console program. I then run this VBScript, which runs my LINK.EXE (as you've said) to switch the executables subsystem: Code: [Select] Option Explicit Dim strLINK, strEXE, WSHShell ' Be sure to set up strLINK to match your VB6 INSTALLATION. strLINK = """D:\Programs\Microsoft Visual Studio\VB98\LINK.EXE""" strEXE = """" & WScript.Arguments(0) & """" Set WSHShell = CreateObject("WScript.Shell") WSHShell.Run strLINK & " /EDIT /SUBSYSTEM:CONSOLE " & strEXE Set WSHShell = Nothing WScript.Echo "Complete!" I normally only use the scrrun FSO for VBScript's designed for others- I've been trying to give my BCFile library some exercise. Main benefit being that it can show the explorer menu for any file/folder- as well as including a FileSearch and FileSearchEx classes. |
|