user3646519
user3646519

Reputation: 363

Windows batch script to list files in directory and assign to variables

I have always scripted in Unix, but now have a requirement to do something in Windows. Requirement is that a property file contains path variable. The script needs to list all files under that path and then call a function on each file. This is what I have been trying

@For /F "tokens=1* delims==" %%A IN (C:\Work\sample\myFile.properties) DO (
IF "%%A"=="MPWR_EXPORT_PATH" set MPWR_EXPORT_PATH=%%B
IF "%%A"=="FILE_SHARE" set FILE_SHARE=%%B
)
cd %MPWR_EXPORT_PATH%
FOR %%i IN (*) DO echo %%i

Question is: How do I assign the %%i to variable and then pass filename to a function DISPAY()

Upvotes: 0

Views: 782

Answers (1)

PA.
PA.

Reputation: 29339

you don't need to assign to a variable to pass it to a function. The called function receives the parameter as %1 and it can be refined with the %~1 syntax.

I also suggest you to change your CD command for a PUSHD. This way (1) your %MPWR_PATH% variable can contain a drive letter or a network path, and (2) the bat can later cleanly restore the current directory with a POPD.

So, read HELP PUSHD and HELP CALL and try this.

pushd %MPWR_PATH%
for %%i in (*) do CALL :display %%i
popd
goto :eof

:display
echo %~f1
goto :eof

Upvotes: 1

Related Questions