2

To list full pathnames of files in specified path I may use:

FOR /F "tokens=*" %G IN ('DIR /B "path\*.*"') DO echo %~fG

WRONG result: <current_directory>\*.*

ss64.com says: "If a filename with no drive letter/path is expanded to display a drive letter/path the command shell will assume; often incorrectly; that the file resides in the current directory."

This is quite a silly behaviour. However this is probably the problem as DIR here returns a bare filename.

IS THERE ANY WAY TO AVOID SUCH MISTAKE? As it is very easy to make.

I know I can use /S option in DIR command, which makes the result be a full pathname but it also goes through subfolders which is undesired.

Using following syntax everything goes fine but I can't use the advantages of DIR command:

FOR %G IN ("path\*.*") DO echo %~fG

result: <path>\*.*

Do you have any tips or tricks how to work with DIR and full paths?

endigo
  • 69
  • 3

3 Answers3

1

The environment variable CD contains at any time the path of current directory always without backslash at the end.

So you can use for your example:

@echo off
set "DirectoryPath=%CD%\path"
for /F "tokens=*" %%G in ('dir /B "path\*.*"') do echo %DirectoryPath%\%%G

Therefore whenever using DIR with bare output format without using also /S, it is necessary to determine first the directory path and reference this path within body of FOR loop.

Example on using fixed absolute paths:

@echo off
for /F "tokens=*" %%G in ('dir /B "C:\Temp\My Folder\*.*"') do echo C:\Temp\My Folder\%%G

Don't forget the double quotes with path or file name containing a space on other commands than echo!

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks, I believe this particular method solves the issue only where _path_ is relative to current directory, however, I understand it is necessary to store the path outside the loop and then combine it with the filename. – endigo Oct 22 '15 at 13:50
  • Well, that's right. If on command __DIR__ an absolute path is used, the same path must be also used on file references inside the __FOR__ loop when not using `/S` on command __DIR__ to work with file names with full path. – Mofi Oct 22 '15 at 16:03
0

How about using FORFILES? This will give you the full path for any desired folder:

forfiles /p C:\Some\Directory\ /c "cmd /c echo @path"

FORFILES is really mighty as it povides lots of options such as filters, rucursion into subfolders, etc. For more info check this website.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • **ss64.com** is a great site, I mostly look here. However, I run under XP and `FORFILES` is stated here to be available only under XP with **ResourceKit** which I am afraid I don't have. But I may give it a try. Thanks anyway. – endigo Oct 22 '15 at 11:53
0

If you really need to use the dir command

@echo off
setlocal ENABLEDELAYEDEXPANSION

set _subdir=path
set _mask=*.*

call :get_absolute_path _prefix "%CD%\%_subdir%"

rem  Iterate through a list of files, including files in subdirectories
for /f "tokens=*" %%A in ('dir /b /s /a:-d "%_prefix%\%_mask%"') do (
    rem  The current full file path
    set _f=%%A
    rem  Cut the "%CD%\%_subdir%\" prefix from the current file path
    set _f=!_f:%_prefix%\=!
    rem  Test whether the relative file path has a "subdir\" prefix:
    rem    split the relative file path by "\" delimiter and
    rem    pass %%B and %%C tokens (subdir and the remainder) to the loop
    for /f "delims=\ tokens=1*" %%B in ("!_f!") do (
        rem  If the remainder is empty string then print the full file path
        if "%%C"=="" echo %%A
    )
)

endlocal
exit /b 0

:get_absolute_path
set %1=%~f2
exit /b 0
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
  • Thanks. Anyway, is there another option to `DIR`? I want to have a list of files sorted by eg. date, so I thought `DIR` was an easy way. – endigo Oct 22 '15 at 12:11
  • No, there is not such option. You can use `dir ... | findstr /r "..."` but the regexp in findstr is lame. And it is not easy to detect subdir in the full file path. – Dmitry Sokolov Oct 22 '15 at 12:20
  • Sorry, I meant: 'Is there another option **besides** `DIR`?' (option as possibility, not the command option) – endigo Oct 22 '15 at 13:57