batch file help: parentheses


  1. Posts : 29
    windows 10 1909
       #1

    batch file help: parentheses


    I have the following batch commands to extract and compare the last character of strings in a file:

    @for /f "tokens=1 delims=;" %%A in ( C:\test.txt) do set d=%%A
    @echo %d%
    @echo %d:~-1%
    @set c=%d:~-1%
    @echo %c%
    @if %c%==m @echo found it

    It works as I hoped but only for the last line in the file test.txt. I'd like to have the batch commands work on every line in the file test.txt. So, I put commands after the "do' command in parentheses. Now, nothing works. I got an error message saying "@echo was unexpected at this time."

    My guess is one of the variable is now empty. So, I add before the "For" command "setlocal EnableDelayedExpansion." It did not help and I don't know what triggers the error message.
      My Computer


  2. Posts : 425
    Windows 10
       #2

    Can you post a sample of the file you are processing, please.
      My Computer


  3. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    You are not using the For command correctly.
    - You have told it to work its way through setting each line to be the value of the variable d
    - It is doing exactly what you have told it to do.
    - It works through every line setting each one as d in turn until it finally arrives at the last line and sets it as d
    - Only then does it move on to the next command line @echo %d% so you only get to see its last action.


    In order to achieve what I think you want to achieve, you should use the For command to call a subroutine and do your processing within that.
    - Using subroutines can be thought of as an alternative to using EnableDelayedExpansion. I find subroutines much easier to write, understand & debug.

    Code:
    for /f "tokens=1 delims=;" %%A in ( C:\test.txt) do Call :Processing "%%A"
    Pause at the end of it all
    :: This is where you would move on to do whatever it is that you want to do next.
    Exit
    
    :Processing
    Set "ThisLine=%~1"
    echo "%ThisLine%"
    echo "%ThisLine:~-1%"
    set "c=%:ThisLine~-1%"
    echo "%c%"
    if "%c%==m" echo found it
    Pause in each iteration of the For command
    GoTo :EOF

    - Variables need to be wrapped in " unless & until you check that they contain no special characters such as ampersands.
    - Variables need to be wrapped in " if they contain spaces.
    - If there are no spaces or special characters you can remove the " from "%%A" & the subroutine.


    I do not know why you are limiting the For command with "tokens=1 delims=;" but that's up to you.


    To see For's built-in Help, enter this command
    Code:
    For /?

    For - SS64
    For - Looping commands - SS64
    For -F - SS64 Forum


    All the best,
    Denis
    Last edited by Try3; 07 Dec 2021 at 06:49.
      My Computer


  4. Posts : 29
    windows 10 1909
    Thread Starter
       #4

    Thanks for the help. I got the batch file to work. Here it is:
    Code:
    REM This script first deletes the file C:DELETE-ALL-4-NEW.txt, if it exists
    REM It then proceeds to read from C:\DELETE-ALL-NEW.txt line by line and
    REM delete the file in those lines from several disks.
    REM If the line contains not a file but a directory, as the line ending with a "\",
    REM the script will skip it. 
    REM 2021/12/08
    
    
    @echo off
    
    if exist C:\DELETE-all-4-NEW.txt DEL /Q C:\DELETE-all-4-NEW.txt
    (for /f "tokens=1 delims=;" %%A in ( C:\DELETE-all-NEW.txt) do (
    
    set "d=%%A"
        setlocal enabledelayedexpansion
        echo !d!
        echo !d:~-1!
        set "c=!d:~-1!"
        echo %c%
        if /I not "!c!"=="\"  DEL /Q /A "D:\%%A" & DEL /Q /A "E:\%%A" & DEL /Q /A "F:\%%A"
    
    endlocal
    ) >> C:\DELETE-all-4-NEW.txt 2>&1
    )
    CD /D C:\
      My Computer


  5. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #5

    I'm glad it works now.

    By the way, it's up to you but
    - REM is quite slow.
    - Like many people I only use REM if I deliberately want those lines to appear onscreen whenever the batch file runs.
    - Like many people, I use :: otherwise. It's a bit of a trick in that it makes Windows treat it as an inoperative line but one that it does not take the time to bother displaying or even reading fully.
    - There was an example of its use for remarks in my last post as well.
    - The last time I put the REM / :: comparison to the test was about thirty years ago and, on that occasion, it reduced the running time for a batch file containing a hundred or more REMs from 30 secs to 1 or 2 secs. So it's not vital for normal batch files.
    Code:
    :: This script first deletes the file C:DELETE-ALL-4-NEW.txt, if it exists
    :: It then proceeds to read from C:\DELETE-ALL-NEW.txt line by line and
    :: delete the file in those lines from several disks.
    :: If the line contains not a file but a directory, as the line ending with a "\",
    :: the script will skip it. 
    :: 2021/12/08

    All the best,
    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 09:30.
Find Us




Windows 10 Forums