Link to home
Start Free TrialLog in
Avatar of jordanrog
jordanrogFlag for United States of America

asked on

Script to monitor file for modifications

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.
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

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?
Avatar of jordanrog

ASKER

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.
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.
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”
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:
@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
Avatar of Bill Prew
Bill Prew

==> 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
so where you have set=succeded should that stay on my script?
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
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

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
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial