[英]How do I find if any one of the words in a file is in a very long string, which changes frequently in CMD batch
I have this filename which gets dropped into a directory on a windows share, where it needs to be located into the right location on the users' local machines, depending on if the filename has one of the keywords in it or not.我有这个文件名,它被放入 windows 共享的目录中,它需要位于用户本地计算机上的正确位置,具体取决于文件名中是否包含关键字之一。
For instance:例如:
z:\mailbox\in\Very_but#very-very!long#filenameWITH-keyword+in^it
where z:
is accessible from a set of win-7 or win-10 machines.其中z:
可从一组 win-7 或 win-10 机器访问。 And on these machines, there are these two directories:在这些机器上,有这两个目录:
c:\incoming\special
c:\incoming\regular
if the filename has one of the keywords embedded in it, it needs to be copied into the c:\incoming\special
folder, if not it needs to go into c:\incoming\regular
folder.如果文件名中嵌入了其中一个关键字,则需要将其复制到c:\incoming\special
文件夹中,否则需要将 go 复制到c:\incoming\regular
文件夹中。
my keywords are in a file, say, c:\keywords.txt
at any given time (checked every 5 minutes) there may be no files, or only one file in the z:\mailbox\in
directory.我的关键字在一个文件中,例如c:\keywords.txt
在任何给定时间(每 5 分钟检查一次)可能没有文件,或者z:\mailbox\in
目录中只有一个文件。
so, this is what I come up for, which is not working: (the batch file assumes there is a file to process, I haven't figured what to do, if there isn't one, yet)所以,这就是我想出来的,但它不起作用:(批处理文件假设有一个文件要处理,我还没想好要做什么,如果还没有的话)
dir /b z:\mailbox\in > tmp.out
set /p file=<tmp.out
del tmp.out
set keyword_found_flag=0
for /F "tokens=*" %%keyword in (c:\keywords.txt) do (
echo %file% | find /i "%%keyword"
if errorlevel=0 set keyword_found_flag=1
)
The errorlevel is always zero regardless if the keyword is found or not无论是否找到关键字,错误级别始终为零
IF keyword_found_flag=1 (
copy z:\mailbox\in\%file% c:\incoming\special
) ELSE (
copy z:\mailbox\in\%file% c:\incoming\regular
)
I am not sure what to do here.我不确定在这里做什么。 Any help is appreciated任何帮助表示赞赏
Why not use operator ( &&
) to set your flag if execution is match?如果执行匹配,为什么不使用运算符 ( &&
) 设置标志?
echo %file% | find /i "%%keyword" >nul && set keyword_found_flag=1
You also don't need create/delete tmp.out file for your process, use double for to for the file name in z:\mailbox\in and, and use another for to check file contains in c:\keywords.txt with file names in z:\mailbox\in...您也不需要为您的进程创建/删除 tmp.out 文件,使用 double for to 作为 z:\mailbox\in 中的文件名,并使用另一个 for 检查 c:\keywords.txt 中包含的文件z:\mailbox\in 中的文件名...
@echo off
set "keyword_found_flag=" && for /f tokens^=* %%i in ('dir /on /b /a:-d "z:\mailbox\in\*.*"
')do for /F tokens^=* %%K in ('type c:\keywords.txt')do echo="%%~i"| find /i "%%~K" && (
set "keyword_found_flag=1") || (set "keyword_found_flag=0")
Or...或者...
@echo off
set "keyword_found_flag=" && for /f tokens^=* %%i in ('dir /on /b /a: -d "z:\mailbox\in\*.*"
')do for /F tokens^=* %%K in ('type c:\keywords.txt')do echo="%%~i"|find /i "%%~K" >nul && (
set "keyword_found_flag=1" && copy /v "%%~dpnxi" "c:\incoming\special\" ) || (
set "keyword_found_flag=0" && copy /v "%%~dpnxi" "c:\incoming\regular\" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.