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

Author Topic: rename file to date and timstamp  (Read 3847 times)

0 Members and 1 Guest are viewing this topic.

bang_dba

    Topic Starter


    Greenhorn

    rename file to date and timstamp
    « on: June 29, 2010, 09:01:14 AM »
    I have a batch script as follows.
    Now i want to rename the *.csv file to *.csv_processed_mm/yy/yyyy  format.

    The current rename is coming in the format of mm/dd/yy

    and also, I want to append the timestamp in this.

    Any help on this would be greatly appreciated









    d:

    echo cd data\Scripts


    for %%f in (D:\data\DELIVERY_NOTE\*.csv) Do (


    sqlldr USERID=config/config control=D:\data\Scripts\loaddata_del.ctl skip=2 data=%%f

    (
    echo conn config/config;
    echo exec DELIVERY_NOTE_PKG.PopNote_stage;
    )| sqlplus -s /nolog


    move %%f D:\data\Processed_Delivery\
    cd D:\data\processed_Delivery\
    rename *.csv *.csv.processed_%date%
    cd d:\data\scripts\
    echo #############
    echo PROCESSED THE FILE %%f AT %data% %time%
    echo ############


    )

    marvinengland



      Hopeful

      Thanked: 11
      Re: rename file to date and timstamp
      « Reply #1 on: June 29, 2010, 01:13:22 PM »
      C:\test>type  bang.bat
      Code: [Select]
      @echo off
      rem cd D:\data\processed_Delivery\
      cd  c:\test
      rem dir  /b  > csvfiles.txt
      set MM=%date:~4,2%
      set DD=%date:~7,2%
      set YYYY=%date:~10,4%
      echo MM=%MM%
      echo DD=%DD%
      echo YYYY=%YYYY%
      for /f %%i in (csvfiles.txt) do (
      copy  %%i csv_processed_%MM%%DD%%YYYY%
      dir /b csv_processed_%MM%%DD%%YYYY%
      rem *.csv

      echo PROCESSED THE FILE %%i AT %date% %time%

      rem  *.csv_processed_mm/dd/yyyy  format.
      )

      Output:C:\test>bang.bat
      MM=06
      DD=29
      YYYY=2010
              1 file(s) copied.
      csv_processed_06292010
      PROCESSED THE FILE timefile1.csv AT Tue 06/29/2010 13:07:29.42
              1 file(s) copied.
      csv_processed_06292010
      PROCESSED THE FILE timefile2.csv AT Tue 06/29/2010 13:07:29.42
              1 file(s) copied.
      csv_processed_06292010
      PROCESSED THE FILE timefile3.csv AT Tue 06/29/2010 13:07:29.42
              1 file(s) copied.
      csv_processed_06292010
      PROCESSED THE FILE timefile4.csv AT Tue 06/29/2010 13:07:29.42

      C:\test>
      USA

      marvinengland



        Hopeful

        Thanked: 11
        Re: rename file to date and timstamp
        « Reply #2 on: June 29, 2010, 06:38:24 PM »

        "Now I want to rename the *.csv file to *.csv_processed_mm/dd/yyyy  format.
        Also, I want to append the timestamp in this.
        Any help on this would be greatly appreciated"

        The above post (reply 1) did not include the original file name as part of the final file name.
        The following code corrects that mistake.

        ___________________________________


        C:\test>type  bang.bat
        Code: [Select]
        @echo off
        rem cd D:\data\processed_Delivery\
        cd  c:\test
        rem dir  /b  *.csv > csvfiles.txt
        set MM=%date:~4,2%
        set DD=%date:~7,2%
        set YYYY=%date:~10,4%
        echo MM=%MM%
        echo DD=%DD%
        echo YYYY=%YYYY%
        for /f %%i in (csvfiles.txt) do (
        copy  %%i %%i_processed_%MM%%DD%%YYYY%
        dir /b %%i_processed_%MM%%DD%%YYYY%
        rem   del  %%i

        rem cd d:\data\scripts\

        echo PROCESSED THE FILE %%i AT %date% %time%

        rem  *.csv_processed_mm/dd/yyyy  format.
        )

        Output:

        C:\test>bang.bat
        MM=06
        DD=29
        YYYY=2010
                1 file(s) copied.
        sdate.csv_processed_06292010
        PROCESSED THE FILE sdate.csv AT Tue 06/29/2010 19:31:04.06
                1 file(s) copied.
        stime.csv_processed_06292010
        PROCESSED THE FILE stime.csv AT Tue 06/29/2010 19:31:04.06
                1 file(s) copied.
        txtfile.csv_processed_06292010
        PROCESSED THE FILE txtfile.csv AT Tue 06/29/2010 19:31:04.06

        C:\test>

        p.s.  I was unable to edit the above post ( reply one).
        « Last Edit: June 29, 2010, 06:56:18 PM by marvinengland »
        USA

        marvinengland



          Hopeful

          Thanked: 11
          Re: rename file to date and timstamp
          « Reply #3 on: July 01, 2010, 01:59:29 AM »
          Hello
          USA

          bang_dba

            Topic Starter


            Greenhorn

            Re: rename file to date and timstamp
            « Reply #4 on: July 01, 2010, 03:18:35 AM »
            Hi marvinengland,

            Thanks for the update.

            It works.

            I have one more problem.

            I am generating log file with the sqlloader.

            ie.,

            sqlldr USERID=config/config control=D:\data\Scripts\loaddata_del.ctl skip=2 log=d:\log\log01.log data=%%f

            Now for every loop, there is log01.log file getting generated, and hence it is getting overwritten.

            So, I tried to rename it to  log01.log_<date>_<time>.


            (I am using simple command
            copy *.log *.log.%DATE:/=%_%time::=%.processed
            del *.log
            )

            But,  For every loop iteration.,  ie., once the loop starts the TIME STAMP remains constant for all the loop items.

            How can i handle this ?

            thx.




            « Last Edit: July 01, 2010, 03:42:26 AM by bang_dba »

            marvinengland



              Hopeful

              Thanked: 11
              Re: rename file to date and timstamp
              « Reply #5 on: July 01, 2010, 09:23:29 AM »
              C:\test>type   bang.bat
              @echo off
              rem echo. > time.log
              rem dir /b > csvfiles.txt
              rem cd D:\data\processed_Delivery\
              cd  c:\test
              set MM=%date:~4,2%
              set DD=%date:~7,2%
              set YYYY=%date:~10,4%
              echo MM=%MM%
              echo DD=%DD%
              echo YYYY=%YYYY%
              for /f %%i in (csvfiles.txt) do (
              copy  %%i %%i_processed_%MM%%DD%%YYYY%
              dir /b %%i_processed_%MM%%DD%%YYYY%

              call  :mklog %%i
              rem del %%i

              )
              echo time.log
              type  time.log
              goto :eof

              :mklog %1
              sleep  6
              rem echo PROCESSED THE FILE %1 AT %date% %time%
              echo PROCESSED THE FILE %1 AT %date% %time%  >> time.log


              rem   C:\Program Files\Windows Resource Kits\Tools\sleep.exe
              rem  If a time delay is needed for each timestamp,  use ping or sleep
              rem The %time% variable will only change with a call to a label or another batch

              Output:

              C:\test>bang.bat
              MM=07
              DD=01
              YYYY=2010
                      1 file(s) copied.
              timefile1.csv_processed_07012010
                      1 file(s) copied.
              timefile2.csv_processed_07012010
                      1 file(s) copied.
              timefile3.csv_processed_07012010
                      1 file(s) copied.
              timefile4.csv_processed_07012010
              time.log

              PROCESSED THE FILE timefile1.csv AT Thu 07/01/2010 10:39:26.15
              PROCESSED THE FILE timefile2.csv AT Thu 07/01/2010 10:39:32.17
              PROCESSED THE FILE timefile3.csv AT Thu 07/01/2010 10:39:38.19
              PROCESSED THE FILE timefile4.csv AT Thu 07/01/2010 10:39:44.21

              C:\test>
              « Last Edit: July 01, 2010, 09:40:43 AM by marvinengland »
              USA

              marvinengland



                Hopeful

                Thanked: 11
                Re: rename file to date and timstamp
                « Reply #6 on: July 01, 2010, 03:03:17 PM »
                Don't use sleep or ping.  There is at least 1/100 of a second between each timestamp.
                When there are many *.csv files,  the batch will run much faster

                C:\test>type   bang.bat
                @echo off
                echo. > time.log
                rem dir /b  *.csv > csvfiles.txt
                rem cd D:\data\processed_Delivery\
                cd  c:\test
                set MM=%date:~4,2%
                set DD=%date:~7,2%
                set YYYY=%date:~10,4%
                echo MM=%MM%
                echo DD=%DD%
                echo YYYY=%YYYY%
                for /f %%i in (csvfiles.txt) do (
                copy  %%i %%i_processed_%MM%%DD%%YYYY%
                dir /b %%i_processed_%MM%%DD%%YYYY%

                call  :mklog %%i
                rem del %%i

                )
                echo time.log
                type  time.log
                goto :eof

                :mklog %1
                echo PROCESSED THE FILE %1 AT %date% %time%  >> time.log


                rem The %time% variable will only change with a call to a label or another batch

                Output:
                C:\test>bang.bat
                MM=07
                DD=01
                YYYY=2010
                        1 file(s) copied.
                timefile1.csv_processed_07012010
                        1 file(s) copied.
                timefile2.csv_processed_07012010
                        1 file(s) copied.
                timefile3.csv_processed_07012010
                        1 file(s) copied.
                timefile4.csv_processed_07012010
                time.log

                PROCESSED THE FILE timefile1.csv AT Thu 07/01/2010 15:53:36.94
                PROCESSED THE FILE timefile2.csv AT Thu 07/01/2010 15:53:36.95
                PROCESSED THE FILE timefile3.csv AT Thu 07/01/2010 15:53:36.97
                PROCESSED THE FILE timefile4.csv AT Thu 07/01/2010 15:53:36.98

                C:\test>
                USA