0

I am trying to find the specific files, which exists or not there in the directory using Batch script

@echo off
setlocal
CD C:\MM-cmd\INPUT

for /r %%i in (*.csv) do set var=%%i( 

echo %var%
)

it's not getting filenames.

it's printing like:

ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.

Can anyone help me please?!

Trace
  • 51
  • 10
  • you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082). But why don't you just `echo %%i`? – Stephan Nov 08 '17 at 10:18
  • thanks Stephan for suggestion if i am using echo %%i it's printing %i only inside the loop i want to check if whether particular csv file is there or not in the directory so that's why i am trying to assigning file name only into the variable. – Ilavarapu Brahmaiah Nov 08 '17 at 10:21
  • How do you know that a file isn't there?. Attention: Syntax is different on command line and batchfiles. On command line use `%i` syntax, inside batchfiles use `%%i` syntax. – Stephan Nov 08 '17 at 12:28

1 Answers1

0

This lists all the files (and only the files) in the current directory:

for /r %i in (*) do echo %i

Also if you run that command in a batch file you need to double the % signs.

for /r %%i in (*) do echo %%i

try this:

::Example directory
set SetupDir=C:\Users

::Loop in the folder with "/r" to search in recursive folders, %%f being a loop ::variable 
for /r "%SetupDir%" %%f in (*.msi *.exe) do set /a counter+=1

echo there are %counter% files in your folder it counts .msi and .exe files in your directory (and in the sub directory). So it also makes the difference between folders and files as executables.

Just add an extension (.pptx .docx ..) if you need to filter other files in the loop

Mushfiq
  • 759
  • 1
  • 8
  • 41
  • C:\MM-cmd\INPUT\D1.csv C:\MM-cmd\INPUT\D2.csv C:\MM-cmd\INPUT\D3.csv it's coming all the files with directory but i need filenames alone and i need set to a variable, after that i am going to do the if D1.csv is there or not in the directory – Ilavarapu Brahmaiah Nov 08 '17 at 10:54
  • try `%%~nxi` for file**n**ame and e**x**tension only. – Stephan Nov 08 '17 at 12:30
  • Thanks Stephan, I am getting the filenames with extension, but while using the below for loop for /r %%i in (*.csv) do set xfilevar=%%~nxi i am getting the last file name but i need to get the filenames into a variable one by one – Ilavarapu Brahmaiah Nov 08 '17 at 12:42
  • extend your loop: `for ..... do (`, then several lines containing your code, then `)` to close the loop. – Stephan Nov 08 '17 at 12:58
  • I tried the below for /r %%i in (*.csv) do( set xfilevar=%%~nxi echo %xfilevar% ) i am getting like this "do( was unexpected at this time." – Ilavarapu Brahmaiah Nov 08 '17 at 13:30