Tengo el siguiente código:
for /f "tokens=*" %%p in (input.txt) do ( echo %%p cd %%p set /a c = 0 for %%f in (*) do ( echo %%f if not exist *test*.* ( set /a c += 1 ) ) if %%c GTR 0 echo %%p >>folders.txt cd .. )
But the last if statement is not working. I want to know what do I have to do to have access to the variable c that I set up in the first for. I've tried different combinations like !c! or %c% but nothing seems to work. What am I missing?
preguntado el 31 de julio de 12 a las 12:07
Tienes algunos problemas aquí:
No spaces must be around the
set c=0 set /a c+=1
Otherwise you're creating a variable whose name ends in a space.
You cannot set variables in a block and use them in the same block again without using delayed expansion. So you need
al comienzo de su archivo por lotes y luego use
!c!en lugar de
%%c(which in itself is wrong already because variables of the
forloop variables, not environment variables you set with
%%pis correct because it is a
Delayed expansion is necessary because
cmdwill expand variables to their values as soon as a statement is analizado, not directly prior to its execution. And the whole block of your
forloop is a single statement as far as
cmdis concerned, thus when the loop runs any environment variables are already expanded to their values.
So you'll end with
setlocal enabledelayedexpansion for /f "tokens=*" %%p in (input.txt) do ( echo %%p pushd %%p set /a c=0 for %%f in (*) do ( echo %%f if not exist *test*.* ( set /a c+=1 ) ) if !c! GTR 0 echo %%p >>folders.txt popd )
popd en lugar de
cd to change directories which is a little nicer.)