0

I use

dir /b   > test.txt

in order to get a list of all files and folders in my current directory.

Is there a way to have :

  • normal filename if it is a file, example blabla.txt

  • "/" at the end if it is a folder, example myfolder\

?

Thanks.

Basj
  • 41,386
  • 99
  • 383
  • 673

4 Answers4

1

It's probably easiest to do it in two steps. One command for folders with appended \, and another command for files.

for /d %F in (*) do @(echo %F\)>test.txt
for %F in (*) do @(echo %F)>>test.txt

Double up the percents if used in a batch file.

dbenham
  • 127,446
  • 28
  • 251
  • 390
1
@ECHO OFF
:: method 1
SETLOCAL
ECHO %TIME%
(
FOR /f "delims=" %%i IN ('dir /b') DO (
 ECHO %%~ai|FIND "d" >NUL
 IF ERRORLEVEL 1 (ECHO(%%i) ELSE (ECHO(%%i\)
)
)>u:\file1.txt
endlocal
ECHO %TIME%

:: method 2
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%i IN ('dir /b') DO (
 SET notdir=%%~ai
 if "!notdir:~0,1!"=="d" (ECHO(%%i\) ELSE (ECHO(%%i)
)
)>U:\file2.txt
endlocal
ECHO %TIME%

:: method 3
SETLOCAL
(
FOR /f "delims=" %%i IN ('dir /b') DO (
 SET notdir=%%~ai&CALL :isitadir
 if DEFINED notdir (ECHO(%%i) ELSE (ECHO(%%i\)
)
)>U:\file3.txt
endlocal
ECHO %TIME%
FC u:\file1.txt u:\file2.txt
FC u:\file1.txt u:\file3.txt
GOTO :eof

:isitadir
IF %notdir:~0,1%==d SET "notdir="
GOTO :eof

Three methods shown.

The first is quite slow - took about 35 seconds to run on my test directory with 1976 files and 333 directories.

The second took under a second, but doesn't correctly process names containing !

The third is more complex, but produced the same result as the first in about 6 sec.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

This does as you ask. Folders have \ at the end, files don't.

@echo off
for /f "delims=" %%a in ('dir /b') do (
   if exist "%%a\" (
      echo %%a\
   ) else (
      echo %%a
   )
)
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

To list files that end with "txt"

dir /b *.txt > test.txt

List only directories:

dir /ad /b
daz3d
  • 53
  • 1
  • 5