首页 > 解决方案 > 将带空格的文件名批量输出到不带引号的文件

问题描述

因此,我编写了一个脚本作为批处理文件,它使用 FFmpeg 来“连接”我硬盘上的多个视频文件。脚本如下。

@echo off
title Printing video info...
(for %%i in (
"%USERPROFILE%"/Dropbox/Video1.MKV
"%USERPROFILE%"/Dropbox/Video2.MKV
S:/Exports/Video3.MKV
../../video/Video4.mkv
) do ( if exist "%%i" echo file '%%i' )) > "%~n0.txt"
type "%~n0.txt"
title Copying to compiled video...
"C:\Program Files\ffmpeg\bin\ffmpeg.exe" -hide_banner -f concat^
                  -safe 0 -y -i "%~n0.txt" -c copy "%~n0.mkv"

这里的问题是计算机上的用户名名称中有空格,因此脚本不起作用。如果我把引号加上%USERPROFILE%,那么批处理脚本会检测到该文件,但是批处理脚本也会将引号放入输出 TXT 文件中,这会导致 FFmpeg 在遇到该文件时失败。

脚本输出到的文本文件的内容应该是:

file 'C:\Users\Name/Dropbox/Video1.MKV'
file 'C:\Users\Name/Dropbox/Video2.MKV'
file 'S:/Exports/Video3.MKV'
file '../../video/Video4.mkv'

标签: batch-fileffmpeg

解决方案


引用所有文件路径并将~修饰符正确应用于%%i如下:

(for %%i in (
    "%USERPROFILE%/Dropbox/Video1.MKV"
    "%USERPROFILE%/Dropbox/Video2.MKV"
    S:/Exports/Video3.MKV
    ../../video/Video4.mkv
) do ( if exist "%%~i" echo file '%%~i' )) > "%~n0.txt"

推荐阅读