【问题标题】:how to make file listing using the .bat script如何使用 .bat 脚本制作文件列表
【发布时间】:2010-11-01 03:39:06
【问题描述】:

我有目录结构:

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

……等等

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

提前谢谢你

【问题讨论】:

    标签: batch-file file-listing


    【解决方案1】:
    dir /A:-D /B /S>out.txt
    

    这就是你想要的大部分。

    【讨论】:

    • 谢谢,但我需要在字符串 File0001、File0002 等中生成循环,而且我需要文件 dir /A:-D /B /S>out.txt 显示完整路径的相对路径跨度>
    【解决方案2】:

    从以下内容开始:

    @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
    

    【讨论】:

      【解决方案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
      

      这可能不是您所需要的,但应该提供一个起点。但是,输出是相同的。对于以下文件/目录树:

      包 │ 文件.cmd │ └────组件 ├───lib │ LIcons.res │ TstandardBtn_16.RES │ TstandardBtn_24.RES │ └────来源 AWindowSplash.pas AWindowT.pas ImageLister.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
      

      【讨论】:

      • 非常感谢!这正是我想要的。
      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2020-07-23
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2018-06-11
      相关资源
      最近更新 更多