2

I am trying to read in a directory and get the filename from that directory. I then want to save the filename as a variable and echo that filename out.

Here is the code I am using:

 for /F %%a in ('dir C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\ *.csv') do set FileName=%%a
 echo %FileName%

I am getting the following when command prompt runs: "File not found Directory"

Does anyone know how to resolve this or where I'm going wrong?

Thanks

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Karen
  • 123
  • 2
  • 4
  • 14

1 Answers1

5

Safer way of doing the same:

@echo off
setlocal
set "yourDir=C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\"
set "yourExt=*.csv"
pushd %yourDir%
for %%a in (%yourExt%) do echo %%a
popd
endlocal

Sets both: Your directory and the extension you are searching for, Changes the directory to the one previously setted possibly including a /drive change and then runs a loop over all files matching your extension and echo them out. To save only the last one you can use:

...do set fileName=%%a
echo %FileName%

Or to use them all within the loop you can use:

@echo off
Setlocal EnableDelayedExpansion
REM Other things done here

do (
REM Do stuff with %%a here
Set filename=%%a
echo !filename!
echo !filename:~0,6!
echo !filename:a=b!
)

If you just want to echo them, you can just go for echo %%a. If you want to do other things like string-substitution or substrings as described in the comments you need DelayedExpansion as shown above. There are a lot of questions on SO as well.

Note that you can get different "parts" of the path of your file. Have a look on this answer I always have a look on as well. Alternatively check the documentation for the for command typing for /? into the command-line.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • thank you, this works! :) Could I not just use the following to get part of the filename (this does not seem to be working though?) SET result=%%a:~0,6% ECHO %result% – Karen Jan 30 '17 at 11:20
  • Nope. Reason is that these for loop parameter-variables are not thought to be used in that way. You could save it as variable and then using `DelayedExpansion` (<- Important stuff ;) ) do what you wanted to do. Note that you would always have to know which part of the string you want to get. Using delimiters and `for /f` or above shown way to get the filename are the usually safer way. – geisterfurz007 Jan 30 '17 at 11:25
  • 1
    I have found a way to do it using the following code: for %%a in (*.%yourExt%) do set filename=%%a echo %filename% However it then only echos out one filename when there are three filenames in the directory (I'm new to this) I have looked into delayedexpansion and I'm not 100% sure how it could help in this case? – Karen Jan 30 '17 at 11:39
  • 2
    I might be inclined to use `setlocal`, `pushd`, `popd`, and `endlocal` rather than just `set` and `cd` in something like this, just to ensure that I'm not doing anything potentially catastrophic to the environment. – Jeff Zeitlin Jan 30 '17 at 13:06
  • .... This was meant to write after the for-stuff... I was too lazy to type the rest of it. – geisterfurz007 Jan 30 '17 at 14:03
  • In your example, `set "yourExt=*.csv"` and `(*.%yourExt%)` is incompatible, I believe? I think it will yield a wildcard called `*.*.csv`. So it should just be `(%yourExt%)`, instead. – Hunnicatt Feb 18 '22 at 20:17
  • @Hunnicatt Good catch, fixed, thanks! – geisterfurz007 Feb 19 '22 at 18:01