jayant
jayant

Reputation: 1

batch files finding specific files in a directory

i am trying to get files from a directory and want to set names of the file to a variable using batch script.

this is my code .but it always setting same value to variable can any body give solution

echo on  
setlocal EnableDelayedExpansion  
for /f %%x in ('dir /b C:\backup_dir') do (  
SET test=%%~nx
if "%test:~0,6%"=="kdc_db" (set DUMP=%%x)  
if "%test:~0,6%"=="kdc_ke" (set KEYS=%%x)  
)  
echo %DUMP%  
echo %KEYS%

here dump and keys variables are always set to same value

Upvotes: 1

Views: 200

Answers (1)

Bali C
Bali C

Reputation: 31251

You need to use delayed expansion. You have already enabled it, you just need to replace your %'s with !'s

echo on  
setlocal EnableDelayedExpansion  
for /f %%x in ('dir /b C:\backup_dir') do (  
SET test=%%~nx
if "!test:~0,6!"=="kdc_db" (set DUMP=%%x)  
if "!test:~0,6!"=="kdc_ke" (set KEYS=%%x)  
)  
echo %DUMP%  
echo %KEYS%

Upvotes: 2

Related Questions