Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: files listed added to folder during last week  (Read 7989 times)

0 Members and 1 Guest are viewing this topic.

silberzin

    Topic Starter


    Rookie

    files listed added to folder during last week
    « on: July 13, 2010, 06:40:06 AM »
    Hi All

    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.
    Script should list results on screen and also create txt fog file each time script is running.

    Is possible to create such script?
    Unfortunatelly my programming skills are not so big therefore if possible please advice in more details :)

    Thank you in advance.

    BR
    Michal

    gpl



      Apprentice
    • Thanked: 27
      Re: files listed added to folder during last week
      « Reply #1 on: July 13, 2010, 07:37:28 AM »
      Will this do ?
      Code: [Select]
      @echo off

      REM create the vb script
      >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

      REM extract todays date less 7 days
      for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A

      REM tidy
      del evaluate.vbs

      REM break up the date into constituent parts
      REM Note - the date is parsing UK format dates,
      REM        swap the MM and DD in the lines below
      REM        for US format

      set MM=%OldestDate:~3,2%
      set DD=%OldestDate:~0,2%
      set YYYY=%OldestDate:~6,4%

      REM reconstruct US format with '-' separators
      set OldestDate=%MM%-%DD%-%YYYY%

      REM list the files that would be copied if we really were doing a copy
      REM force it to believe that the target NonExistentDirectory is a directory
      REM and echo the name & time for each

      for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do echo %%A %%~tA

      I assume you meant to say 'logfile each time the script runs' ... what would go in there ? the date and time the script ran ?

      silberzin

        Topic Starter


        Rookie

        Re: files listed added to folder during last week
        « Reply #2 on: July 13, 2010, 11:44:35 PM »
        Hi

        Thank you for replay.

        Basically what we need  is script which we will add to scheduled tasks in windows.
        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)



        How should save that file as *.bat or *.vbs?

        I do not need  copy files, just list the newest from last week with creation date.
        My time zone is Europe (DK), maybe some part of the script has to be changed.

        I am sorry if I am asking not to smart questions :)
        Thank you very much.

        BR
        Michal

        gpl



          Apprentice
        • Thanked: 27
          Re: files listed added to folder during last week
          « Reply #3 on: July 14, 2010, 12:42:06 AM »
          Hi Michal
          No, you are asking very good questions!

          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

          The XCopy is not actually copying anything - but listing the files that would be copied; the /L tells it not to copy anything

          If you schedule this then you will not see any output, I suggest that the list of files is placed in the log also.

          Denmark uses the correct date format  ;D so that you will not need to change the script

          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 monitor
          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
          Code: [Select]
          @echo off
          CD /d F:\FolderForFiles
          Set LogFile="d:\logs\NewFiles.Log"

          REM create the vb script
          >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

          REM extract todays date less 7 days
          for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A

          REM tidy
          del evaluate.vbs

          REM break up the date into constituent parts
          REM Note - the date is parsing UK format dates,
          REM        swap the MM and DD in the lines below
          REM        for US format

          set MM=%OldestDate:~3,2%
          set DD=%OldestDate:~0,2%
          set YYYY=%OldestDate:~6,4%

          REM reconstruct US format with '-' separators
          set OldestDate=%MM%-%DD%-%YYYY%

          REM add the new entry to the log
          >> %LogFile% Echo %DATE% %TIME%
          REM list the files that would be copied if we really were doing a copy
          REM force it to believe that the target NonExistentDirectory is a directory
          REM and echo the name & time for each

          for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A

          REM tidy up the log
          >> %LogFile% Echo ========================================

          goto :EOF

          :ProcessEach

          >> %LogFile% echo %%~fA %%~tA

          goto :EOF
          One problem with this is that the last line of each log entry will say xxx File(s) Copied, with the path preceding it

          silberzin

            Topic Starter


            Rookie

            Re: files listed added to folder during last week
            « Reply #4 on: July 14, 2010, 01:25:01 AM »
            Hi gpl

            Thank you very much for your help. The script is working almost :) I just got information: "Invalid number of parameters"

            I do not know why. only what I did was change folder location:
            @echo off
            CD /d e:\pictures
            Set LogFile="e:\NewFiles.Log"

            Can we have separate log for each day? then we can track changes for each day. It is complicated to explain why ( read -> bos) :)

            Thank you very very much for help :)

            BR
            Michal

            gpl



              Apprentice
            • Thanked: 27
              Re: files listed added to folder during last week
              « Reply #5 on: July 14, 2010, 01:54:34 AM »
              Hi Michal
              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

              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:

              Code: [Select]
              @echo off
              CD /d e:\pictures

              REM grab todays date into variables
              set MM=%Date:~3,2%
              set DD=%Date:~0,2%
              set YYYY=%Date:~6,4%

              REM Add the date variables to the log name
              REM putting the date in this order will sort the files sensibly
              Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"

              REM create the vb script
              >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

              REM extract todays date less 7 days
              for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate = %%A

              REM tidy
              del evaluate.vbs

              REM break up the date into constituent parts
              REM Note - the date is parsing UK format dates,
              REM        swap the MM and DD in the lines below
              REM        for US format

              set MM=%OldestDate:~3,2%
              set DD=%OldestDate:~0,2%
              set YYYY=%OldestDate:~6,4%

              REM reconstruct US format with '-' separators
              set OldestDate=%MM%-%DD%-%YYYY%

              REM add the new entry to the log
              >> %LogFile% Echo %DATE% %TIME%
              REM list the files that would be copied if we really were doing a copy
              REM force it to believe that the target NonExistentDirectory is a directory
              REM and echo the name & time for each

              for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A

              REM tidy up the log
              >> %LogFile% Echo ========================================

              goto :EOF

              :ProcessEach

              >> %LogFile% echo %%~fA %%~tA

              goto :EOF


              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.

              Graham

              silberzin

                Topic Starter


                Rookie

                Re: files listed added to folder during last week
                « Reply #6 on: July 14, 2010, 04:26:14 AM »
                Hi gpl

                I jsut make as suggested, here is error information:
                E:\Pictures>for /F "delims=" %A in (' xcopy /l /i /d:~3 2-~0 2-~6 4 *.* NonExist
                entDirectory ') do call :ProcessEach %A
                Invalid number of parameters

                Do you know how to fix that?

                BTW:
                error log: excellent job :)

                Thank you in advance for help.

                BR
                Michal

                gpl



                  Apprentice
                • Thanked: 27
                  Re: files listed added to folder during last week
                  « Reply #7 on: July 14, 2010, 05:14:22 AM »
                  It is clear why the error comes up, the %oldestdate% variable no longer holds a date

                  you will note that the xcopy instruction looks like this before expansion
                  xcopy /l /i /d:%OldestDate%
                  but what you see expanded is
                  xcopy /l /i /d:~3 2-~0 2-~6 4

                  I think you may have deleted a % or more in these lines
                  Code: [Select]
                  set MM=%OldestDate:~3,2%
                  set DD=%OldestDate:~0,2%
                  set YYYY=%OldestDate:~6,4%

                  REM reconstruct US format with '-' separators
                  set OldestDate=%MM%-%DD%-%YYYY%

                  Graham

                  silberzin

                    Topic Starter


                    Rookie

                    Re: files listed added to folder during last week
                    « Reply #8 on: July 14, 2010, 05:31:23 AM »
                    Hi Graham

                    I just tried to add more, but I can't make it work.

                    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 :)

                    maybe something to do is also about date, I noticed that log file has this date format:
                    NewFiles_7-14-0--20.Log which is almost correct :)


                    Thank you in advance.

                    BR
                    Michal
                    « Last Edit: July 14, 2010, 05:44:27 AM by silberzin »

                    gpl



                      Apprentice
                    • Thanked: 27
                      Re: files listed added to folder during last week
                      « Reply #9 on: July 14, 2010, 05:51:30 AM »
                      OK Michal
                      The last change I did to make the logfile name hold todays date stopped it working.

                      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.

                      Try this
                      Code: [Select]
                      @echo off
                      CD /d e:\pictures

                      REM grab todays date into variables
                      set MM=%Date:~3,2%
                      set DD=%Date:~0,2%
                      set YYYY=%Date:~6,4%

                      REM Add the date variables to the log name
                      REM putting the date in this order will sort the files sensibly
                      Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"

                      REM create the vb script
                      >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

                      REM extract todays date less 7 days
                      set OldestDate=
                      for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate=%%A

                      REM tidy
                      del evaluate.vbs

                      REM break up the date into constituent parts
                      REM Note - the date is parsing UK format dates,
                      REM        swap the MM and DD in the lines below
                      REM        for US format

                      set MM=
                      set DD=
                      set YYYY=

                      set MM=%OldestDate:~3,2%
                      set DD=%OldestDate:~0,2%
                      set YYYY=%OldestDate:~6,4%

                      REM reconstruct US format with '-' separators
                      set OldestDate=%MM%-%DD%-%YYYY%

                      REM add the new entry to the log
                      >> %LogFile% Echo %DATE% %TIME%
                      REM list the files that would be copied if we really were doing a copy
                      REM force it to believe that the target NonExistentDirectory is a directory
                      REM and echo the name & time for each

                      for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A

                      REM tidy up the log
                      >> %LogFile% Echo ========================================

                      goto :EOF

                      :ProcessEach

                      >> %LogFile% echo %~f1 %~t1

                      goto :EOF
                      Graham

                      silberzin

                        Topic Starter


                        Rookie

                        Re: files listed added to folder during last week
                        « Reply #10 on: July 14, 2010, 05:59:12 AM »
                        hi Graham

                        It seems to be working now. No error :)

                        But :))))

                        in the log message there is no new files listed only date like that (I think date when script was running - your correction):

                        2010-07-14 13:55:08,37
                        ========================================
                        2010-07-14 13:55:14,53
                        ========================================
                        2010-07-14 13:55:26,75
                        ========================================


                        I created few files today, but they are not listed. Any change to list them together with creation date?

                        Thank you very much for help.

                        I very much appreciate your fast help.

                        BR
                        Michal

                        gpl



                          Apprentice
                        • Thanked: 27
                          Re: files listed added to folder during last week
                          « Reply #11 on: July 14, 2010, 06:24:23 AM »
                          I think I have it
                          The entry in your log is
                          2010-07-14 13:55:26,75
                          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
                          Code: [Select]
                          @echo off
                          CD /d e:\pictures

                          REM grab todays date into variables
                          set MM=%Date:~3,2%
                          set DD=%Date:~0,2%
                          set YYYY=%Date:~6,4%

                          REM Add the date variables to the log name
                          REM putting the date in this order will sort the files sensibly
                          Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"

                          REM create the vb script
                          >evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

                          REM extract todays date less 7 days
                          set OldestDate=
                          for /f "delims=" %%A in ( ' cscript //nologo evaluate.vbs "Date -7" ' ) do set OldestDate=%%A

                          REM tidy
                          del evaluate.vbs

                          REM break up the date into constituent parts
                          REM Note - the date is parsing YYYY-MM-DD format dates,

                          set MM=
                          set DD=
                          set YYYY=

                          set MM=%OldestDate:~5,2%
                          set DD=%OldestDate:~8,2%
                          set YYYY=%OldestDate:~0,4%

                          REM reconstruct US format with '-' separators
                          set OldestDate=%MM%-%DD%-%YYYY%

                          REM add the new entry to the log
                          >> %LogFile% Echo %DATE% %TIME%
                          >> %LogFile% Echo Since : %OldestDate%
                          REM list the files that would be copied if we really were doing a copy
                          REM force it to believe that the target NonExistentDirectory is a directory
                          REM and echo the name & time for each

                          for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach %%A

                          REM tidy up the log
                          >> %LogFile% Echo ========================================

                          goto :EOF

                          :ProcessEach

                          >> %LogFile% echo %~f1 %~t1

                          goto :EOF


                          Ive added a line which inserts into the log
                          Since : 'the value of OLDESTDATE'
                          which is in US format
                          Could you try it ?
                          Thanks
                          Graham

                          silberzin

                            Topic Starter


                            Rookie

                            Re: files listed added to folder during last week
                            « Reply #12 on: July 14, 2010, 06:55:40 AM »
                            Hi Graham

                            It seems to be working a lot better now. It is creating log with information like that:

                            2010-07-14 14:42:31,45
                            Since : 07-07-2010
                            E:\all
                            E:\New
                            E:\NewFiles_7-14-0--20.Log 2010-07-14 14:42
                            E:\test.xml 2010-07-14 14:41
                            E:\4
                            ========================================


                            the only small problem is that file names are not complet (I marked 2 names as blue as example).

                            2010-07-14 14:42:31,45
                            Since : 07-07-2010
                            E:\all Briefcase.bfc
                            E:\New all files for last 7 days.bat
                            E:\NewFiles_7-14-0--20.Log 2010-07-14 14:42
                            E:\test.xml 2010-07-14 14:41
                            E:\4
                            ========================================


                            The other is I do not have file 4, and I do not know from ´where it come from.

                            To easier read log is possible to have file file name few "spaces" and then date?
                            example:
                            E:\test.xml                                           2010-07-14 14:41

                            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 report for "old" my local path (that same result after deleting path). Do you know how to fix that?

                            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).

                            like this:

                            code
                            ----------------

                            @echo off
                            CD /d \\server\path1

                            REM grab todays date into variables
                            .
                            .
                            .
                            goto :EOF

                            CD /d \\server\path2

                            REM grab todays date into variables
                            .
                            .
                            .
                            goto :EOF


                            BR
                            Michal
                            « Last Edit: July 14, 2010, 07:16:04 AM by silberzin »

                            gpl



                              Apprentice
                            • Thanked: 27
                              Re: files listed added to folder during last week
                              « Reply #13 on: July 14, 2010, 08:09:50 AM »
                              Hi Michal
                              The 4 is simply the number of files found (it should actually read 4 File(s) copied)
                              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

                              the line near the bottom is the one that writes the info to the log
                              Code: [Select]
                              >> %LogFile% echo %~f1 %~t1
                              change to this
                              Code: [Select]
                              [code]>> %LogFile% echo %~f1                                     %~t1or even use tab characters instead of spaces

                              I am not very sure how to user the \\servername - batch files dont like it and try to change to a d: type drive
                              You could try remapping the servers to drive e: and running the batch ?

                              Otherwise, perhaps one of the more experienced members here could jump in with the solution ?

                              Graham[/code]

                              silberzin

                                Topic Starter


                                Rookie

                                Re: files listed added to folder during last week
                                « Reply #14 on: July 14, 2010, 11:53:37 PM »
                                Hi Graham

                                Thank you for info with log it works fine.

                                I just checked. Whenever I use one of mounted diskson my computer like c: or e: then I can see the list.
                                However when I use \\servername\network path it is not working.
                                Conclusion is that application works on mapped disks but not on network.

                                Regarding 'all Briefcase.bfc' the name have space. That is the problem. With no space I can see all as expected :)
                                Luckily that is not a problem because all files I have to monitor insted of space have "_".


                                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 :)
                                Please help  :'(

                                Do you or anybody know how to fix that?

                                Thank you for any help.

                                BR
                                Michal

                                « Last Edit: July 15, 2010, 12:23:53 AM by silberzin »