Computer Hope

Microsoft => Microsoft DOS => Topic started by: zask on February 02, 2017, 10:32:05 AM

Title: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 10:32:05 AM
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".
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: Geek-9pm on February 02, 2017, 10:58:43 AM
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?

Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 11:01:36 AM
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
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 11:06:54 AM
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
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: Salmon Trout on February 02, 2017, 01:59:32 PM
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 . . .
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: Salmon Trout on February 02, 2017, 02:51:50 PM
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...


Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 02:56:54 PM
Thank you, I'll have to check it out tomorrow, I'll swing back back and tell you if it worked for me.
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 03:00:36 PM
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.
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 03:24:07 PM
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.
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: Salmon Trout on February 02, 2017, 03:48:50 PM
when I typed echo "%%~DpnxA"  and echo "%%X",
They appear for me. Where and when were you "typing" these things?

Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: patio on February 02, 2017, 05:19:00 PM
 ;D    8)
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 06:07:42 PM
When I'm trying to put it in !file1! & !file2! Running inside the infinite for loop, it just didn't appear for some reason 
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 06:12:15 PM
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.
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 06:33:14 PM
What do you mean I would be interested to know what they are going to do?
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: zask on February 02, 2017, 06:36:57 PM
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.
Title: Re: Can't set variable in for loop even when setlocal enabledelayedexpansion is on
Post by: Salmon Trout on February 02, 2017, 11:59:53 PM
What do you mean I would be interested to know what they are going to do?
I mean, please describe the intended functioning of the batch, line by line.