如何使用 .bat 脚本列出文件

发布于 2024-07-23 07:54:28 字数 464 浏览 12 评论 0原文

我有目录结构:

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

......etc

如何制作这样的文件列表?

先感谢您

I have directory structure:

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

I need to make text file using the files.bat script with file listing like this:

 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

......etc

How to make such file listing?

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缘字诀 2024-07-30 07:54:28

我拼凑了以下脚本:

@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

I have cobbled together the following script:

@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

It may be not exactly what you need, but should provide a starting point. The output, however, is identical. For the following file/directory tree:

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

running the batch will produce the following output:

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
夜灵血窟げ 2024-07-30 07:54:28
dir /A:-D /B /S>out.txt

这就是你最想要的。

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

That get's you most of what you want.

小女人ら 2024-07-30 07:54:28

从以下内容开始:

@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

Start with something along the lines of:

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文