标题: [文件操作] [已解决]批处理怎样列出指定文件夹下的文件? [打印本页]
作者: Wingl83 时间: 2010-10-19 18:42 标题: [已解决]批处理怎样列出指定文件夹下的文件?
我有一个很诡异的问题-
- echo Set SelPath = CreateObject("Shell.Application").BrowseForFolder(0,"请选择文件夹",0)>%tmp%\test.vbs
- echo If SelPath Is Nothing Then WScript.Quit>>%tmp%\test.vbs
- echo TreePath = SelPath.self.Path ^& "\">>%tmp%\test.vbs
- echo WScript.echo TreePath>>%tmp%\test.vbs
- For /F "delims=#" %%i in ('CScript //nologo %tmp%\test.vbs') Do Set Var=%%i
- if exist %tmp%\list.txt del %tmp%\list.txt /q
- :input11
- set input=%Var%
- set "input=%input:"=%"
- if "%input%"==":" goto input11
- if not exist "%input%" goto input11
- for %%i in ("%input%") do if /i "%%~di"==%%i goto input11
- pushd %cd%
- cd /d "%input%">nul 2>nul || exit
- set cur_dir=%cd%
- popd
- for /f "delims=" %%i in ('dir /b /a-d /s /os "%input%"') do echo %%~dpnxi>>%tmp%\list.txt
- if not exist %tmp%\list.txt goto no_file11
- 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
- @echo off
- rem vbs选择文件夹对话框
- echo Set SelPath = CreateObject("Shell.Application").BrowseForFolder(0,"请选择文件夹",0)>"%tmp%"\test.vbs
- echo If SelPath Is Nothing Then WScript.Quit>>"%tmp%"\test.vbs
- echo TreePath = SelPath.self.Path ^& "\">>"%tmp%"\test.vbs
- echo WScript.echo TreePath>>"%tmp%"\test.vbs
-
- :input11
- rem 不允许所选目录为根目录?
- For /F "delims=" %%i in ('CScript //nologo "%tmp%"\test.vbs') Do (
- if /i "%%~dpi"=="%%~fi" (echo 所选目录不能为根目录& goto input11)
- Set Var=%%~i
- )
-
- if exist "%tmp%"\list.txt del "%tmp%"\list.txt /a/f/q
- pushd "%Var%"
- for /f "delims=" %%i in ('dir /b /a-d /os "%input%"') do echo %%~fi>>"%tmp%"\list.txt
- popd
- if not exist "%tmp%"\list.txt goto no_file11
- start "" /w "%tmp%"\list.txt
-
- :no_file11
- rem ...
- pause
复制代码
作者: 网上邻居 时间: 2016-3-31 13:01
- 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 |