在文件名中查找字符串 [英] Finding string in a file name
问题描述
我正在写一个批处理文件,其中我已读取目录中所有文件的文件名,然后在文件名中查找特定的字符串.我可以获取文件名,但无法找到在文件名中搜索特定字符串的方法.
I am writing a batch file wherein I have read the filenames of all the files in a directory and then look for a particular string in the filenames. I am able to get the filenames but unable to find a way to search for a particular string in the filename.
例如该文件的名称为abc_account_march_2010.csv.我必须检查文件名中是否包含"account"一词,然后再重命名该文件.
e.g. Name of the file is abc_account_march_2010.csv. I have to check if the filename contains the word "account" in it or not if it does then rename the file.
这是我为获取文件名所做的事情.
this is what I have done to get the file name.
FOR /R %completepath% %%G IN (*.csv) DO (
echo %%~nG
)
%completepath%-是我的目录/文件夹的路径.
%completepath% - is the path to my directory/folder.
谢谢.
推荐答案
您可以简单地使用包含account
的通配符.
You can simply use the wildcard that includes account
.
FOR /R %completepath% %%G IN (*account*.csv) DO (
echo %%~nG
)
如果您仍然需要处理所有* .csv文件并另外重命名名称中带有"account"的文件,那么可以通过以下方法进行检查:
If you still need to process all *.csv files and additionally rename those, that have 'account' in their names, then here's how you could check that:
FOR /R %completepath% %%G IN (*.csv) DO call :process "%%~nG"
GOTO :EOF
:process
SET %name%=%~1
SET chkname=%name:*account=?%
IF "%chkname:~0,1%"=="?" (
ECHO %name% -- this is account file!
) ELSE (
ECHO %name% -- this is NOT account file
)
这篇关于在文件名中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!