Batch File; List files in directory, only filenames?

前端 未结 8 1376
我在风中等你
我在风中等你 2021-01-29 22:07

This is probably a very simple question, but I\'m having trouble with it. Basically, I am trying to write a Batch File and I need it to list all the files in a certain directory

8条回答
  •  离开以前
    2021-01-29 22:33

    • Why not use where instead dir?

    In command line:

    for /f tokens^=* %i in ('where .:*')do @"%~nxi"
    

    In bat/cmd file:

    @echo off 
    
    for /f tokens^=* %%i in ('where .:*')do %%~nxi
    
    • Output:

    file_0003.xlsx
    file_0001.txt
    file_0002.log
    
    where .:*
    
    • Output:

    G:\SO_en-EN\Q23228983\file_0003.xlsx
    G:\SO_en-EN\Q23228983\file_0001.txt
    G:\SO_en-EN\Q23228983\file_0002.log
    

    For recursively:

    where /r . *
    
    • Output:

    G:\SO_en-EN\Q23228983\file_0003.xlsx
    G:\SO_en-EN\Q23228983\file_0001.txt
    G:\SO_en-EN\Q23228983\file_0002.log
    G:\SO_en-EN\Q23228983\Sub_dir_001\file_0004.docx
    G:\SO_en-EN\Q23228983\Sub_dir_001\file_0005.csv
    G:\SO_en-EN\Q23228983\Sub_dir_001\file_0006.odt
    
    • For loop get path and name:

    • In command line:
    for /f tokens^=* %i in ('where .:*')do @echo/ Path: %~dpi ^| Name: %~nxi
    
    • In bat/cmd file:
    @echo off 
    
    for /f tokens^=* %%i in ('where .:*')do echo/ Path: %%~dpi ^| Name: %%~nxi
    
    • Output:

     Path: G:\SO_en-EN\Q23228983\ | Name: file_0003.xlsx
     Path: G:\SO_en-EN\Q23228983\ | Name: file_0001.txt
     Path: G:\SO_en-EN\Q23228983\ | Name: file_0002.log
    

    • For loop get path and name recursively:

    In command line:

    for /f tokens^=* %i in ('where /r . *')do @echo/ Path: %~dpi ^| Name: %~nxi
    

    In bat/cmd file:

    @echo off 
    
    for /f tokens^=* %%i in ('where /r . *')do echo/ Path: %%~dpi ^| Name: %%~nxi
    
    • Output:

     Path: G:\SO_en-EN\Q23228983\ | Name: file_0003.xlsx
     Path: G:\SO_en-EN\Q23228983\ | Name: file_0001.txt
     Path: G:\SO_en-EN\Q23228983\ | Name: file_0002.log
     Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0004.docx
     Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0005.csv
     Path: G:\SO_en-EN\Q23228983\Sub_dir_001\ | Name: file_0006.odt
    

    • Some further reading:

      [√] Where

      [√] Where sample

提交回复
热议问题