简体   繁体   English

output 的源文件名

[英]Source file name for output

I am trying to create a batch file using PDFtk to burst combine file in a certain directory to the output folder using the source file name (which can vary) as the input file name.我正在尝试使用 PDFtk 创建一个批处理文件,以使用源文件名(可能不同)作为输入文件名将特定目录中的合并文件突发到 output 文件夹。 for example例如

source directory:- D:\Temp\IN destination directory:- D:\Temp\OUT File name:- abcdefgh.pdf (which can vary) desired output file name:- abcdefgh-001.pdf, abcdefgh-002.pdf and so on源目录:- D:\Temp\IN 目标目录:- D:\Temp\OUT 文件名:- abcdefgh.pdf(可能不同) 所需的 output 文件名:- abcdefgh-001.pdf、abcdefgh-002.pdf 等等

My batch file will reside in D:\Script The PDFtk.exe is in D:\PDFtk Server\bin我的批处理文件将位于 D:\Script PDFtk.exe 位于 D:\PDFtk Server\bin

I tried for 1 whole day I can't get the input filename for the output. Can anyone help我试了一整天,但无法获得 output 的输入文件名。有人能帮忙吗

My existing program:-我现有的程序:-

CD D:\Temp\IN

for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (

set prefix=%%~ni )

set outname=%prefix%-%%03d-00.pdf 

path D:\PDFtk Server\bin

pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%

exit

There are multiple errors in your approach the first one is that in a case like this you need local delayed expansion.您的方法存在多个错误,第一个错误是在这种情况下您需要本地延迟扩展。 For an initial % later use it as !对于最初的% ,稍后将其用作! . .

CD D:\Temp\IN

This is ok but would be better if it allowed for /Drive shifting to "quoted drive folder" (beware input folder must NOT in this case be same as output folder with this approach, it is best if output is a sibling as you have done or use a common parent is better.)这没关系,但如果它允许 /Drive 转移到“引用的驱动器文件夹”会更好(注意输入文件夹在这种情况下不能与使用这种方法的 output 文件夹相同,最好是 output 是你所做的兄弟姐妹或使用共同的父母更好。)

CD /D "D:\Temp\IN"

for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (

set prefix=%%~ni )

You have mixed up %%A and %%i they should be the same, tokens is not needed in this case but usebackq is for odd filenames and add quotes, don't need current directory, and ) is way too early.您混淆了 %%A 和 %%i 它们应该是相同的,在这种情况下不需要tokens ,但是 usebackq 用于奇数文件名并添加引号,不需要当前目录,并且)太早了。

setlocal enabledelayedexpansion  
for /f "usebackq delims=="  %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"

next part下一部分

set outname=%prefix%-%%03d-00.pdf  

again best quoted and needs using expansion再次最佳引用和需要使用扩展
set "outname=.prefix!-%%03d-00.pdf"

path D:\PDFtk Server\bin

pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%

better if combined and quoted you don't need to use Current Directory如果合并并引用会更好你不需要使用当前目录

"D:\PDFtk Server\bin\pdftk.exe" ".prefix:.pdf" burst output "D:\Temp\OUT\!outname!"

the terminal bracket goes here终端支架在这里
)

exit

is superfluous是多余的

so all in all所以总而言之

CD /D "D:\Temp\IN"
setlocal enabledelayedexpansion
for /f "usebackq delims==" %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"
set "outname=!prefix!-%%03d-00.pdf" 
"D:\PDFtk Server\bin\pdftk.exe" "!prefix!.pdf" burst output "D:\Temp\OUT\!outname!"
)

The whole command could be simplified to ONE line however to show where cuts can be made整个命令可以简化为一行,但要显示可以进行切割的位置
try this尝试这个

CD /D "D:\Temp"
for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do (
"D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf"
)

the need for usebackq delims== is because of later problems with characters such as filename (1).pdf usebackq delims==的需要是因为后来的字符问题,如filename (1).pdf

so we cannot shorten much more than所以我们不能缩短太多

CD /D "D:\Temp" & for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do ("D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用powershell输出多个.csv文件附加.csv文件名作为源文件夹名称 - Output multiple .csv files appending the .csv file name as the source folder name with powershell 批处理脚本从源文件中读取文件名并将所有出现的路径写入输出文件 - Batch script to read from file name from source file and write path of all occurrences into output file 如何将目录中的所有* .txt文件写入一个文件中,并在输出文件的每一行中包含源文件名? - How to write all *.txt files in a directory into one file with source file name in each line in output file? 根据文件夹和文件名输出 - Output on the basis of folder and file name 如何在bat文件中更改输出文件名? - How to change the output file name in a bat file? 如何捕获文件复制/移动事件的详细信息,例如源文件名 - How to catch the details of the file copy/move event, such as the source file name Windows 7 cmd不会输出文件名 - Windows 7 cmd wont output name of file 如何将cmd输出保存为文件名 - How to save cmd output as file name 从批处理脚本中的.lnk快捷方式获取源.exe文件名? - Getting the source .exe file name from .lnk shortcut in batch script? 由于源文件名是动态的,如何在xcopy中使用变量? - How to use variable in xcopy since source file name is dynamic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM