Script to monitor file for modifications

jordanrog
jordanrog used Ask the Experts™
on
I need some help with s script that will copy a file into a folder when a file has been modified with an exact string. There are two strings that I am looking for and they are in a txt file. Once the file is finished it will have one of the two strings and dump it into one of two folders one for success and one for failure. This will be running on a server 2008 box. placing files to a locally attached drive. Can anyone assist with this.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Hi, so you want to monitor 1 file for 2 strings to appear in the file. Based on which of the 2 you want to move the file to either folder1 or folder2. Is this correct and what do you mean by the file being finished? Is this about the same file and what process or program is writing to it? Does this file always have the same name or is there a pattern in the name?

Author

Commented:
I am monitoring a folder that will have a bunch of username.txt files created then modified by another script. when the username.txt is modified i need it to look for a success or failure  string in the text file. and copy the username.txt to the appropriate folder either success or failure.
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Ok, this batch file should cover the basics:

@echo off
set basefold=c:\temp
set filter=*.txt
set errfold=%basefold%\failure
set succfold=%basefold%\success
set success=Succeeded
set failure=Failed

:loop
rem find files with success string and move if found
for /f %%f in ('findstr /m %success% %basefold%\%filter%') do move %%f %succfold%

rem find files with failure string and move if found 
for /f %%f in ('findstr /m %failure% %basefold%\%filter%') do move %%f %errfold%

rem pause for 10s 
ping -n 1 -w 10000 1.0.0.0 >nul

rem repeat
goto loop

Open in new window

Some variables that you can use to set folders, strings to search for etc. Sample above will look in c:\temp for *.txt files, search for 2 strings (see success and failure) and if found move to succfold or errfold. The script waits for 10 seconds so files can 'finish'. Note that if files are still accessed, the move command will probably fail but when files are not accessed anymore, moving will succeed. Would this work for you? You can test by commenting out the first line and change the 2 'do move' commands to 'do echo move', no files are moved in that case and you'll what would happen.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
I would need to put basefolder = e:\pst

then for set success= put the my success phrase?

and this would search in the txt not the name of the txt right?

Error phrase will be “error occurred in step:”
Success phrase “This mailbox has been exported to the PST fi”

Author

Commented:
FINDSTR: Cannot open mailbox
FINDSTR: Cannot open has
FINDSTR: Cannot open been
FINDSTR: Cannot open exported
FINDSTR: Cannot open to
FINDSTR: Cannot open the
FINDSTR: Cannot open PST
FINDSTR: Cannot open fi
FINDSTR: Cannot open occurred
FINDSTR: Cannot open in
FINDSTR: Cannot open step:

Author

Commented:
@echo off
set basefold=e:\pst
set filter=*.txt
set errfold=%basefold%\failure
set succfold=%basefold%\success
set success=This mailbox has been exported to the PST fi
set failure=error occurred in step:

:loop
rem find files with success string and move if found
for /f %%f in ('findstr /m %success% %basefold%\%filter%') do move %%f %succfold%

rem find files with failure string and move if found
for /f %%f in ('findstr /m %failure% %basefold%\%filter%') do move %%f %errfold%

rem pause for 10s
ping -n 1 -w 10000 1.0.0.0 >nul

rem repeat
goto loop
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
==> I would need to put basefolder = e:\pst

Correct.

==> then for set success= put the my success phrase?

Correct.

==> and this would search in the txt not the name of the txt right?

Correct.

==> Error phrase will be “error occurred in step:”
==> Success phrase “This mailbox has been exported to the PST fi”

See below.

@echo off
set basefold=c:\temp
set filter=*.txt
set errfold=%basefold%\failure
set succfold=%basefold%\success
set success=Succeeded
set failure=Failed

:loop
rem find files with success string and move if found
for /f %%f in ('findstr /m "%success%" "%basefold%\%filter%"') do (
  echo This mailbox has been exported to the PST fi
  move %%f "%succfold%"
)

rem find files with failure string and move if found 
for /f %%f in ('findstr /m "%failure%" "%basefold%\%filter%"') do (
  echo error occurred in step:
  move %%f "%errfold%"
)

rem pause for 10s 
ping -n 1 -w 10000 1.0.0.0 >nul

rem repeat
goto loop

Open in new window

~bp

Author

Commented:
so where you have set=succeded should that stay on my script?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
If you mean this line:

set success=Succeeded

then yes, that must stay.  That merely gives a variable name to the string you want to look for.  And is then used in the code below as in:

for /f %%f in ('findstr /m "%success%" "%basefold%\%filter%"') do (

The "%success%" will be replaced by "Succeeded".

The reason we do this is to make it easy for you to see what things you need to change in the script to adapt it to your specific situation.  This way you just change the SET line to have whatever text you want to search for to indicate success.

~bp
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
Because you are searching for a sentence instead of just a single string, the findstr command has to be modified a little. This will find your “error occurred in step:” and "This mailbox has been exported to the PST fi" and move files accordingly:
::@echo off
set basefold=c:\temp
set filter=*.txt
set errfold=%basefold%\failure
set succfold=%basefold%\success
set success="This mailbox has been exported to the PST fi"
set failure="error occurred in step:"

:loop
rem find files with success string and move if found
for /f %%f in ('findstr /m /c:%success% %basefold%\%filter%') do move %%f %succfold%

rem find files with failure string and move if found 
for /f %%f in ('findstr /m /c:%failure% %basefold%\%filter%') do move %%f %errfold%

rem pause for 10s 
ping -n 1 -w 10000 1.0.0.0 >nul

rem repeat
goto loop

Open in new window

Note that I commented the echo off and removed the echo with the move commands.

Here's some logging, I created a testfile that contains your 'success' string:
...
C:\Temp>rem find files with success string and move if found
C:\Temp>for /F %f in ('findstr /m /c:"This mailbox has been exported to the PST fi" c:\temp\*.txt') do move %f c:\temp\success
C:\Temp>rem find files with failure string and move if found
C:\Temp>for /F %f in ('findstr /m /c:"error occurred in step:" c:\temp\*.txt') do move %f c:\temp\failure
C:\Temp>rem pause for 10s
C:\Temp>ping -n 1 -w 10000 1.0.0.0  1>nul
C:\Temp>rem repeat
C:\Temp>goto loop
C:\Temp>rem find files with success string and move if found
C:\Temp>for /F %f in ('findstr /m /c:"This mailbox has been exported to the PST fi" c:\temp\*.txt') do move %f c:\temp\success
C:\Temp>move c:\temp\okfile.txt c:\temp\success
        1 file(s) moved.
C:\Temp>rem find files with failure string and move if found
C:\Temp>for /F %f in ('findstr /m /c:"error occurred in step:" c:\temp\*.txt') do move %f c:\temp\failure
...

Open in new window

Author

Commented:
i get this but if i put in a folder called new in temp it moves that folder any ideas?


C:\temp>set basefold=c:\temp

C:\temp>set filter=*.txt

C:\temp>set errfold=c:\temp\failure

C:\temp>set succfold=c:\temp\success

C:\temp>set success="This mailbox has been exported to the PST fi"

C:\temp>set failure="error occurred in step:"

C:\temp>rem find files with success string and move if found

C:\temp>for /F %f in ('findstr /m /c:"This mailbox has been exported to the PST
fi" c:\temp\*.txt') do move %f c:\temp\success

C:\temp>move c:\temp\New c:\temp\success
The system cannot find the file specified.

C:\temp>rem find files with failure string and move if found

C:\temp>for /F %f in ('findstr /m /c:"error occurred in step:" c:\temp\*.txt') d
o move %f c:\temp\failure

C:\temp>rem pause for 10s

C:\temp>ping -n 1 -w 10000 1.0.0.0  1>nul


but if i put a folder in with named new in temp then it moves that folder what am i missing?


::@echo off
set basefold=c:\temp
set filter=*.txt
set errfold=%basefold%\failure
set succfold=%basefold%\success
set success="This mailbox has been exported to the PST fi"
set failure="error occurred in step:"

:loop
rem find files with success string and move if found
for /f %%f in ('findstr /m /c:%success% %basefold%\%filter%') do move %%f %succfold%

rem find files with failure string and move if found
for /f %%f in ('findstr /m /c:%failure% %basefold%\%filter%') do move %%f %errfold%

rem pause for 10s
ping -n 1 -w 10000 1.0.0.0 >nul

rem repeat
goto loop
Topic Advisor
Most Valuable Expert 2016
Commented:
What do you mean by "put in a folder called new in temp"? findstr is only searching in %basefold% for files that match %filter% - how do you get it to match one of the 2 strings in the new folder? findstr is not searching recursively.

Your requirement is to search through txt files in one folder, right?

But: you may have matching file called "new file.txt" in that case the move command will probably try to move new indeed... If so, change the 2 for lines this way:

for /f "tokens=*" %%f in ('findstr /m /c:%success% %basefold%\%filter%') do move "%%f" "%succfold%"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial