views:

477

answers:

2

Hi,

I receive a file that contains the following:

\direcotry1\directory2\directory3\file1
\direcotry1\file2
\direcotry1\directory2\directory3\directory4\file3
\direcotry1\file4
\direcotry1\directory2\file5
file6

The amount of files in the file and the amount of directories are variable.

What I need is the filename (file1, file2, file3, file4, file5, ...). This is because I need to perform some actions for each file.

Thanks a lot in advance for your help.

A: 

Are you looking for this command?

dir /r /b /A:-D

EDIT:

I am sorry I did not understand that correctly. You can use that dir command to collect filenames directly in the similar manner as you asked.

You cannot use too many string processing function in batch files, but what you want is possible:

@echo off
for /f %%I in (inputfile.txt) DO ECHO %%~nxI

This will print filenames without path from inputfile.txt.

Jiri
/r? What does that do? In addition, the file names are in a file rather than on the filesystem where dir can see them.
paxdiablo
Ok, my fault, I have made correction.
Jiri
This solution is perfect aswell!Seems I can only click one green tick
Hey, A simular question. Now I'm trying to get the path only. I'm using %%~pi which works fine when there is a path. When there's no path it returns a path that has nothing to do.So in above example for file6 it shouldn't return anything but on my machine it returns /cft/
+4  A: 

Use this. Just replace the echo inside the loop to do whatever it is you need to do to file1, file2 and so on.

@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%i in (infile.txt) do (
    set x=%%~ni
    echo !x!
)
endlocal
paxdiablo
This works perfect!Thanks a thousand times!
Hey,A simular question.Now I'm trying to get the path only. I'm using %%~pi which works fine when there is a path. When there's no path it returns a path that has nothing to do
So in above example for file6 it shouldn't return anything but on my machine it returns /cft/
Can I assume your path when you're running the script is \cft ? There's no path information in the "file6" string so I suspect cmd.exe will just give you a default (either empty string or current directory would be my guess). What do you believe it should return?
paxdiablo
I would also suggest opening up another question (referencing this one if you wish) since you'll get a lot more people looking at it that way. Nobody's going to be looking at this one from two weeks ago - I only did because I'm notified of comments to my answers.
paxdiablo
I think that the program that is calling is indeed \cftI'll open a new question. Thanks