Find string in txt files then do IF ELSE?

April 19, 2011 at 14:42:41
Specs: Windows Vista
Hi there

Please help me with this problem with my batch file. I have a folder of txt files and need to search each txt file contents and find a specific string. If it has this string I want to move the file to folder 1 and if it doesnt move it to folder 2.

I found some code which does the searching and can move the txt file to folder 1 (below). Im puzzled how to move the 'false' files to folder 2. I thought maybe to add somewhere a IF ELSE condition to do the moving?

FOR /f "tokens=*" %%a IN ('FINDSTR /i /m "12345678" "C:\Users\admin\Desktop\test\*.txt"') DO (
	IF "%ERRORLEVEL%"=="0" MOVE %%a "C:\Users\admin\Desktop\test\folder1\">Nul
)

I dont specifically have to use this code so if there are alternatives thats great.

Please let me know if you need more info or im not clear enough. Thanks so much!


See More: Find string in txt files then do IF ELSE?

Report •

#1
April 19, 2011 at 17:26:58
findstr /m only lists filenames of matched files so you don't need to check the errorlevel.

The easy solution would be to move the files that remain after the loop.

FOR /f "tokens=*" %%a IN ('FINDSTR /i /m "12345678" "C:\Users\admin\Desktop\test\*.txt"') DO (
	MOVE "%%a" "C:\Users\admin\Desktop\test\folder1\" >Nul
)
move "C:\Users\admin\Desktop\test\*.txt" "C:\Users\admin\Desktop\test\folder2"


Report •

#2
April 20, 2011 at 02:07:21
Thanks for the reply. The folder which contains the txt files gets constantly updated (sometimes every second) with new txt files. The problem I thought your solution may have is that if during the FOR loop a new txt file is added when it comes to the MOVE to folder 2 there may be some instances where txt files are moved without being checked.
Does that make sense? Or does the whole process happen so quick this will not happen?

Report •

#3
April 20, 2011 at 02:20:17
I didn't realize that the folder was constantly being updated, it could definately cause problems with the above script.

I think this should work:

rem untested
FOR %%a IN ("C:\Users\admin\Desktop\test\*.txt"') DO (
       >nul FINDSTR /i "12345678" "%%~a"
        if errorlevel 1 (
            rem The string was not found
            > nul MOVE "%%~a" "C:\Users\admin\Desktop\test\folder2"
        ) else (
            rem The string is in the file
            >nul MOVE "%%~a" "C:\Users\admin\Desktop\test\folder1"
        )
)


Report •

Related Solutions

#4
April 20, 2011 at 02:44:15
Hi Judago

Sorry I didnt mention that in my first post. Thanks for the code...I tested it but when I run the batch file nothing happens and the txt files dont move.

Am I doing something wrong? Any suggestions? Thanks!


Report •

#5
April 20, 2011 at 03:23:04
Single character error ;)

rem untested
FOR %%a IN ( "C:\Users\admin\Desktop\test\*.txt" ) DO (
       >nul FINDSTR /i "12345678" "%%~a"
        if errorlevel 1 (
            rem The string was not found
            > nul MOVE "%%~a" "C:\Users\admin\Desktop\test\folder2"
        ) else (
            rem The string is in the file
            >nul MOVE "%%~a" "C:\Users\admin\Desktop\test\folder1"
        )
)


Report •

#6
April 20, 2011 at 03:43:11
Excellent! Doh, I should have noticed that too. Thanks!

I have another problem with a similar issue about a folder being updated constantly. I dont know if I should start a new thread or can I ask here? If this is not allowed please let me know and I will start a new one.

The task I am trying to achieve is COPY contents from 'test' folder to 'folder1' and then MOVE the contents from 'test' to 'folder2'. The problem is similar in that im worried that as it finishes copying to folder1, a new txt file is added and then it would be moved to folder2 meaning that folder1 would never get this new txt file.

So what I thought we could do is to COPY and MOVE individual files at the same time so there would never be a chance that folder1 would not be an exact replica to folder2. Does that make sense?

I can do the basic copy and move functions but this problem is puzzling me!? Cheers!


Report •

#7
April 20, 2011 at 04:11:09
I have another problem with a similar issue about a folder being updated constantly. I dont know if I should start a new thread or can I ask here? If this is not allowed please let me know and I will start a new one.

Generally speaking, if it's closely related then you can continue the thread, but you can also start a new one. Sometimes a new thread can get other responders and be quicker because of sleep/work/other activities and the varying time zones.

**************************************************************

Is this conditional of the files contents again or just every file?

If it's every file then I would suggest a move then copy, as long as folder2 isn't being constantly updated it removes the issue of the folders containing different files.

move "w:\hat\ever\test*.txt" "w:hat\ever\folder2"
copy "w:hat\ever\folder2\*.txt" "w:hat\ever\folder1"

You could also use del with a wildcard (i.e. del "w:hat\ever\folder1\*.txt") to ensure there isn't any files already in folder1.

If it's conditional again I need to know what the condition is.


Report •

#8
April 20, 2011 at 05:50:25
Nope its not conditional but does have a txt wildcard.

I forgot to mention that folder2 may contain other files, so i couldnt use that one...sorry about that.

Any alternatives?


Report •

#9
April 20, 2011 at 16:13:46
Ok then I guess using a for loop should sort it out:

for %%a in ( "w:\hat\ever\test*.txt" ) do (
    copy "%%~a" "w:\hat\ever\folder1"
    move "%%~a" "w:\hat\ever\folder2"
)


Report •

Ask Question