本文介绍了批处理文件-Dir的结果分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试完成自己的文件资源管理器时遇到问题.这是我的工作目录中的文件.我希望它返回:

I ran into a problem while trying to finish up my own file explorer.This is the files in my working directory. I would like it to return:

FolderFoo     FileFoo
FolderBar     FileBar

但是我的脚本返回:

FolderFoo FolderBar FileFoo FileBar

有人有什么主意吗?这是我的脚本:

Does anybody have some idea? Here's my script:

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

推荐答案

甚至没有完善的过程,但是这显示了如何在提供格式化"输出的同时并行处理两个列表的读取.

Not even a refined process, but this shows how to handle the read of the two lists in parallel while provinding a "formated" output.

@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

这篇关于批处理文件-Dir的结果分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:38