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

Author Topic: .bat files  (Read 3226 times)

0 Members and 1 Guest are viewing this topic.

loosecannon

  • Guest
.bat files
« on: March 11, 2010, 09:02:12 PM »
Hi

i am trying to creat at batch file that will add the date/time into a file name, i need to keep the origional file name intact but have the date/time reference to avoid duplication

any ideas?

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: .bat files
« Reply #1 on: March 12, 2010, 06:26:08 AM »
This is depends on so many things, one of which is your local date format. I'm really not understanding the question. As you pointed out, you can't have duplicate file names in a folder, so adding the date/time really does nothing. This then becomes more a function of what program(s) are adding files to the folder. It is these programs that should be adding the date/time stamps as part of the file name.

This little snippet will tack on date/time to existing files in a folder. Test this in a trash directory as the files names look rather funky.

Code: [Select]
@echo off
setlocal enabledelayedexpansion
pushd %cd%
cd /d c:\temp

set search=/
set replace=-

set search1=:
set replace1=.

for /f %%i in ('dir /a-d /b') do (
  set newName=%%~ni-%date%-%time%
  set newName=!newName:%search%=%replace%!
  set newName=!newName:%search1%=%replace1%!
  ren "%%i" "!newName!%%~xi"


popd

You need to change the cd (4th lne) directory to something appropriate. 

Good luck.  8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

greg



    Intermediate

    Thanked: 7
    Re: .bat files
    « Reply #2 on: March 14, 2010, 02:30:32 PM »
    I am trying to create a batch file that will add the date/time into a file name, I need to keep the original file name intact but have the date/time reference to avoid duplication.


    C:\>type sidesnake.bat

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion

    for /f %%i in ('dir /a-d /b  a*.txt') do (

    set newName=%%i


    set newName=!newName!-%date%-%time%
    echo newName = !newName!

     rem  copy "%%i" "!newName!%%~xi"
    )

    Outout:



    C:\>sidesnake.bat
    newName = a-hire-0824.txt-Sun 03/14/2010-15:23:20.04
    newName = a-hire.txt-Sun 03/14/2010-15:23:20.04
    newName = aaa.txt-Sun 03/14/2010-15:23:20.04
    newName = abc.txt-Sun 03/14/2010-15:23:20.04
    newName = abc2.txt-Sun 03/14/2010-15:23:20.04
    newName = abc5.txt-Sun 03/14/2010-15:23:20.04
    newName = alwhite.txt-Sun 03/14/2010-15:23:20.04
    newName = ans.txt-Sun 03/14/2010-15:23:20.04
    newName = apple.txt-Sun 03/14/2010-15:23:20.04
    newName = arc.txt-Sun 03/14/2010-15:23:20.04
    C:\>

    Input:

    a-hire-0824.txt
    a-hire.txt
    aaa.txt
    abc.txt
    abc2.txt
    abc5.txt
    alwhite.txt
    ans.txt
    apple.txt
    arc.txt
    Have a Nice Day

    greg



      Intermediate

      Thanked: 7
      Re: .bat files
      « Reply #3 on: March 14, 2010, 09:27:17 PM »
      I need to keep the original file name intact but have the date/time reference to avoid duplication

      "set newName=%%~ni-%date%-%time%"
      %date% as part of file name will not copy or ren ( rename ). Certain characters are not allowed as part of file name.
      A file name cannot contain any of the following characters:

      / \ : * ? “ < >

      C:\>type  sidew.bat
      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      for /f "tokens=1-4 delims=/- " %%a in ('date /t') do (
      set mm=%%b
      set dd=%%c
      set yy=%%d
      )

      for /f "delims=" %%i in ('dir /b  a*.txt') do (
      echo  %%~ni
      set newName=%%~ni


      set newName=!newName!!mm!!dd!!yy!.txt
      echo newName = !newName!

      copy "%%i" "!newName!"
      )

      Output:

      C:\>sidew.bat
       abc
      newName = abc03142010.txt
              1 file(s) copied.
       abc2
      newName = abc203142010.txt
              1 file(s) copied.
       abc5
      newName = abc503142010.txt
              1 file(s) copied.
       ans
      newName = ans03142010.txt
              1 file(s) copied.
       arc
      newName = arc03142010.txt
              1 file(s) copied.

      C:\>dir  arc*.txt
       Volume in drive C has no label.
       Volume Serial Number is F4A3-D6B3

       Directory of C:\

      12/13/2009  10:03 PM                41 arc.txt
      12/13/2009  10:03 PM                41 arc03142010.txt
                     2 File(s)             82 bytes
                     0 Dir(s)  297,515,442,176 bytes free

      C:\>
      « Last Edit: March 14, 2010, 09:46:28 PM by greg »
      Have a Nice Day

      greg



        Intermediate

        Thanked: 7
        Re: .bat files
        « Reply #4 on: March 15, 2010, 11:30:41 AM »
        I am trying to create at batch file that will add the date/time into a file name, I need to keep the original file name intact but have the date/time reference to avoid duplication

        The following is Sidewinder's code ( post 1) with slight modification and the output is listed.  What does pop and push do?  How does set search=/  and set replace=- work?


        C:\batch>type loose.bat
        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        pushd %cd%

        set search=/
        set replace=-

        set search1=:
        set replace1=.

        for /f %%i in ('dir /a-d /b a*.txt') do (
          set newName=%%~ni-%date%-%time%
          set newName=!newName:%search%=%replace%!
          set newName=!newName:%search1%=%replace1%!
          copy "%%i" "!newName!%%~xi"
        )

        popd

        Output:

        C:\batch>loose.bat
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
                1 file(s) copied.
        C:\batch>dir  a*Mon*.txt
         Volume in drive C has no label.
         Volume Serial Number is F4A3-D6B3

         Directory of C:\batch

        11/10/2009  03:24 PM                36 abc-Mon 03-15-2010-12.23.10.21.txt
        11/13/2009  01:21 PM               198 abc2-Mon 03-15-2010-12.23.10.21.txt
        11/17/2009  05:36 PM               290 abc5-Mon 03-15-2010-12.23.10.21.txt
        03/12/2010  08:44 PM                89 all_a-Mon 03-15-2010-12.23.10.21.txt
        12/21/2009  01:29 PM               144 arc-Mon 03-15-2010-12.23.10.21.txt
        02/07/2010  04:44 PM                94 army-Mon 03-15-2010-12.23.10.21.txt
        12/24/2009  09:58 PM               269 awkinfile-Mon 03-15-2010-12.23.10.21.txt
        12/23/2009  10:56 PM                39 awlinfile-Mon 03-15-2010-12.23.10.21.txt
                       8 File(s)          1,159 bytes
                       0 Dir(s)  297,769,824,256 bytes free

        C:\batch>
        Have a Nice Day