locked
Listing log file in dd_mm_yy.txt format RRS feed

  • Question

  • Hi everyone,

    I want to list log files whose names are changing daily in command line. Like that; 

    C:\log\dd_mm_yy.txt 

    "dd_mm_yy.txt" is changing daily. What kind of a definition I must use in Windows command prompt to list the log file ? Could you please help me about how can I solve that problem? 

    Best Regards 
    Murat 
    Friday, May 9, 2014 8:41 PM

Answers

  • So it's like I said: It depends on the date format.

    Try it this way:


    echo %DATE:~0,2%_%DATE:~3,2%_%DATE:~8,2%


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Friday, May 9, 2014 10:30 PM
    • Marked as answer by Bill_Stewart Friday, May 30, 2014 9:56 PM
    Friday, May 9, 2014 10:23 PM

All replies

  • PowerShell:


    get-childitem ("C:\log\{0:MM_dd_yy}.txt" -f (get-date))
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Friday, May 9, 2014 9:04 PM
    Friday, May 9, 2014 9:01 PM
  • PowerShell:


    get-childitem ("C:\log\{0:MM_dd_yy}.txt" -f (get-date))
    


    -- Bill Stewart [Bill_Stewart]

    Bill - dd_MM_yy


    ¯\_(ツ)_/¯

    Friday, May 9, 2014 9:04 PM
  • Right, my code is provided as a guideline.

    -- Bill Stewart [Bill_Stewart]

    Friday, May 9, 2014 9:09 PM
  • Thanks for reply.  Is that working in  Windows command prompt? I need to run in command line prompt

    Best Regards

    Murat

    Friday, May 9, 2014 9:09 PM
  • As noted, the command is PowerShell.

    Why do you need it to run in Cmd.exe? Just use PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 9, 2014 9:11 PM
  • Because I have limited acces to server where I can only use cmd.exe, there is no PowerShell 
    Friday, May 9, 2014 9:16 PM
  • Are you sure? PowerShell is installed by default on Windows Server 2008 R2 and later.

    That aside, what does your script look like so far?


    -- Bill Stewart [Bill_Stewart]

    Friday, May 9, 2014 9:24 PM
  • I use 2003 server and  I only can use cmd.exe
    Friday, May 9, 2014 9:40 PM
  • PowerShell 2.0 can be installed on Windows Server 2003.

    And you still have not told what you've tried so far.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 9, 2014 9:49 PM
  • In linux command line similarly I can use below command, but in Windows I couldnt do

    ls -d /usr/local/logs/*error*.$(date +%Y-%m-%d) 

    Friday, May 9, 2014 9:51 PM
  • In linux command line similarly I can use below command, but in Windows I couldnt do

    ls -d /usr/local/logs/*error*.$(date +%Y-%m-%d) 

    set /?

    look at string manipulation

    also:

    echo %date%

    We will eventually break you Unix guys of your old bad habits.  In Windows we actually have all of those things.  In Unix you only have some of them.

    Don't feel bad.  PowerShell is almost ready to run on all versions of Linux.


    ¯\_(ツ)_/¯

    Friday, May 9, 2014 9:59 PM
  • C:\scripts>echo %date:~7,2%-%date:~4,2%-%date:~12,2%.txt
    09-05-14.txt

    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Friday, May 9, 2014 10:03 PM
    Friday, May 9, 2014 10:03 PM
  • I checked PowerShell, you are right, itcan be installed, but  as I sait at first I have to run this only command in cmd.exe, of course if it's possible. I might not have too much experience in cmd.exe so I couldn't try even if i wanted to.

    Friday, May 9, 2014 10:04 PM
  • If you insist on cmd.exe, as jrv showed, it can be done, but the syntax is a bit tortured.

    One thing to note about jrv's solution is that it's locale dependent--the order of the month, day, and year in the %DATE% string can change depending on your localization. This may or may not be important in your case.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 9, 2014 10:08 PM
  • the output is below, could you please check?

    C:\log>echo %date%
    10.05.2014

    C:\log>echo %date:~7,2%-%date:~4,2%-%date:~12,2%.txt
    01-5.-.txt


    Friday, May 9, 2014 10:19 PM
  • So it's like I said: It depends on the date format.

    Try it this way:


    echo %DATE:~0,2%_%DATE:~3,2%_%DATE:~8,2%


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Friday, May 9, 2014 10:30 PM
    • Marked as answer by Bill_Stewart Friday, May 30, 2014 9:56 PM
    Friday, May 9, 2014 10:23 PM
  • the output is below, could you please check?

    C:\log>echo %date%
    10.05.2014

    C:\log>echo %date:~7,2%-%date:~4,2%-%date:~12,2%.txt
    01-5.-.txt


    C'mon.  Your a Unix expert.  That should be easy ;)

    type "Set /?" at a prompt and read how to parse strings.  Don't be a helpless Unix Nerd.

    Note that I was using Unix before you were born.  The syntax in CMD comes partly from the Unix Shell.  Not ksh but one of the earlier shells.  IBM borrowed it for COMMAND.COM.


    ¯\_(ツ)_/¯

    Friday, May 9, 2014 10:30 PM
  • It work as I want. Thank you ver much Bill and Jrv.

    Best Regards

    Murat

    Friday, May 9, 2014 10:35 PM
  • It work as I want. Thank you ver much Bill and Jrv.

    Best Regards

    Murat


    Good luck and don't give up on Windows.  It was partially cloned from Unix.

    ¯\_(ツ)_/¯

    Friday, May 9, 2014 10:50 PM