3

I have a batch file that I am trying to get working and I'm having trouble suppressing the output of a couple of commands. One solution I've tried is to start the commands with the @ symbol - (something I've done sucessfully plenty of times). Here is the code:

@echo off
setlocal
for /f "tokens=*" %%i in (tmp\hdata.txt) do (

    ::' just a note - hdata.txt contains lines of text such as,
    ::' for example:
    ::' jquery_javascript_32
    ::' underscore_javascript_9
    ::' I couldn't do this with simple delimiters because some lines are like:
    ::' underscore_js_javascript_43

    set "id=%%i"
    set filewithnum=!id:_javascript_=!

     ::' *** BELOW throws error ***
     @echo !filewithnum!|findstr /R "[0-9]$" && set "file=!filewithnum:~,-1!" >nul 2>&1

     ::' *** BELOW throws error ***
     @echo !filewithnum!|findstr /R "[0-9]$" && set "file=!file:~,-1!" >nul 2>&1

    echo !file!

)

endlocal
exit /b    

The lines commented above throw: '@echo' is not recognized as an internal or external command, operable program or batch file.

Seems weird.

Any ideas as to what's happening here?


Note: The extra ' after the comment :: above is to make syntax highlighting work properly on stackoverflow.

dgo
  • 3,877
  • 5
  • 34
  • 47
  • 3
    two obvious issues are that you've used `setlocal` but not `setlocal enabledelayedexpansion`, so the `!var!` syntax will not work. The second is that `::` is a broken label, and labels are not allowed within a block - replace with `rem ` – Magoo Dec 21 '16 at 14:06
  • @Magoo - I have `enabledelayedexpansion` enabled in the registry, so I don't need it. That's good to know about `::`. I didn't know that; but in this case, this isn't the issue, as the comments have only been added to `Stackoverflow`, and are not included in the original file. – dgo Dec 21 '16 at 14:19
  • 1
    [**Never** use `:label` nor `:: label-like comment` inside a command block enclosed in `()` parentheses](http://stackoverflow.com/a/32147995/3439404) – JosefZ Dec 22 '16 at 08:42

1 Answers1

3

Once you've fixed the points Magoo raised in the comments, you need to suppress the output of findstr. You don't need @ since command echo mode is already turned off at the start of the script.

You've got this:

@echo !filewithnum!|findstr /R "[0-9]$" && set "file=!filewithnum:~,-1!" >nul 2>&1

So you are redirecting the output of the set command! To redirect the output of findstr, do this:

echo !filewithnum!|findstr /R "[0-9]$" >nul 2>&1 && set "file=!filewithnum:~,-1!"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • This answers the question I really wanted the answer to, so thanks. I will accept it as the answer if you can tell me this: Is the double `@` syntax what is causing the error? You get the upvote for sure, but I do want to know why the error is getting thrown. – dgo Dec 21 '16 at 14:22
  • 2
    @user1167442 I think the reason you get that specific error is one of those undocumented cmd.exe mysteries, but you'll find that if you replace the `::`-style comments with `rem`, your problem will go away. `::` was never meant to be used as a comment, so if you are using an undocumented feature, any behaviour is permitted. – Klitos Kyriacou Dec 21 '16 at 14:32
  • 5
    You can use `::` as comment, even inside command blocks, BUT you should know exactly the behaviour [In parenthesis lables are "two line" oriented](http://stackoverflow.com/a/4006006/463115). The secondary line after `:label` will not detect `@` as special character, instead it tries to execute a file named `@echo.bat` – jeb Dec 21 '16 at 14:43
  • 2
    Another way to do it, which is 100% pure "batch": `for /L %i in (0 1 9) do if "!filewithnum:~-1!" == "%i" set "file=!filewithnum:~,-1!"` – Klitos Kyriacou Dec 22 '16 at 09:42
  • @KlitosKyriacou - clever. – dgo Dec 22 '16 at 13:50