Home > Mobile >  How to get the list of file present in folder and pass the file name dynamically to loop to get the
How to get the list of file present in folder and pass the file name dynamically to loop to get the

Time:04-05

In a directory, I have a multiple .txt file. For example:

abc.txt
bcd.txt
def.txt

Now in each of these file I have credential information like:

Hostname: 10.87.99.09
username: qwerty
password: ytrewq

These are the content present in the each of the .txt files present in folder.

Now my requirement is that, I need to ask the user for the folder path and once user provide the folder path I need to take .txt file present in the folder one by one and then take the server credentials present in file assign it to one variable so that I can pass those variable to sqlcmd command to perform operation.

For example, hostname: 10.23.32.21, then a= 10.23.32.21 so that I can pass %a% to sqlcmd command.

As, I am newbie to batch script I have written some code by doing rnd:

@echo off
setlocal enableDelayedExpansion
set /p file="Please enter file Name: "
cd %file%
set /x ID=1

for /f "delims=" %%G in ('dir *.txt /b') do (
set filename[!ID!]=%%~G
for /f "tokens=1,2delims=: " %%a in ('findstr /n . %%~G') do set "name[%%a]=%%b"
set c= %name[1]%
echo%c%
set /x ID =1
)

set filename
endlocal
pause

And getting output as:

Please enter file Name: C:\Users\LENOVO\Desktop\testing\multiple
The syntax of the command is incorrect.
ECHO is off.
The syntax of the command is incorrect.
ECHO is off.
The syntax of the command is incorrect.
ECHO is off.
The syntax of the command is incorrect.
filename[]=test.txt

I also tried so many things but not able to get the result. Can someone please help me with these? Thanks in advance!

CodePudding user response:

@ECHO OFF
setlocal enableDelayedExpansion
:again
set /p file="Please enter file Name: "
pushd "%file%"
if /i "           
  • Related