问题
I am not familiar with batch script variable scoping. But I ran into scoping problem due to my lack of batch script experience.
for /f %%i in ('dir /s /b "%FolderLocation%"') do (
set fullpathandfilename=%%i ::
For %%A in ("%fullpathandfilename%") do (
Set File=%%~nxA
echo %%File :: this would work and print out only filename
)
echo %File% ::this will not have filename I extracted above
)
So how can i use %%File outside my inner for loop
回答1:
Again, EnableDelayedExpansion:
setlocal enabledelayedexpansion
for /f %%i in ('dir /s /b "%FolderLocation%"') do (
For %%A in ("%%~i") do (
Set File=%%~nxA
echo !File!
)
:: This shows how "%" doesn't work but "!" does
Echo ^%File^%: %File% - ^!File^!: !File!
)
And that should work for you. Just remember to use !File! inside a for-loop and %File% outside.
来源:https://stackoverflow.com/questions/25495767/how-to-use-batch-file-variable-outside-inner-for-loop