批处理语法 For Loop If Exists

发布于 2024-12-28 10:41:54 字数 1424 浏览 0 评论 0原文

尝试创建批处理以将 .WTV 文件转换为 mpg 文件。我想每晚运行一次下面的代码来转换每日录制内容以在我的媒体服务器上使用。我关心语法的部分是 for 循环行。我只想在尚未转换和移动的文件上运行脚本。如果发生错误,我无法删除原始文件。最后一个移动循环是获取第一次尝试时无法移动的所有文件。

输入文件看起来像: show.wtv

文件中的输出是 show.wtv.mpg (我同意)

我不知道如何编写 for 循环以获得所需的功能。任何帮助表示赞赏。

最初的想法来自 http://ireckon.net/ 2009/10/converting-wtv-to-mpg-in-windows-7/

@echo off

set recordedtv="D:\Recorded TV\"
set destfolder="D:\Videos\Recorded TV\"
set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
set wtvconv="C:\Windows\ehome\WTVConverter.exe"

for %%f in (%recordedtv%*.wtv) Do If Not Exist "%destfolder%%%f.mpg" (
%wtvconv% "%%f" "%%f.dvr-ms"
%ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
del "%%f.dvr-ms"
move "%%f.mpg" %destfolder%
)

for %%f in (%recordedtv%*.mpg) Do move "%%f" %destfolder%

更新:最终使用了这个

@echo off

set "recordedtv=D:\Recorded TV\"
set "destfolder=D:\Videos\Recorded TV\"
set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
set wtvconv="C:\Windows\ehome\WTVConverter.exe"

for %%f in ("%recordedtv%*.wtv") do If Not Exist "%destfolder%%%~nxf.mpg" (
%wtvconv% "%%f" "%%f.dvr-ms"
%ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
del "%%f.dvr-ms"
move "%%f.mpg" "%destfolder%"
)

for %%f in ("%recordedtv%*.mpg") Do move "%%f" "%destfolder%"

Trying to create a batch to convert .WTV files to mpg files. The code below I want to run once a night to convert the daily recordings for use on my media server. The section I am concerned about syntax is the for loop line. I only want to run the script on the files that have not been converted and moved. I cant delete the original in case and error occurs. The last move loop is to get all the files it couldn't move on the first try.

the input files look like : show.wtv

the output in the file is show.wtv.mpg (which I'm ok with)

I am not sure how to write that for loop to get the desired functionality. Any help is appreciated.

Original idea came from http://ireckon.net/2009/10/converting-wtv-to-mpg-in-windows-7/

@echo off

set recordedtv="D:\Recorded TV\"
set destfolder="D:\Videos\Recorded TV\"
set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
set wtvconv="C:\Windows\ehome\WTVConverter.exe"

for %%f in (%recordedtv%*.wtv) Do If Not Exist "%destfolder%%%f.mpg" (
%wtvconv% "%%f" "%%f.dvr-ms"
%ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
del "%%f.dvr-ms"
move "%%f.mpg" %destfolder%
)

for %%f in (%recordedtv%*.mpg) Do move "%%f" %destfolder%

UPDATE: Ended up using this

@echo off

set "recordedtv=D:\Recorded TV\"
set "destfolder=D:\Videos\Recorded TV\"
set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
set wtvconv="C:\Windows\ehome\WTVConverter.exe"

for %%f in ("%recordedtv%*.wtv") do If Not Exist "%destfolder%%%~nxf.mpg" (
%wtvconv% "%%f" "%%f.dvr-ms"
%ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
del "%%f.dvr-ms"
move "%%f.mpg" "%destfolder%"
)

for %%f in ("%recordedtv%*.mpg") Do move "%%f" "%destfolder%"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2025-01-04 10:41:54

如果您计划将其他部分添加到存储在变量中的路径,请不要存储带有引号的路径。只需保留双引号或像这样放置它们:

...
set "recordedtv=D:\Recorded TV\"
set "destfolder=D:\Videos\Recorded TV\"
...

这样您就可以将路径与名称/掩码连接起来,而不会导致语法错误。 ,您的路径不是用双引号分隔的,因此您需要根据需要在评估它们的位置对它们进行分隔,因此,例如,for 循环将如下所示:

for %%f in ("%recordedtv%*.wtv") do ...
...
for %%f in ("%recordedtv%*.mpg") do ...

请注意 其他地方也是如此。例如,在脚本中的 move 命令中,目标路径也必须被分隔:

move "%%f.mpg" "%destfolder%"

至于文件存在性检查,这是通过 if exit 命令完成的

if exist target_file (
...    // do whatever you need to do in case the file exists
)

target_file 位可以是文件、掩码或目录(在后一种情况下,它应以 \ 结尾)。

更新

我想我现在可以看到您的脚本存在一个特定问题。这部分

for %%f in (%recordedtv%*.wtv) Do If Not Exist "%destfolder%%%f.mpg"

是错误的,因为它将目标文件夹和 .wtv 文件的完整路径放在一起,并将生成的字符串用作单个路径。因此 "%destfolder%%%f" 的计算结果如下:

"D:\Videos\Recorded TV\D:\Recorded TV\somename.wtv.mpg"

我只是不确定额外的引号,但这不是这里的重点。我想你已经看到了主要问题。要解决此问题,您只需从 D:\Recorded TV\somename.wtv.mpg 中提取文件名和扩展名,这是使用组合的 ~nx 说明符完成的( n 代表“名称”,x 代表“扩展名”):(

for %%f in ("%recordedtv%*.wtv") Do If Not Exist "%destfolder%%%~nxf.mpg"

请注意,存储文件夹路径时仍应不使用双引号,以便正确评估它们复杂的表达方式。)

If you are planning to add other parts to paths stored in variables, do not store the paths with the surrounding quotation marks. Just leave the double quotes out or put them like this:

...
set "recordedtv=D:\Recorded TV\"
set "destfolder=D:\Videos\Recorded TV\"
...

That way you'll be able to concatenate the paths with names/masks without causing syntax errors. Note that your paths are not delimited with double quotes, so you'll need to delimit them where they are evaluated, as necessary, so, for instance, the for loops would look like this:

for %%f in ("%recordedtv%*.wtv") do ...
...
for %%f in ("%recordedtv%*.mpg") do ...

Pay attention to other places as well. For example, in the move command in your script the target path will also have to be delimited:

move "%%f.mpg" "%destfolder%"

As for file existance check, that is done with the if exist command:

if exist target_file (
...    // do whatever you need to do in case the file exists
)

The target_file bit can be a file, a mask, or a directory (in the latter case it should end with \).

UPDATE

I think I can see now a particular problem with your script. This part

for %%f in (%recordedtv%*.wtv) Do If Not Exist "%destfolder%%%f.mpg"

is wrong in that it puts your destination folder and the full path to a .wtv file together and uses the resulting string as a single path. So "%destfolder%%%f" gets evaluated to something like this:

"D:\Videos\Recorded TV\D:\Recorded TV\somename.wtv.mpg"

I'm only not sure about extra quotation marks, but that is not the point here. I guess you can see the main problem. To resolve it, you need to extract only file name and extension from D:\Recorded TV\somename.wtv.mpg, which is done using the combined ~nx specifier (n standing for ‘name’ and x for ‘extension’):

for %%f in ("%recordedtv%*.wtv") Do If Not Exist "%destfolder%%%~nxf.mpg"

(Note that the folder paths should still be stored without surrounding double quotes in order for them to be evaluated correctly in complex expressions.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文