Announcement

Collapse
No announcement yet.

How to append time and date to file name?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to append time and date to file name?

    Hi!!
    I need some advice... I'm using some sample script that map network drive and make copy of a local files to a folder on this mapped drive. In additional it appends date/time to a backup folder. The question is if it possible to append date and time to all files that copied into backup folder also?
    Here is code of my script:
    Code:
     
    net use S: \\ServerName\ShareName
    @echo Drive mapped successfully
    for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
      for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
      set tm=%tm::=-%
      set dtt=%dt%%tm%
    cd P:\"Backup files"
    xcopy P:\"Backup files" S:\backup-%dtt% /s /c /d /i /y
    if exist S: net use S: /d /y
    Thank you in advance,
    Igor.

  • #2
    Re: How to append time and date to file name?

    cd P:\"Backup files"
    Are all the backup files in the same folder (one level)?


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: How to append time and date to file name?

      Yes, all files in same folder. Actually I need to back up 3 log files of an application (user create them manually in same folder)...

      Comment


      • #4
        Re: How to append time and date to file name?

        Try something like this:

        Code:
        @echo off
        :main
        for %%f in (logfile*.log) do call :change %%f
        goto :eof
        
        :change
        set incoming=%1
        ::ren %1 %date:~3,8%-%incoming%
        echo %date:~3,8%-%incoming%
        :: end
        The idea is that you loop through files with the for %%f -statement and call the change function for each matching file. In the change function, you will rename each file by adding part of the current date to the filename. Beware! The script must be adapted to your local settings, as %date% output changes between locales. You would do wisely to test the date extraction first by using the echo version.

        -vP

        Comment


        • #5
          Re: How to append time and date to file name?

          Prefix or append datetimestamp to the names of the files being copied by XCOPY,
          Code:
          @echo off
          SetLocal
          
          :: prepare date/time stamp
          For /F "tokens=1-4 delims=:., " %%i in ('echo.%time%') Do (
             (Set HH=0%%i)&(set NN=%%j)&(set SS=%%k)&(Set Sd=%%l)
          )
          Set "HH=%HH:~-2%"
          for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
             for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
                (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set YYYY=%%p)
             )
          )
          
          :: timestamp (yyyymmmdd-hhnnss)
          Set "month=%YYYY%%MM%"
          set "dtt=%YYYY%%MM%%DD%-%HH%%NN%%SS%"
          
          :: directories
          Set "sourcefolder=\\Server1\backupfiles"
          Set "destinationfolder=\\Server2\ShareName\backup-%month%"
          
          :: copy files
          For /f "delims=" %%a in (
            'Dir /a-D /b /s "%sourcefolder%\*.bkf" 2^>nul'
            ) do If exist "%%a" (
            xcopy /s /c /d /i /y "%%a" "%destinationfolder%\%dtt%-%%~na.*"
          )
          
          EndLocal
          The Source and Destination folders can exist on a local drive or unc path (no need to map a drive first).
          The files *.bkf can also be replaced by any other file type - or, all files: *.*

          I use For /f with the /f switch, so I could run Dir /s with the /s switch this make the script searching for files also in subfolders of the source.
          But in your case you could also do
          For %%a in ("%sourcefolder%\*.bkf") Do If exist "%%a" (

          The For command is needed to beable to retrieve the filename w/out the extension.
          samples:
          To prepend a text ("test-") to all the files that you copy you can simply do this:
          xcopy "%sourcefolder%\*.bkf" "%destinationfolder%\test-*.*"

          It is however, not posible to use a Variable in the filename together with a wildcard like *.*
          But it is possible to use a Variable in the filename and use a wildcard just for the extension. That why you need the filename of the file that being copied w/out the extension.
          Why must the extension being replaced with a wildcard?? - because otherwise the Xcopy would aks for every file if it is a file or folder, this requires user interaction.


          \Rems

          EDIT -
          forgot to add the datestamp to the file names (hahaha),,, <= FIXED
          Last edited by Rems; 30th October 2008, 23:20. Reason: haha, see Edit

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: How to append time and date to file name?

            forgot to add the datestamp to the file names (hahaha),,, <= FIXED
            I catch it!! Thank you buddy for help!!
            Here is my new script, works perfect!!

            Code:
            @echo off
            SetLocal
            
            :: prepare date/time stamp
            for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
              for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
            
            :: timestamp (yyyymmmdd-hhnnss)
            set tm=%tm::=-%
            set dtt=%dt%%tm%
            
            :: directories
            Set "sourcefolder=D:\Project\Backup files"
            Set "destinationfolder=C:\Test-%dtt%"
            
            :: copy files
            For /f "delims=" %%a in (
              'Dir /a-D /b /s "%sourcefolder%\*.*" 2^>nul'
              ) do If exist "%%a" (
              xcopy /s /c /d /i /y "%%a" "%destinationfolder%\%dtt%-%%~na.*"
            
            )
            
            EndLocal

            Comment


            • #7
              Re: How to append time and date to file name?

              Rems,
              I need you advice one more time... The script in my previous post work fine on local computer. It provides very good solution to backup important files from one local drive to another. But... Two days ago I tried copy files from on remote computer to another and it not work (even when I do this with you original script)... This is a screenshot with error message:





              It looks like this bat file doesn't accept UNC patch as destination folder... Here code of script I used:

              Code:
              @echo on
              SetLocal
               
              :: prepare date/time stamp
              for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
              for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
              :: timestamp (yyyymmmdd-hhnnss)
              set tm=%tm::=-%
              set dtt=%dt%%tm%
               
              :: directories
              Set "sourcefolder=\\Conroe\Users"
              Set "destinationfolder=\\Paris\WMELogs-%dtt%"
               
              :: copy files
              For %%a in ("%sourcefolder%\*.*") Do If exist "%%a" (
              xcopy /s /c /d /i /y "%%a" "%destinationfolder%\%dtt%-%%~na.*"
              )
              Pause
               
              EndLocal
              And if I map \\Paris\WMELogs as network drive before, and use local patch as destination folder, script working just fine:



              Here the working code:

              Code:
              @echo on
               
              SetLocal
               
              :: Map Network drive
              net use S: \\Paris\WMELogs
               
              :: prepare date/time stamp
              for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
              for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
               
              :: timestamp (yyyymmmdd-hhnnss)
              set tm=%tm::=-%
              set dtt=%dt%%tm%
               
              :: directories
              Set "sourcefolder=\\Conroe\Users"
              Set "destinationfolder=S:\MWELogs-%dtt%"
               
              :: copy files
              For %%a in ("%sourcefolder%\*.*") Do If exist "%%a" (
              xcopy /s /c /d /i /y "%%a" "%destinationfolder%\%dtt%-%%~na.*"
              )
               
              :: Disconnect network Drive
              if exist S: net use S: /d /y
               
              EndLocal
              To be honest, I can live with this. I just trying to understand why...
              This is scenario I working with:
              Conroe - local computer (Windows 2000 SP4) member in workgroup WORK. It running some application, that saved log file in \\Conroe\Users folder.
              Marsel - Litware domain computer (Windows XP SP3) have two NIC's. One for communication with Conroe, and another for communication within Domain Litware. I am running script on this computer.
              Paris - Litware domain file server (Windows 2K3), where log files should be copied from Conroe computer.

              And one additional question. Because Conroe computer not Domain member, before I start using scrip above I should connect to \\Conroe\Users folder from Marsel computer manually (from the Run menu), otherwise script not working... When I connect, it prompt's me username/password of Conroe user, who have access to Users shared folder. I know that I can map \\Conroe\Users also as network drive and provide user information in same line:

              net use R: \\Conroe\Users/ User:Conroe\Username password

              May be it can be done differently, without mapping \\Conroe\Users as network drive??
              Last edited by igor7; 12th December 2008, 05:56.

              Comment


              • #8
                Re: How to append time and date to file name?

                That's correct - support in batch files and at the command prompt for UNC paths is highly limited. Try browsing one at the prompt with cd - you'll see that you get an error.

                To do what you want you'll have to map a drive letter as you've suggested, or change the script to something like VBS.
                Gareth Howells

                BSc (Hons), MBCS, MCP, MCDST, ICCE

                Any advice is given in good faith and without warranty.

                Please give reputation points if somebody has helped you.

                "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

                "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

                Comment


                • #9
                  Re: How to append time and date to file name?

                  Originally posted by gforceindustries View Post
                  - support in batch files and at the command prompt for UNC paths is highly limited. Try browsing one at the prompt with cd - you'll see that you get an error.
                  The limitation is with a 'few' commands only. Mostly when you cannot use UNC-paths in a batch it is becauce of the fact that a UNC path never can be set as the working directory.

                  If there is a reason to change directory in the batch, then rather use PushD and PopD instead of using net use commands for network drives where you have to assin a static drive letter, and is slow in disconnecting the drive.
                  PushD and PopD can also be used for local drives instead of CD /d


                  My script should work without mapping network drives, xcopy support unc-paths and the batch does not change its working directory.

                  This is what went wrong,

                  In the first batch you do this:
                  Set "destinationfolder=\\Paris\WMELogs-%dtt%"
                  (that is causing "the invalid drive specification")

                  While the second batch you have changed the sharedfolder!
                  net use S: \\Paris\WMELogs
                  Set "destinationfolder=S:\MWELogs-%dtt%"

                  So in the first batch you should have defined the destinationfolder like:
                  Set "destinationfolder=\\Paris\WMELogs\MWELogs-%dtt%"


                  \Rems

                  This posting is provided "AS IS" with no warranties, and confers no rights.

                  __________________

                  ** Remember to give credit where credit's due **
                  and leave Reputation Points for meaningful posts

                  Comment


                  • #10
                    Re: How to append time and date to file name?

                    Hi, Rems!!
                    Thank you very match for explanation!! As usual it makes sense now!
                    But I still have a problem... When user attempt to run bat file for first time, it not working because it can't connect to \\Conroe\Users folder. So, for each user, who running this script first time from Marsel computer it not working, until this user not connect to Users share on Conroe manually from Run menu:



                    And after user connect manually everything work as expected. I'm trying avoiding such as behavior and include to the bat file command line for mapping \\Conroe\Users share with appropriate user credentials (who has access to this share,- local Conroe user, that I created before). But when I running this command:

                    net use R: \\Conroe\Users/User:Conroe\test test

                    it returns me following error:



                    But when I running Net use R:\\Conroe\Users command from cmd window it asks me for username and password and then successfully map the network drive:



                    I found vbs script that can map network drive with special user credential:

                    Code:
                    ' MappingNetworkDrive.vbs
                    ' VBScript to map a network drive with all 5 arguments. 
                    ' ----------------------------------------------------------------------' 
                    Option Explicit
                    Dim objNetwork 
                    Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile
                    ' Values of variables set
                    strDriveLetter = "S:" 
                    strRemotePath = "\\Conroe\Users" 
                    strUser = "test"
                    strPassword = "test"
                    strProfile = "false"
                    Set objNetwork = WScript.CreateObject("WScript.Network") 
                    objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
                    strProfile, strUser, strPassword
                    and it works for me. Then I add to this vbs script ability execute bat within itself:

                    Code:
                    'Calling bat file
                    dim shell
                    set shell=createobject("wscript.shell")
                    shell.run "SaveLogFile.bat",1, True
                    set shell=nothing
                    But as I said, I trying figure out why this command: net use R: \\Conroe\Users/User:Conroe\test test not working???
                    Thanks for you help!!
                    Last edited by igor7; 16th December 2008, 05:35.

                    Comment


                    • #11
                      Re: How to append time and date to file name?

                      [quote=igor7;139022]
                      net use R: \\Conroe\Users/User:Conroe\test test

                      [/code]

                      You are missing a space character between "\users" and "/user".

                      The correct syntax would be:

                      Code:
                      net use r: \\conroe\users /user:conroe\test test
                      -vP

                      Comment


                      • #12
                        Re: How to append time and date to file name?

                        more:
                        http://technet.microsoft.com/en-us/l.../bb490717.aspx

                        note,
                        The password may not contain any spaces or characters/signs that have a special function for dos commands. The command line interpreter interpret certain signs before executing a command, like the percent sign and the redirection symbols ( < | > ).


                        \Rems

                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        __________________

                        ** Remember to give credit where credit's due **
                        and leave Reputation Points for meaningful posts

                        Comment


                        • #13
                          Re: How to append time and date to file name?

                          Thanks guys!! It really was stupid spelling mistake!! Now it works fine!
                          BTW. When I searched for solution for my problem I found useful link with explanation of possible cause of "The network name cannot be found"
                          error. Here it is: http://support.microsoft.com/kb/875441. I hope it will be useful for somebody else...
                          Last edited by igor7; 19th December 2008, 21:46.

                          Comment


                          • #14
                            Re: How to append time and date to file name?

                            Hi!!
                            Can you, guys, help me one more time? Our engineers ask me, if it possible also provides a solution for deleting files\folders from destination folder (\\Paris\WMELogs\) older than 6 month? How it can be done with already existing vbs or bat file? Thank you in advance,
                            Igor.

                            Comment


                            • #15
                              Re: How to append time and date to file name?

                              Google? http://www.google.co.uk/search?q=win...elete+old+file

                              Try Janitor http://lifehacker.com/software/lifeh...ipt-288443.php
                              Gareth Howells

                              BSc (Hons), MBCS, MCP, MCDST, ICCE

                              Any advice is given in good faith and without warranty.

                              Please give reputation points if somebody has helped you.

                              "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

                              "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

                              Comment

                              Working...
                              X