Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check and copy files

    Hi Gurus,

    I've been searching on the Internet, but could not really find what I'm looking for.

    Would need some help in performing the following tasks using windows batch script.

    For the current month and year, example: August 2012, need to perform the followings:
    1) Check and create a directory called: Aug2012 if it does not exist.
    2) Check all files with extension *.AUD on D:\AuditData (source) and copy them to target directory. Target directory name

    is based on the respective month and year, for example: Aug2012. Overwrite the files if exist.

    Some samples:
    20120701.AUD
    20120702.AUD
    20120703.AUD
    20120704.AUD
    20120705.AUD

    20120801.AUD
    20120802.AUD
    20120803.AUD
    20120814.AUD
    20120815.AUD

    For all files with timestamp July 2012, copy them to Jul2012 directory.

    For all files with timestamp August 2012, copy them to Aug2012 directory.

    Please help to check and advise.

    Thank you for your help.



    - Jack
    Last edited by JackOne; 2012-08-06 at 22:35. Reason: Update the info

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Getting the month name is tricky in a batch file. I use a bit of VBS to do it.
    Code:
    set _TmpFile="%temp%\tmp.vbs"
    echo  n=Now>%_TmpFile%
    echo WScript.Echo WeekDayName(MonthName(n),True)>>%_TmpFile%
    for /f "delims=*" %%x in ('cscript //nologo %_TmpFile%') do set _month=%%x
    del %_TmpFile%
    You will also need the 2 digit month so you can pick out the correct files.
    Code:
    @echo off&SETLOCAL
    
    :: This will return date into environment vars
    :: Works on any NT/2K/XP machine independent of regional date settings
    :: 20 March 2002
    :: This works by taking advantage of a quirk in the date command 
    :: first noticed by Michael Jerkovic - the DATE command will display something like:
    :: The current date is: Fri 14/04/2000
    :: Enter the new date: (dd-mm-yy)
    :: The useful bit there is the (dd-mm-yy), on a machine with different 
    :: regional settings it may appear as (mm-dd-yy)
    
    FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :s_fixdate %%G %%H %%I %%J)
    goto :s_print_the_date
    
    :s_fixdate
    if "%1:~0,1%" GTR "9" shift
    FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
       set %%G=%1&set %%H=%2&set %%I=%3)
    goto :eof
    
    :s_print_the_date
    echo Month:[%mm%]  Day:[%dd%]  Year:[%yy%]
    ENDLOCAL&SET mm=%mm%&SET dd=%dd%&SET yy=%yy%
    Then you need to list the files and extract the timestamp.
    Code:
    for %X in (*.*) do echo %~nX.%~xX %~tX
    Last you need to find the correct files to move.
    Code:
    for /f "tokens=3 delims=/ " %X in ("file.ext 13/07/2012") do if %X.==07. echo OK
    cheers, Paul

  3. #3
    New Lounger
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Paul,

    Thanks for your response and help.

    I'll try to test it and hopefully it's working OK.


    - Jack

  4. #4
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    You can't just stick those fragments together and have it work. They are really only suggestions.

    cheers, Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •