2

I need to move some XML files from one directory to another based on whether a particular string is present in it or not(i.e. if the string is present then move, else keep it). Is it possible to do so using a batch script?

1
  • 1
    is the string to be checked in the file or just the file name ? Can you use powershell, it will be a lot easier. Commented Mar 31, 2014 at 12:26

2 Answers 2

1

Use FINDSTR like this to move file fred if it contains the word "hello":

findstr hello fred
if %ERRORLEVEL% == 0 (
   echo Move that puppy
)

If you want to search insensitive to case (lowercase vs uppercase) use FINDSTR /I

Powershell is not available on all Windows versions, by the way, whereas this is.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I am using windows server 2003. Powershell is most probably unavailable here.
Ok, so hopefully this is the correct way to go for you (as this is not Powershell).
0

This should copy the files with string in the filename.

@echo off
for /f "delims=" %%a in ('dir *.xml /b /a-d ^|find /i "string" ') do copy "%%a" "d:\new\folder"

2 Comments

Well, if the keyword is meant to be in the name then perhaps it would be better to do just COPY *string*.xml "targetpath".
@AndriyM Yes, that will work just as well. It was meant to be move I noticed later.

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.