Avatar of jordanrog
jordanrog
Flag 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.
VB ScriptShell ScriptingWindows Batch

Avatar of undefined
Last Comment
Gerwin Jansen

8/22/2022 - Mon
Gerwin Jansen

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?
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.
Gerwin Jansen

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
jordanrog

ASKER
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”
jordanrog

ASKER
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:
jordanrog

ASKER
@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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
jordanrog

ASKER
so where you have set=succeded should that stay on my script?
Bill Prew

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Gerwin Jansen

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

jordanrog

ASKER
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
Gerwin Jansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question