1.

Solve : files listed added to folder during last week?

Answer» <html><body><p>Hi All<br/><br/>I have rather very difficult task to accomplish. Can you help me and prepare script as *.bat. That script should check folder (it will be network path) and list all files created within last week in that folder. Next to file name should be creation date. <br/>Script should list results on <a href="https://interviewquestions.tuteehub.com/tag/screen-25632" style="font-weight:bold;" target="_blank" title="Click to know more about SCREEN">SCREEN</a> and also create txt fog file each time script is running.<br/><br/>Is possible to create such script?<br/>Unfortunatelly my programming skills are not so big therefore if possible please advice in more details <br/><br/>Thank you in advance.<br/><br/>BR<br/>MichalWill this do ?<br/> Code: <a>[Select]</a>echo off<br/><br/>REM create the vb script<br/>&gt;evaluate.<a href="https://interviewquestions.tuteehub.com/tag/vbs-3852369" style="font-weight:bold;" target="_blank" title="Click to know more about VBS">VBS</a> echo Wscript.echo eval(WScript.Arguments(0))<br/><br/>REM extract todays date less 7 days<br/>for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A<br/><br/>REM tidy<br/>del evaluate.vbs<br/><br/>REM break up the date into constituent parts<br/>REM Note - the date is parsing UK format dates,<br/>REM        swap the MM and DD in the lines below<br/>REM        for US format<br/><br/>set MM=%OldestDate:~3,2%<br/>set DD=%OldestDate:~0,2%<br/>set YYYY=%OldestDate:~6,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>REM list the files that would be copied if we really were doing a copy<br/>REM force it to believe that the target NonExistentDirectory is a directory<br/>REM and echo the name &amp; time for each<br/><br/>for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do echo %%A %%~tA<br/><br/>I assume you meant to say 'logfile each time the script runs' ... what would go in there ? the date and time the script ran ?Hi<br/><br/>Thank you for replay.<br/><br/>Basically what we need  is script which we will add to scheduled tasks in windows.<br/>When script finish we should have results displayed on the screen (for example in cmd) and log file when script was run (date is enough but if is possible also time)<br/><br/><br/><br/>How should save that file as *.bat or *.vbs?<br/><br/>I do not need  copy files, just list the newest from last week with creation date.<br/>My time zone is Europe (DK), maybe some part of the script has to be changed.<br/><br/>I am sorry if I am asking not to smart questions <br/>Thank you very much.<br/><br/>BR<br/>MichalHi Michal<br/>No, you are asking very good questions!<br/><br/>You would save it with a .bat extension, the small vbscript that is used is created by the batch, this is because date and time manipulation is simple in vbscript but nearly impossible in batch files<br/><br/>The XCopy is not actually copying anything - but listing the files that would be copied; the /L tells it not to copy anything<br/><br/>If you schedule this then you will not see any output, I suggest that the list of files is placed in the log also.<br/><br/>Denmark uses the correct date format  so that you will not need to change the script<br/><br/>This is the latest version, you will need to change the drive and directory in line 2 to be the folder that you wish to <a href="https://interviewquestions.tuteehub.com/tag/monitor-563344" style="font-weight:bold;" target="_blank" title="Click to know more about MONITOR">MONITOR</a><br/>In Line 3, a variable is created to hold the location of your logfile, you will have to change this also to be the name and location of your log<br/> Code: <a>[Select]</a>echo off<br/>CD /d F:\FolderForFiles<br/>Set LogFile="d:\logs\NewFiles.Log"<br/><br/>REM create the vb script<br/>&gt;evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))<br/><br/>REM extract todays date less 7 days<br/>for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A<br/><br/>REM tidy<br/>del evaluate.vbs<br/><br/>REM break up the date into constituent parts<br/>REM Note - the date is parsing UK format dates,<br/>REM        swap the MM and DD in the lines below<br/>REM        for US format<br/><br/>set MM=%OldestDate:~3,2%<br/>set DD=%OldestDate:~0,2%<br/>set YYYY=%OldestDate:~6,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>REM add the new entry to the log<br/>&gt;&gt; %LogFile% Echo %DATE% %TIME% <br/>REM list the files that would be copied if we really were doing a copy<br/>REM force it to believe that the target NonExistentDirectory is a directory<br/>REM and echo the name &amp; time for each<br/><br/>for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A<br/><br/>REM tidy up the log<br/>&gt;&gt; %LogFile% Echo ========================================<br/><br/>goto :EOF<br/><br/>:ProcessEach<br/><br/>&gt;&gt; %LogFile% echo %%~fA %%~tA<br/><br/>goto :EOF<br/>One problem with this is that the last line of each log entry will say xxx File(s) Copied, with the path preceding itHi gpl<br/><br/>Thank you very much for your help. The script is working almost I just got information: "Invalid number of parameters"<br/><br/>I do not know why. only what I did was change folder location:<br/>echo off<br/>CD /d e:\pictures<br/>Set LogFile="e:\NewFiles.Log"<br/><br/>Can we have separate log for each day? then we can track changes for each day. It is complicated to explain why ( read -&gt; bos) <br/><br/>Thank you very very much for help <br/><br/>BR<br/>MichalHi Michal<br/>It is hard to tell what the problem is, change the first line to Echo On and you will see how each command is made up after variables have been expanded<br/><br/>Yes you can have a new file every day, remember the section that creates a variable with the date of 1 week ago, we can do domething similar:<br/><br/> Code: <a>[Select]</a>echo off<br/>CD /d e:\pictures<br/><br/>REM grab todays date into variables<br/>set MM=%Date:~3,2%<br/>set DD=%Date:~0,2%<br/>set YYYY=%Date:~6,4%<br/><br/>REM Add the date variables to the log name<br/>REM putting the date in this order will sort the files sensibly<br/>Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"<br/><br/>REM create the vb script<br/>&gt;evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))<br/><br/>REM extract todays date less 7 days<br/>for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A<br/><br/>REM tidy<br/>del evaluate.vbs<br/><br/>REM break up the date into constituent parts<br/>REM Note - the date is parsing UK format dates,<br/>REM        swap the MM and DD in the lines below<br/>REM        for US format<br/><br/>set MM=%OldestDate:~3,2%<br/>set DD=%OldestDate:~0,2%<br/>set YYYY=%OldestDate:~6,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>REM add the new entry to the log<br/>&gt;&gt; %LogFile% Echo %DATE% %TIME% <br/>REM list the files that would be copied if we really were doing a copy<br/>REM force it to believe that the target NonExistentDirectory is a directory<br/>REM and echo the name &amp; time for each<br/><br/>for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A<br/><br/>REM tidy up the log<br/>&gt;&gt; %LogFile% Echo ========================================<br/><br/>goto :EOF<br/><br/>:ProcessEach<br/><br/>&gt;&gt; %LogFile% echo %%~fA %%~tA<br/><br/>goto :EOF<br/><br/><br/>I suspect the error about the invalid number of parameters is because you have a file or directory name somewhere with a space in it and the name is not enclosed by quotes - with echo on, it will be clear where.<br/><br/>GrahamHi gpl<br/><br/>I jsut make as suggested, here is error information:<br/>E:\Pictures&gt;for /F "delims=" %A in (' xcopy /l /i /d:~3 2-~0 2-~6 4 *.* NonExist<br/>entDirectory ') do call :ProcessEach %A<br/>Invalid number of parameters<br/><br/>Do you know how to fix that?<br/><br/>BTW:<br/>error log: excellent job <br/><br/>Thank you in advance for help.<br/><br/>BR<br/>MichalIt is clear why the error comes up, the %oldestdate% variable no longer holds a date<br/><br/>you will note that the xcopy instruction looks like this before expansion<br/>xcopy /l /i /d:%OldestDate%<br/>but what you see expanded is<br/>xcopy /l /i /d:~3 2-~0 2-~6 4<br/><br/>I think you may have deleted a % or more in these lines<br/> Code: <a>[Select]</a>set MM=%OldestDate:~3,2%<br/>set DD=%OldestDate:~0,2%<br/>set YYYY=%OldestDate:~6,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>GrahamHi Graham<br/><br/>I just tried to add more, but I can't make it work.<br/><br/>Can you try how it works on your computer, and publish the code. I make some mistake (s) - my programming skills are not so great therefore I do not trust my self <br/><br/>maybe something to do is also about date, I <a href="https://interviewquestions.tuteehub.com/tag/noticed-2877256" style="font-weight:bold;" target="_blank" title="Click to know more about NOTICED">NOTICED</a> that log file has this date format:<br/>NewFiles_7-14-0--20.Log which is almost correct <br/><br/><br/>Thank you in advance.<br/><br/>BR<br/>MichalOK Michal<br/>The last change I did to make the logfile name hold todays date stopped it working.<br/><br/>In the version below, I have blanked the OldestDate, DD, MM and YYYY variables before they are reused and it is working again .... except the bit that shows the name and creation time didnt work either - that is now fixed.<br/><br/>Try this<br/> Code: <a>[Select]</a>echo off<br/>CD /d e:\pictures<br/><br/>REM grab todays date into variables<br/>set MM=%Date:~3,2%<br/>set DD=%Date:~0,2%<br/>set YYYY=%Date:~6,4%<br/><br/>REM Add the date variables to the log name<br/>REM putting the date in this order will sort the files sensibly<br/>Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"<br/><br/>REM create the vb script<br/>&gt;evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))<br/><br/>REM extract todays date less 7 days<br/>set OldestDate=<br/>for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate=%%A<br/><br/>REM tidy<br/>del evaluate.vbs<br/><br/>REM break up the date into constituent parts<br/>REM Note - the date is parsing UK format dates,<br/>REM        swap the MM and DD in the lines below<br/>REM        for US format<br/><br/>set MM=<br/>set DD=<br/>set YYYY=<br/><br/>set MM=%OldestDate:~3,2%<br/>set DD=%OldestDate:~0,2%<br/>set YYYY=%OldestDate:~6,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>REM add the new entry to the log<br/>&gt;&gt; %LogFile% Echo %DATE% %TIME% <br/>REM list the files that would be copied if we really were doing a copy<br/>REM force it to believe that the target NonExistentDirectory is a directory<br/>REM and echo the name &amp; time for each<br/><br/>for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A<br/><br/>REM tidy up the log<br/>&gt;&gt; %LogFile% Echo ========================================<br/><br/>goto :EOF<br/><br/>:ProcessEach<br/><br/>&gt;&gt; %LogFile% echo %~f1 %~t1<br/><br/>goto :EOF<br/>Grahamhi Graham<br/><br/>It seems to be working now. No error <br/><br/>But )))<br/><br/>in the log message there is no new files listed only date like that (I think date when script was running - your correction):<br/><br/>2010-07-14 13:55:08,37 <br/>========================================<br/>2010-07-14 13:55:14,53 <br/>========================================<br/>2010-07-14 13:55:26,75 <br/>========================================<br/><br/><br/>I created few files today, but they are not listed. Any change to list them together with creation date?<br/><br/>Thank you very much for help.<br/><br/>I very much appreciate your fast help.<br/><br/>BR<br/>MichalI think I have it<br/>The entry in your log is <br/>2010-07-14 13:55:26,75 <br/>I think that this is the default way a date is shown on your computer; the section that calculates the date 7 days before is not quite right; I think that you should try this instead<br/> Code: <a>[Select]</a>echo off<br/>CD /d e:\pictures<br/><br/>REM grab todays date into variables<br/>set MM=%Date:~3,2%<br/>set DD=%Date:~0,2%<br/>set YYYY=%Date:~6,4%<br/><br/>REM Add the date variables to the log name<br/>REM putting the date in this order will sort the files sensibly<br/>Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"<br/><br/>REM create the vb script<br/>&gt;evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))<br/><br/>REM extract todays date less 7 days<br/>set OldestDate=<br/>for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate=%%A<br/><br/>REM tidy<br/>del evaluate.vbs<br/><br/>REM break up the date into constituent parts<br/>REM Note - the date is parsing YYYY-MM-DD format dates,<br/><br/>set MM=<br/>set DD=<br/>set YYYY=<br/><br/>set MM=%OldestDate:~5,2%<br/>set DD=%OldestDate:~8,2%<br/>set YYYY=%OldestDate:~0,4%<br/><br/>REM reconstruct US format with '-' separators<br/>set OldestDate=%MM%-%DD%-%YYYY%<br/><br/>REM add the new entry to the log<br/>&gt;&gt; %LogFile% Echo %DATE% %TIME%<br/>&gt;&gt; %LogFile% Echo Since : %OldestDate%<br/>REM list the files that would be copied if we really were doing a copy<br/>REM force it to believe that the target NonExistentDirectory is a directory<br/>REM and echo the name &amp; time for each<br/><br/>for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A<br/><br/>REM tidy up the log<br/>&gt;&gt; %LogFile% Echo ========================================<br/><br/>goto :EOF<br/><br/>:ProcessEach<br/><br/>&gt;&gt; %LogFile% echo %~f1 %~t1<br/><br/>goto :EOF<br/><br/><br/>Ive added a line which inserts into the log<br/>Since : 'the value of OLDESTDATE'<br/>which is in US format<br/>Could you try it ?<br/>Thanks<br/>GrahamHi Graham<br/><br/>It seems to be working a lot better now. It is creating log with information like that:<br/><br/>2010-07-14 14:42:31,45<br/>Since : 07-07-2010<br/>E:\all <br/>E:\New<br/>E:\NewFiles_7-14-0--20.Log 2010-07-14 14:42<br/>E:\test.xml 2010-07-14 14:41<br/>E:\4 <br/>========================================<br/><br/><br/>the only small problem is that file names are not complet (I marked 2 names as blue as example).<br/><br/>2010-07-14 14:42:31,45<br/>Since : 07-07-2010<br/>E:\all Briefcase.bfc<br/>E:\New all files for last 7 days.bat<br/>E:\NewFiles_7-14-0--20.Log 2010-07-14 14:42<br/>E:\test.xml 2010-07-14 14:41<br/>E:\4 <br/>========================================<br/><br/><br/>The other is I do not have file 4, and I do not know from ´where it come from.<br/><br/>To easier read log is possible to have file file name few "spaces" and then date?<br/>example:<br/>E:\test.xml                                           2010-07-14 14:41<br/><br/>One more question, I tried to change path to network path as we are going to monitor network folders, but the script is constantly show <a href="https://interviewquestions.tuteehub.com/tag/report-25448" style="font-weight:bold;" target="_blank" title="Click to know more about REPORT">REPORT</a> for "old" my local path (that same result after deleting path). Do you know how to fix that?<br/><br/>In the future I will be monitoring few network folders, by simply copy/paste this code in that some file and change monitoring folder path (log will stay that some).<br/><br/>like this:<br/><br/>code<br/>----------------<br/><br/>echo off<br/>CD /d \\server\path1<br/><br/>REM grab todays date into variables<br/>.<br/>.<br/>.<br/>goto :EOF<br/><br/>CD /d \\server\path2<br/><br/>REM grab todays date into variables<br/>.<br/>.<br/>.<br/>goto :EOF<br/><br/><br/>BR<br/>MichalHi Michal<br/>The 4 is simply the number of files found (it should actually read 4 File(s) copied)<br/>the batch file which you highlighted is locked as it is executing, I expect that is why it does not show the timestamp, maybe that is why the 'all Briefcase.bfc' file does not show a date either<br/><br/>the line near the bottom is the one that writes the info to the log<br/> Code: <a>[Select]</a>&gt;&gt; %LogFile% echo %~f1 %~t1<br/>change to this<br/> Code: <a>[Select]</a>[code]&gt;&gt; %LogFile% echo %~f1                                     %~t1or even use tab characters instead of spaces<br/><br/>I am not very sure how to user the \\servername - batch files dont like it and try to change to a d: type drive<br/>You could try remapping the servers to drive e: and running the batch ?<br/><br/>Otherwise, perhaps one of the more experienced members here could jump in with the solution ?<br/><br/>Graham[/code]Hi Graham<br/><br/>Thank you for info with log it works fine.<br/><br/>I just checked. Whenever I use one of mounted diskson my computer like c: or e: then I can see the list.<br/>However when I use \\servername\network path it is not working.<br/>Conclusion is that application works on mapped disks but not on network.<br/><br/>Regarding 'all Briefcase.bfc' the name have space. That is the problem. With no space I can see all as expected <br/>Luckily that is not a problem because all files I have to monitor insted of space have "_".<br/><br/><br/>It is very important for me that application can work with network path as I am going to monitor many folders (probably I do not have enough letters to map all the disks <br/>Please help  <br/><br/>Do you or anybody know how to fix that?<br/><br/>Thank you for any help.<br/><br/>BR<br/>Michal<br/><br/></p></body></html>


Discussion

No Comment Found