I am writing a batch file wherein I have read the filenames of all the files in a directory and then look for a pa开发者_如何学Pythonrticular 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.
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% - is the path to my directory/folder.
Thanks.
You can simply use the wildcard that includes account
.
FOR /R %completepath% %%G IN (*account*.csv) DO (
echo %%~nG
)
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
)
Maybe grep can help you?
FOR /F "usebackq delims= " %%m IN (`findstr /c:"-stringtolookfor" "*.csv"`) DO
(
set first_word_in_this_line=%%m
)
精彩评论