как сделать список файлов с помощью скрипта .bat

У меня есть структура каталогов:

pakages
|-files.bat
|-component
  |-source
  |-lib

Мне нужно создать текстовый файл с помощью скрипта files.bat с таким списком файлов:

 File0001=source\WindowT.pas
 File0002=source\AWindowT.pas
 File0003=source\AWindowSplash.pas
 File0004=source\InternalT.pas
 File0005=source\ImageLister.pas
 File0006=lib\LIcons.res
 File0007=lib\TstandartBtn_16.RES
 File0008=lib\TstandartBtn_24.RES

......и т.д

Как сделать такой листинг файлов?

заранее спасибо


person phpcoder    schedule 08.06.2009    source источник


Ответы (3)


Я собрал следующий сценарий:

@echo off
setlocal
rem The number of the current file, gets incremented
set FileNumber=1
rem Move into "component" directory, the following "for" command will 
pushd %~dp0\component
rem loop over directories there
for /d %%f in (*) do call :process "%%f"
rem move into the previous directory again
popd
endlocal
goto :eof

:process
rem process files
for /f %%x in ('dir /b /a:-d %1 2^>nul') do call :process_files %~1 %%x
rem go down recursively, if there are more subdirectories
for /f %%d in ('dir /b /a:+d %1 2^>nul') do call :process %%d
goto :eof

:process_files
call :leadingzeros %FileNumber%
rem Output line
echo File%RESULT%=%~1\%2
set /a FileNumber+=1
goto :eof

rem Parameter: a non-negative number <= 9999
rem Result: a string with zero padding at the start
:leadingzeros
if %1 LSS 10 set RESULT=000%1&goto :eof
if %1 LSS 100 set RESULT=00%1&goto :eof
if %1 LSS 1000 set RESULT=0%1&goto :eof
set RESULT=%1
goto :eof

Возможно, это не совсем то, что вам нужно, но должно стать отправной точкой. Вывод, однако, идентичен. Для следующего дерева файлов / каталогов:

    packages
    │   files.cmd
    │
    └───component
        ├───lib
        │       LIcons.res
        │       TstandardBtn_16.RES
        │       TstandardBtn_24.RES
        │
        └───source
                AWindowSplash.pas
                AWindowT.pas
                ImageLister.pas
                InternalT.pas
                WindowT.pas

запуск пакета приведет к следующему результату:

File0001=lib\LIcons.res
File0002=lib\TstandardBtn_16.RES
File0003=lib\TstandardBtn_24.RES
File0004=source\AWindowSplash.pas
File0005=source\AWindowT.pas
File0006=source\ImageLister.pas
File0007=source\InternalT.pas
File0008=source\WindowT.pas
person Joey    schedule 08.06.2009
comment
Большое спасибо! Это именно то, что я хотел. - person phpcoder; 08.06.2009

dir /A:-D /B /S>out.txt

Это дает вам большую часть того, что вы хотите.

person MatthewMartin    schedule 08.06.2009
comment
Спасибо, но мне нужен цикл для генерации в строке File0001, File0002 и т. Д. А также мне нужен относительный путь к файлу dir / A: -D / B /S›out.txt показывает полный путь - person phpcoder; 08.06.2009

Начните с чего-нибудь вроде:

@echo off

call :dodir .
goto eof

:dodir
set TEMP_CUR_DIR="%*"
REM echo Directory %TEMP_CUR_DIR%:
for %%f in (%TEMP_CUR_DIR%\*) do call :dofile %%f
for /d %%d in (%TEMP_CUR_DIR%\*) do call :dodir %%d
goto eof

:dofile
set TEMP_CUR_FILE="%*"
echo File: %TEMP_CUR_FILE%
goto eof

:eof
person aib    schedule 08.06.2009