Board logo

标题: [文件操作] [已解决]批处理怎样列出指定文件夹下的文件? [打印本页]

作者: Wingl83    时间: 2010-10-19 18:42     标题: [已解决]批处理怎样列出指定文件夹下的文件?

我有一个很诡异的问题
  1. echo Set SelPath = CreateObject("Shell.Application").BrowseForFolder(0,"请选择文件夹",0)>%tmp%\test.vbs
  2. echo If SelPath Is Nothing Then WScript.Quit>>%tmp%\test.vbs
  3. echo TreePath = SelPath.self.Path ^& "\">>%tmp%\test.vbs
  4. echo WScript.echo TreePath>>%tmp%\test.vbs
  5. For /F "delims=#" %%i  in ('CScript //nologo %tmp%\test.vbs') Do Set Var=%%i
  6. if exist %tmp%\list.txt del %tmp%\list.txt /q
  7. :input11
  8. set input=%Var%
  9. set "input=%input:"=%"
  10. if "%input%"==":" goto input11
  11. if not exist "%input%" goto input11
  12. for %%i in ("%input%") do if /i "%%~di"==%%i goto input11
  13. pushd %cd%
  14. cd /d "%input%">nul 2>nul || exit
  15. set cur_dir=%cd%
  16. popd
  17. for /f "delims=" %%i in ('dir /b /a-d /s /os "%input%"') do echo %%~dpnxi>>%tmp%\list.txt
  18. if not exist %tmp%\list.txt goto no_file11
  19. start /w %tmp%\list.txt
复制代码
可以列出你选择的文件夹下的所有文件名,但是我不想列出子文件夹的文件名,所以删除了/s时,返回的路径是错误的,为什么会这样?应该怎样修改?

[ 本帖最后由 Wingl83 于 2010-10-21 20:47 编辑 ]
作者: yiyuan    时间: 2010-10-19 21:28     标题: 我是菜菜

/s显示指定目录和所有子目录中的文件,去掉的话,则将路径写为当前目录,所以肯定是不可以去掉的
应该是通过其他方法修改吧,等待高手。。。
我想楼主的技术要比我高多了,楼主自己想出答案了,不用忘记给出来啊
作者: powerbat    时间: 2010-10-19 21:48

For /F "delims=#" %%i in ('CScript //nologo %tmp%\test.vbs') Do Set Var=%%~i
为什么要用delims=#?
作者: powerbat    时间: 2010-10-19 22:05

  1. @echo off
  2. rem vbs选择文件夹对话框
  3. echo Set SelPath = CreateObject("Shell.Application").BrowseForFolder(0,"请选择文件夹",0)>"%tmp%"\test.vbs
  4. echo If SelPath Is Nothing Then WScript.Quit>>"%tmp%"\test.vbs
  5. echo TreePath = SelPath.self.Path ^& "\">>"%tmp%"\test.vbs
  6. echo WScript.echo TreePath>>"%tmp%"\test.vbs
  7. :input11
  8. rem 不允许所选目录为根目录?
  9. For /F "delims=" %%i in ('CScript //nologo "%tmp%"\test.vbs') Do (
  10.     if /i "%%~dpi"=="%%~fi" (echo 所选目录不能为根目录& goto input11)
  11.     Set Var=%%~i
  12. )
  13. if exist "%tmp%"\list.txt del "%tmp%"\list.txt /a/f/q
  14. pushd "%Var%"
  15. for /f "delims=" %%i in ('dir /b /a-d /os "%input%"') do echo %%~fi>>"%tmp%"\list.txt
  16. popd
  17. if not exist "%tmp%"\list.txt goto no_file11
  18. start "" /w "%tmp%"\list.txt
  19. :no_file11
  20. rem ...
  21. pause
复制代码

作者: 网上邻居    时间: 2016-3-31 13:01

  1. for %%i in ("%input%") do if /i "%%~di"==%%i goto input
复制代码
是什么意思?
作者: 网上邻居    时间: 2016-3-31 13:06

明白了,比较输入的是不是根目录。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2