FOR循环将每个迭代以相同的前缀分组在一行中 [英] FOR loop group each iteration with same prefix in one line
本文介绍了FOR循环将每个迭代以相同的前缀分组在一行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以一个练习中发现有趣的问题为例:合并具有相关文件名的pdf文件
I take as an example the question that I found interesting as an exercise: Merge pdf files with related filenames
在该问题中,我尝试部分成功地回答,因为它已更新了更多信息。
In the question I tried to answer with a partial success because it has updated the question with more information.
总结一下问题是包含以下内容的文件夹:
To summarize the problem is a folder containing:
123456_ABCD.pdf
123456_EFGH.pdf
123456_IJKL.pdf
111111_ABCD.pdf
111111_EFGH.pdf
222222_IJKL.pdf
222222_WXYZ.pdf
然后在命令 FOR
中,我想要得到这样的输出:
And in a command FOR
I wanted to get output like this:
123456_ABCD.pdf, 123456_EFGH.pdf, 123456_IJKL.pdf
111111_ABCD.pdf, 111111_EFGH.pdf
222222_IJKL.pdf, 222222_WXYZ.pdf
这里的每一行都表示在命令 for
Each line here are supposed representing the same prefix found in the command for
这是我尝试的方法:
Here's what I tried:
@echo off
(
copy nul 123456_ABCD.pdf
copy nul 123456_EFGH.pdf
copy nul 123456_IJKL.pdf
copy nul 111111_ABCD.pdf
copy nul 111111_EFGH.pdf
copy nul 222222_IJKL.pdf
copy nul 222222_WXYZ.pdf
)
setlocal enabledelayedexpansion
:: set _pdffiles=
set _prevfiles=
for /f "delims=" %%i in ('dir /b /a-d /o:n "??????_????.pdf"') do (
set "files=%%nxi"
if "!files:~0,6!" neq "!_prevfiles:~0,6!" (
set "_prevfiles=%%i"
set _pdffiles=!_pdffiles! "%%i"
set "_outputpdf=%%~ni"
) else (
set _prevfiles=
set _pdffiles=
)
echo pdftk.exe !_pdffiles! cat output "!_outputpdf:~0,6!.pdf"
)
但是给我输出:
pdftk.exe "111111_ABCD.pdf" cat output "111111.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" cat output "111111.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" cat output "123456.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" "222222_IJKL.pdf" cat output "222222.pdf"
pdftk.exe "111111_ABCD.pdf" "111111_EFGH.pdf" "123456_ABCD.pdf" "123456_EFGH.pdf" "123456_IJKL.pdf" "222222_IJKL.pdf" "222222_WXYZ.pdf" cat output "222222.pdf"
I搜索
ps:如果有人可以找到不错的标题建议,请:)
推荐答案
echo off
setlocal EnableDelayedExpansion
set "list="
for /F "tokens=1,2 delims=_" %%a in ('dir /B *.pdf') do (
if "!last!" neq "%%a" (
if defined list echo pdftk.exe !list:~2!
set "list="
set "last=%%a"
)
set "list=!list!, %%a_%%b"
)
echo pdftk.exe !list:~2!
这篇关于FOR循环将每个迭代以相同的前缀分组在一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文