我在尝试完成自己的文件浏览器时遇到了问题。 这是我工作目录中的文件。我希望它回归:
FolderFoo FileFoo
FolderBar FileBar
但是我的脚本会返回:
FolderFoo FolderBar FileFoo FileBar
有人有想法吗?这是我的剧本:
echo.|set /p some=>"%~Dp0foo\bar\FileExprTemp.tmp"
for /F "tokens=*" %%a in ('dir /B') do (
echo.|set /p some="%%a ">>%~Dp0foo\bar\FileExprTemp.tmp
)
type %~dp0foo\bar\FileExprTemp.tmp
答案 0 :(得分:3)
在解决并测试并尝试和打高尔夫球后,我得到了一个(有点)工作代码:
@echo off
setlocal EnableDelayedExpansion
set counter=0
for /f "delims=" %%d in ('dir /ad /b') do (
set outputs[!counter!]=%%d
set /a counter=!counter!+1
)
set counter=0
for /f "delims=" %%f in ('dir /a-d /b') do (
call echo %%outputs[!counter!]%% %%f >> FileExprTemp.tmp
set /a counter=!counter!+1
)
type FileExprTemp.tmp
说明:
将只有目录(包括dir /ad /b
和.
)的命令..
的内容读入列表outputs
将显式排除目录的'dir /a-d /b'
命令的内容及echo
与已存在的列表内容一起读入临时文件。
type
文件的内容。
上述问题:格式化。没有什么可以让它看起来像桌子或任何东西。除了你的文件夹名称都是相同的长度...然后你是一个很好的人!
我希望这是一个足够的模板来工作,并使其适合你更紧密的愿望!
答案 1 :(得分:3)
这是我的部分解决方案。
像geisterfurz007所说,麻烦在于格式化。 但是这会并排组装文件和目录。 也许它会帮助你取得进步?
@echo off & goto :main
:WriteLine [%1|Skip]
setlocal
:: Apparently for /F "skip=0" is an error,
:: So I have to check for skip=0, and avoid that.
:: (To me, that is a bug, as skip=0 should be perfectly valid)
if %1 GTR 0 set SKIP_CMD="skip=%1"
for /F %SKIP_CMD% %%a in (Dirs.txt) do (set DIRNAME=%%a & goto :doFile)
:doFile
for /F %SKIP_CMD% %%a in (Files.txt) do (set FILENAME=%%a & goto :doOutput)
:doOutput
echo %DIRNAME% %FILENAME% >> FileExprTemp.tmp
exit /B
:main
dir /B /AD > Dirs.txt &:: Get all the Directories into one file
for /F "tokens=3" %%a in ('find /V /C "" Dirs.txt') do set DIR_COUNT=%%a
dir /B /A-D > Files.txt &:: Get all the files into a different file
for /F "tokens=3" %%a in ('find /V /C "" files.txt') do set FILE_COUNT=%%a
for /L %%a in (0,1,%FILE_COUNT%) do (call :WriteLine %%a) &:: Append each Directory and Filename to Output
type FileExprTemp.tmp &:: Show the results
del dirs.txt &:: Clean up temporary files
del files.txt
exit /B
答案 2 :(得分:3)
甚至不是一个精致的过程,但是这显示了如何在并行地处理两个列表的同时处理这两个列表的读取#34;输出
@echo off
setlocal enableextensions disabledelayedexpansion
rem Get root folder from command line - Default to current directory
for %%a in ("%~f1.") do set "root=%%~fa"
rem Prepare padding for directories column
set "folderWidth=30"
setlocal enabledelayedexpansion
set "padding= "
for /l %%a in (1 1 %folderWidth%) do set "padding=!padding! "
endlocal & set "padding=%padding%"
rem Prepate two files to store files and directories lists
for %%d in ("%temp%\directories.%random%%random%%random%.tmp") do (
for %%f in ("%temp%\files.%random%%random%%random%.tmp") do (
rem Retrieve the list of directories and files into the temp files
dir "%root%" /b /on /ad > "%%~fd" 2>nul
dir "%root%" /b /on /a-d > "%%~ff" 2>nul
rem Assign each temporary file to a separate stream
rem and call the code to process them
9<"%%~fd" 8<"%%~ff" call :dumpPairs
rem Clean up temporary files
) & del /q "%%~ff"
) & del /q "%%~fd"
rem All done, leave
goto :eof
rem Read stream 9 to retrieve folder name - Clean var on failure
rem Read stream 8 to retrieve file name - Clean var on failure
rem If both variables are uninitialized, there is nothing more to do
rem Concatenate folder name and padding
rem Echo the padded folder name and file name
rem Repeat the process
:dumpPairs
<&9 set /p "directory=" || set "directory="
<&8 set /p "file=" || set "file="
if not defined directory if not defined file goto :eof
set "line=%directory%%padding%"
setlocal enabledelayedexpansion
echo(!line:~0,%folderWidth%! !file!
endlocal
goto :dumpPairs
答案 3 :(得分:0)
我认为你的子文件夹中有多个文件。
代码:
@echo off
set /p Parent=Type parent folder location ^>
for /d %%D in ("%Parent%\*") do (
<nul set /p =%%~nxD
for /f "tokens=1-2 delims=:" %%F in ('dir /b "%%~fD" ^| findstr /n .') do (
if "%%F"=="1" (echo( %%G) else (echo( %%G)
)
echo.
)
pause>nul
使用<nul set /p
将允许您打印字符串而不在字符串后添加新行。
我认为有更简单的方法,我无法想到。
答案 4 :(得分:0)
当问题形成时,这个简单的界限应该是:
dir /d|findstr /bvc:" "
dir /d
执行表格事务(甚至处理文件名长度),findstr /bvc:" "
删除标题和摘要。
仅适用于文件夹:
dir /d /ad|findstr /bvc:" "
仅限文件:
dir /d /a-d|findstr /bvc:" "