Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Can't set variable in for loop even when setlocal enabledelayedexpansion is on  (Read 8767 times)

0 Members and 1 Guest are viewing this topic.

zask

    Topic Starter


    Intermediate

    • Experience: Experienced
    • OS: Other
    Okay I have this script which uses a for loop to search through all batch files on the computer, I want to disable them by turning them Into a text file. However I  need to modify a variable but it will not set.

    Here is the script

    for %%A in ("%CD%") do set topfolder=%%~DA\
    setlocal enabledelayedexpansion
    for /l %%x in (1,0,2) do (
    for %%A in (*.bat) do echo "%%~DPNXA"
    for /r %%X in (*.bat) do echo "%%X"
    CD..
    if "!CD!"=="%TOPFOLDER%" call :end )
    :end

    What I'm trying to do is set this variable

    for %%A in ("%CD%") do set topfolder=%%~DA\
    setlocal enabledelayedexpansion
    for /l %%x in (1,0,2) do (
    for %%A in (*.bat) do set "file1=%%~DPNXA"
    for /r %%X in (*.bat) do set "file2=%%X"
    Echo !file1!
    Echo !file2!
    Pause
    CD..
    if "!CD!"=="%TOPFOLDER%" call :end )
    :end

    For some reason I just can't get the value "℅℅~DPNXA" To equal "!file1!" And I can't get "%%X" to equal to "!file2!", I want to do this so a can change them to something like this
    "!file1:~0,-4!.txt".
    « Last Edit: February 02, 2017, 10:46:49 AM by zask »

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    First of all,doing nested FOR loops is way too hard for many of us.
    Is there a good reason for using nested loops?
    Have you tried it with out loops?


    zask

      Topic Starter


      Intermediate

      • Experience: Experienced
      • OS: Other
      well i really haven't found any difficulty until now, figured out just about everything so far except this one issue, i was trying to avoid spaghetti code mainly

      zask

        Topic Starter


        Intermediate

        • Experience: Experienced
        • OS: Other
        and yes i have tried without the for loop

        the non for loop version looks like this.

        @echo off
        for %%A in ("%cd%") do set topfolder=%%~dA\
        :start
        for %%A in (*.bat) do set "file1=%%~dpnxA"
        for /r %%X in (*.bat) do set "file2=%%X"
        echo "%file1:~0,-4%.txt"
        echo "%file2:~0,-4%.txt"
        pause
        if "%cd%"=="%topfolder%" goto root
        cd..
        goto start
        :root

        Salmon Trout

        • Guest
        It sets variables for me, or not, depending on whether there are any files that satisfy the filespec.

        I can see some traps lurking...

        I modified the script to provide some debug information (you should be doing this!)

        Script is: E:\plex\data\Plex Media Server\test\test.bat

        Script:

        @echo off
        for %%A in ("%cd%") do set topfolder=%%~dA\
        :start
        echo CD="%cd%"
        for %%A in (*.bat) do set "file1=%%~dpnxA"
        for /r %%X in (*.bat) do set "file2=%%X"
        echo "%file1:~0,-4%.txt"
        echo "%file2:~0,-4%.txt"
        pause
        if "%cd%"=="%topfolder%" goto root
        cd..
        goto start
        :root
        echo Finished
        Pause


        Output:

        CD="E:\plex\data\Plex Media Server\test"
        "E:\plex\data\Plex Media Server\test\test.txt"
        "E:\plex\data\Plex Media Server\test\test.txt"
        Press any key to continue . . .
        CD="E:\plex\data\Plex Media Server"
        "E:\plex\data\Plex Media Server\test\test.txt"
        "E:\plex\data\Plex Media Server\test\test.txt"
        Press any key to continue . . .
        CD="E:\plex\data"
        "E:\plex\data\Plex Media Server\test\test.txt"
        "E:\plex\data\Plex Media Server\test\test.txt"
        Press any key to continue . . .
        CD="E:\plex"
        "E:\plex\data\Plex Media Server\test\test.txt"
        "E:\plex\data\Plex Media Server\test\test.txt"
        Press any key to continue . . .
        CD="E:\"
        "E:\plex\data\Plex Media Server\test\test.txt"
        "E:\plex\data\Plex Media Server\test\test.txt"
        Press any key to continue . . .
        Finished
        Press any key to continue . . .
        « Last Edit: February 02, 2017, 02:17:44 PM by Salmon Trout »

        Salmon Trout

        • Guest
        Your loop one gives values too...

        Again, a couple of mods

        E:\plex\data\Plex Media Server\test\test2.bat

        @echo off
        setlocal enabledelayedexpansion
        for %%A in ("%CD%") do set topfolder=%%~DA\
        for /l %%x in (1,0,2) do (
        for %%A in (*.bat) do set "file1=%%~DPNXA"
        for /r %%X in (*.bat) do set "file2=%%X"
        Echo !file1!
        Echo !file2!
        Pause
        CD..
        if "!CD!"=="%TOPFOLDER%" call :end
        )
        :end
        Echo Finished
        Pause


        Output

        E:\plex\data\Plex Media Server\test\test2.bat
        E:\plex\data\Plex Media Server\test\test2.bat
        Press any key to continue . . .
        E:\plex\data\Plex Media Server\test\test2.bat
        E:\plex\data\Plex Media Server\test\test2.bat
        Press any key to continue . . .
        E:\plex\data\Plex Media Server\test\test2.bat
        E:\plex\data\Plex Media Server\test\test2.bat
        Press any key to continue . . .
        E:\plex\data\Plex Media Server\test\test2.bat
        E:\plex\data\Plex Media Server\test\test2.bat
        Press any key to continue . . .
        Finished
        Press any key to continue . . .
         


        I'd be in interested to know what you think these scripts are going to do...



        zask

          Topic Starter


          Intermediate

          • Experience: Experienced
          • OS: Other
          Thank you, I'll have to check it out tomorrow, I'll swing back back and tell you if it worked for me.

          zask

            Topic Starter


            Intermediate

            • Experience: Experienced
            • OS: Other
            Thank you, I'll have to check it out tomorrow, I'll swing back back and tell you if it worked for me.
            I think the issue I was having was not being able to see the values within the loop, because when I typed echo "%%~DpnxA"  and echo "%%X", nothing appeared except "", so it made it difficult to make debug notes within the script.

            zask

              Topic Starter


              Intermediate

              • Experience: Experienced
              • OS: Other
              Is there a way to make it ignore file paths that don't satisfy or end with the .bat extension before its path is set to the variable and modified to an .txt extension? I know everything isnt possible in batch but thank you anyway for the help.
              « Last Edit: February 02, 2017, 03:36:59 PM by zask »

              Salmon Trout

              • Guest
              when I typed echo "%%~DpnxA"  and echo "%%X",
              They appear for me. Where and when were you "typing" these things?


              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
               ;D    8)
              " Anyone who goes to a psychiatrist should have his head examined. "

              zask

                Topic Starter


                Intermediate

                • Experience: Experienced
                • OS: Other
                When I'm trying to put it in !file1! & !file2! Running inside the infinite for loop, it just didn't appear for some reason 

                zask

                  Topic Starter


                  Intermediate

                  • Experience: Experienced
                  • OS: Other
                  When I'm trying to put it in !file1! & !file2! Running inside the infinite for loop, it just didn't appear for some reason, I don't know why but I'll try to figure it out.

                  zask

                    Topic Starter


                    Intermediate

                    • Experience: Experienced
                    • OS: Other
                    What do you mean I would be interested to know what they are going to do?

                    zask

                      Topic Starter


                      Intermediate

                      • Experience: Experienced
                      • OS: Other
                      What do you mean you would be interested to know what they are going to do? I'm trying to mimic the assoc command without actually setting every file's extension to a different extension permanently until you change it back, well it would change it like that, but it wouldn't permanently associate every extension for every file after that. If sure you understand what I mean if you have ever used the assoc command.