简体   繁体   English

如何查找文件中的任何一个单词是否在一个很长的字符串中,该字符串在 CMD 批处理中经常更改

[英]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.

相关问题 如何在cmd批处理文件中运行另一个命令? - How do I run another command in a cmd batch file? 如何将变量放入批处理文件中的 FOR /F cmd 中? - How do I put a variable into a FOR /F cmd in a batch file? 如何在 Windows cmd 中找到不属于管理员的文件? - How do i find a file in Windows cmd that is not owned by Administrator? 如何检查字符串是否包含单词批处理文件 - how to check if string contain words batch file 如何在Windows批处理文件中的多行中拆分长字符串,同时仍允许cmd.exe将其解析为单个参数? - How to break up a long string in multiple lines in a windows batch file while still allow cmd.exe to parse it as a single argument? 除了当前从批处理运行的cmd.exe之外,如何杀死所有cmd.exe? - how do I kill all cmd.exe except the one currently running from batch? 如何防止批处理文件输出任何错误? - How do I prevent a batch file from outputting any error? 在1个文件中查找最频繁出现的字符串 - Find most frequently reoccurring string in 1 file 如何避免在此批处理文件中重复启动cmd.exe来运行AWS CLI命令? - How do I avoid repeatedly starting cmd.exe in this batch file to run AWS CLI commands? 如何从批处理文件运行充当cmd实例的子进程的python脚本? - How do I run a python script, from a batch file, that acts as a subprocess of the cmd instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM