1

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.

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.

3 Answers 3

4

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
)
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe grep can help you?

Comments

0
FOR /F "usebackq delims= " %%m IN (`findstr /c:"-stringtolookfor" "*.csv"`) DO 
(
   set first_word_in_this_line=%%m
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.