gpt4 book ai didi

batch-file - 批处理文件 - 目录的结果分离

转载 作者:行者123 更新时间:2023-12-03 19:17:15 24 4
gpt4 key购买 nike

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

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

最佳答案

这甚至不是一个精炼的过程,但这展示了如何在提供“格式化”输出的同时并行处理两个列表的读取。

@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

关于batch-file - 批处理文件 - 目录的结果分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41697947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com