batch-file - 批处理 - 搜索零件/确切名称并将行从文本文件复制到批处理中作为 var

标签 batch-file automation

以下信息包含在一个文本文件中,并按此格式设置。

/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7

我在已经定义的批处理文件中还有一个名为 %file_name% 的变量。

所以假设它是 %file_name% = V001-video_folder_6.mp4

如您所见,还有一些额外的信息,V001-.mp4

我想使用 var %file_name% 来搜索文本文件并返回整行。在这种情况下,它将返回 /var/www/xxx/html/videos/video_folder_6,然后将此信息放入一个新的变量中,比方说,%folder_path%

我想我会使用 findstr 但是我一直在尝试并没有得到最好的结果。

最佳答案

使用 findstr 的方法的问题是它们很慢,因为它们每次都需要执行 findstr.exe(一个大约 30KB 的文件)。一个更简单/更快的解决方案是在 array 的帮助下仅使用内部 Batch 命令。 .如果要处理的名称数量很大,可以标记两种方法之间的时间差异。

@echo off
setlocal EnableDelayedExpansion

rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
   set "line=%%a"
   for %%b in (!line:/^= !) do set "lastPart=%%b"
   set "folder[!lastPart!]=%%a"
)

set "file_name=V001-video_folder_6.mp4"

rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"

echo Folder path is: %folder_path%

关于batch-file - 批处理 - 搜索零件/确切名称并将行从文本文件复制到批处理中作为 var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29588155/

相关文章:

用于创建文件和日期的 csv 列表的 Windows 批处理文件

android - 即使找不到元素或在 appium for android 中等待,如何继续

java - 这是什么错误 : at com. google.common.base.Preconditions.checkNotNull

for-loop - 解析HTML文件

windows - 批量删除文本文件中的一行?

javascript - 编写脚本来自动化 npm 命令

delphi - 如何加快 Delphi XE2 的文字自动化速度

javascript - 如何在浏览器加载时自动启动 iMacros Javascript

windows - 我不遵循此脚本中发生的数学运算

batch-file - Shell.StdOut.ReadAll 返回的垃圾字符