Batch to Create a Protocol of File Delivery

Microsoft Acad upg-vc windows 2000 profe...
March 4, 2010 at 15:35:22
Specs: Windows 2000
Hi there,

I was wondering if is possible to create a batch that can indentify when a file or files is or are moved to a folder called for example REOP01, than execute commands for each one that:

1 - Check if there is a log file named with the current year and month (yyyymm) of the system, if there is go to step 3 else go to step 2;
2 - Create a file log named with the current year and month (yyyymm) of the system;
3 - Check if the text contain the file name:
3.1 - If there is no then record into the file log the file name, file extension, date and hour of the system, each line for each file else do nothing.

PS.: My intention is to create a kind of protocol to confirm that a file or file arrived in a folder and organize the information in logs according the current month and year of system.

I hope it is possible.
My work depend of this!


See More: Batch to Create a Protocol of File Delivery

Report •


#1
March 4, 2010 at 18:08:35
Why not just run a script every minute to do this? Do you really need higher precision?

Report •

#2
March 5, 2010 at 01:19:44
I'm not 100% sure what you mean, but this sounds not too difficult. From what I understand, you want to log the name of the file itself, when it is moved, so that you can check the logfile later ? This, instead of relying on the presence of the file itself.

I like to work in functions, so I would start with this (this is just to actually do the move, BUT forward the info to the log - there is no checking involved here, since that can be done in another script):

(untested)

@echo off

call getdt

set logfile=C:\mylog%dt_year%%dt_month%.log

set file=%1
set target=%2
if "%2"=="" goto :EOF

if exist %target%\%file% goto :EOF
if not exist %file% goto :EOF

echo Moved file %file% on %date% %time% to %target%>> %logfile%
move %file% %target%



Report •

#3
March 5, 2010 at 16:28:30
Sorry for bothering, but despite modifying it continue not working, I thing there is a problem with the lines I've marked, may is because I use Windows 2k. I've tested using the cmd.exe and when I use echo file or target it shows exactly %1 or %2. Could you explain me how this lines should work and show me what can be changed?

Thank you for your attention until now.

@echo off

::call getdt

set dt_year=%date:~10,4%
set dt_month=%date:~7,2%

set logfile="C:\mylog%dt_year%%dt_month%.log"

set file=%1 <-----
set target=%2 <-------
if "%2"=="" goto :EOF <----

if exist %target%\%file% goto :EOF
if not exist %file% goto :EOF

echo Moved file %file% on %date% %time% to %target% >> %logfile%
move %file% %target%


Report •

Related Solutions

#4
March 6, 2010 at 03:52:30
It should work ... but, try this:

@echo ON
setlocal

::call getdt

set dt_year=%date:~10,4%
set dt_month=%date:~7,2%

set logfile="C:\mylog%dt_year%%dt_month%.log"

set file=%1
set target=%2
if "%2"=="" goto :EOF

if exist %target%\%file% goto :EOF
if not exist %file% goto :EOF

echo Moved file %file% on %date% %time% to %target% >> %logfile%
move %file% %target%


Report •

#5
March 6, 2010 at 11:15:30
I'm so sorry for bothering again, but I'm having difficult to develop the getdt and pass parameters to %1 and %2.
Could give me a example that I can have an idea?

Thanks for your attention!


Report •

#6
March 6, 2010 at 14:26:36
Ignore the GETDT, it's my custom solution. You already have placed alternative code there.

To pass elements, just do this (assuming your script is called C:\myscript.cmd):

C:\> myscript.cmd test.txt c:\myfolder


Report •

#7
March 6, 2010 at 16:31:30
I understood, thank you. I've adapted a batch that can do what I need. Basically this code verifies if there are files in a folder and take information always the recently file, I mean if there are two files the bacth record the information just the new one to the log file. I'm having a problem because every time the batch is executed it records information of the same file more than once if there is not a recently one.
I was wondering if you could know a way to improve this code to avoid the batch repeat the same file in the log file.
I'm really greateful for the tips!

@echo off
setlocal
set srcDir=
set destdir=
set dt_year=
set dt_month=
set lastlog=
set logfile=
set lastmod=
set srcDir=D:\teste\destiny
set destdir=D:\teste\backup

pushd "%srcDir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof else goto verify

:verify
set dt_year=%date:~10,4%
set dt_month=%date:~7,2%

pushd "%destdir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastlog=%%a
if "%lastlog%"=="" goto different
set logfile=mylog%dt_year%%dt_month%.log
if "%lastlog%"=="%logfile%" goto same else goto different

:same
echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\%lastlog%
goto eof

:different
echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\mylog%dt_year%%dt_month%.log
goto eof

:eof


Report •

#8
March 7, 2010 at 03:17:52
When I look at the code, it looks OK ... apart from this line :

if "%lastmod%"=="" echo Could not locate files.&goto :eof else goto verify

I cannot directly link the behaviour you mention, to what I could expect with the above line of code, but by lack of other ideas ... have a try.

When I try similar code, it also behaves bad :

D:\Temp>if "1"=="1" echo no & echo no again else echo yes
no
no again else echo yes

D:\Temp>

I guess the presence of both & and ELSE and ECHO is a bit too much for the computer to make up the same order to break up this command. I would advise to only use & when handy (not in this case) and to use ELSE only in simple commands (again, not this case), or use the brackets (not my favourite, but it adds clarity):

IF EXIST filename. (
del filename.
) ELSE (
echo filename. missing.
)


Report •

#9
March 7, 2010 at 07:55:28
Thank you tcv, your tip helped me to improve the code, actually it works, however I'd like to avoid that the same recent file can be record at the log file, I mead I want to avoid redundance.

As you can see, the code creats a file.log according to the current month and year of system and if it changes it creates another file.log with the current month and year.

A need just a test that could identify if the has already written in the log.file as the example below:

1-Code JobProtocol.bat:

@echo off
setlocal
set srcDir=
set destdir=
set dt_year=
set dt_month=
set lastlog=
set logfile=
set lastmod=
set srcDir=D:\teste\destiny
set destdir=D:\teste\backup

pushd "%srcDir%"
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do (set lastmod=%%a)
if "%lastmod%"=="" (goto :eof) else (goto verify)

:verify
set dt_year=%date:~10,4%
set dt_month=%date:~7,2%

pushd "%destdir%"
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do (lastlog=%%a)
if "%lastlog%"=="" goto different
set logfile=mylog%dt_year%%dt_month%.log
if "%lastlog%"=="%logfile%" (goto same) else (goto different)

:same
(echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\%lastlog%) & (goto eof)

:different
(echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\mylog%dt_year%%dt_month%.log) & (goto eof)

:eof

2-Result in the mylog201003.log:

Moved file teste2.fac on ter 30/03/2010 23:14:17,68 to D:\teste\destiny
Moved file teste2.fac on ter 30/03/2010 23:14:51,54 to D:\teste\destiny
Moved file teste2.fac on qui 01/04/2010 23:32:06,23 to D:\teste\destiny
Moved file teste2.fac on dom 07/03/2010 12:23:15,46 to D:\teste\destiny
Moved file teste2.fac on dom 07/03/2010 12:23:34,31 to D:\teste\destiny
Moved file teste2.fac on dom 07/03/2010 12:27:22,71 to D:\teste\destiny
Moved file teste2.fac on dom 07/03/2010 12:29:15,48 to D:\teste\destiny
Moved file teste2.fac on dom 07/03/2010 12:30:35,09 to D:\teste\destiny


While I execute the program it records the file information, even though it's already happened before.


Report •

#10
March 7, 2010 at 11:58:41
If you're actually moving the file, and not copying it, you should not be getting the same file repeatedly.
but i don't see where you are doing either "move" or copy operation, just the logging.
is this the whole script? or just the logging part?
also this needs "set", i think:
...%%a in ('dir/b/a-d/od') do (lastlog=%%a)
to:
...%%a in ('dir/b/a-d/od') do (SET lastlog=%%a)

Report •

#11
March 7, 2010 at 13:42:00
This is the whole script, I correct the mistake but even though if there is new file that the code record in the log file, when I execute the batch again it doesn't identify if the file has already been recorded in the log file and record it again. I need a way to compare the last file recorde in the log file when the batch find the recent file in the folder. Actually the batch don't have to copy or move a file but always identify when there is a new file in folder and record information of it in the log file once.

Report •

#12
March 7, 2010 at 14:26:24
ok, i had to rework it so i could see the logic more clearly.
(everyone has their own programming "style", and i have a hard time interpreting other people's styles clearly).
I always try to minimize "verbiage" and redundancy, and i like to place most of the non-volatile stuff at the top.
I also have a hard time with ambiguous variable names, so i changed them. I apologize for this and i hope you can recognize the variables from the context of their usage.

@echo off & setlocal
set srcDir=D:\teste\destiny
set destdir=D:\teste\backup

:verify
set dt_year=%date:~10,4%
set dt_month=%date:~7,2%
set logfile=mylog%dt_year%%dt_month%.log

pushd "%srcDir%"
:: this gets the latest filename in the source directory
call :getlast
if "%last%" equ "" goto :eof
set lastsource=%last%

pushd "%destdir%"
:: this gets the latest logfilename created in the log directory
call :getlast
set lastlogfile=%last%
:: this gets the last entry, from the last line in last logfile
for /f "tokens=3" %%a in (%lastlogfile%) do set lastentry=%%a
if "%lastentry%"=%lastsource% goto :eof
set logstring=Moved file %lastsource% on %date% %time% to %srcDir%
>> %destdir%\%llogfile% echo %logstring%
goto :eof
getlast:
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do set last=%%a
::----------- end script
hope this will help you, lemme know if it bombs.


Report •

#13
March 8, 2010 at 15:00:36
Unfortunately it didn't work, the problem could the line mentioned below:

getlast:
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do set last=%%a
When you call the function getlast it doesn't return to continue doing the others lines it simply go to end and I have doubts if the function verify is being executed.

I'm sending the solution I found today at work if somebody need do something like this:

@echo off & setlocal

set srcDir=D:\teste\destiny
set destdir=D:\teste\backup
pushd "%srcDir%"
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do (set lastmod=%%a)
set /p lastfile=<D:\teste\backup\NAMEfile
echo %lastfile%
echo %lastmod%
if %lastmod%==%lastfile% goto eof
if "%lastmod%"=="" (goto eof) else (goto verify)

:verify
set dt_year=%date:~10,4%
set dt_month=%date:~7,2%
pushd "%destdir%"
for /f "tokens=* delims= " %%a in ('dir/b/a-d/od') do (set lastlog=%%a)
if "%lastlog%"=="" goto different
set logfile=mylog%dt_year%%dt_month%.log
if "%lastlog%"=="%logfile%" (goto same) else (goto different)

:same
(echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\%lastlog%) & (goto compare)

:different
(echo Moved file %lastmod% on %date% %time% to %srcDir% >> D:\teste\backup\mylog%dt_year%%dt_month%.log) & (goto compare)

:compare
DEL /Q NAMEfile
echo %lastmod% > NAMEfile

:eof


Report •


Ask Question