Computer Hope
Microsoft => Microsoft DOS => Topic started 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".
-
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?
-
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
-
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
-
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 . . .
-
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...
-
Thank you, I'll have to check it out tomorrow, I'll swing back back and tell you if it worked for me.
-
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.
-
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.
-
when I typed echo "%%~DpnxA" and echo "%%X",
They appear for me. Where and when were you "typing" these things?
-
;D 8)
-
When I'm trying to put it in !file1! & !file2! Running inside the infinite for loop, it just didn't appear for some reason
-
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.
-
What do you mean I would be interested to know what they are going to do?
-
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.
-
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.