0

I am trying to get the creation date and time of a specific folder in Windows. Based on this answer, I have tried the following:

@echo off
set path_of_folder="C:\folderA\folderB\"

if exist %path_of_folder% ( 
  echo Path exists.
  for /f "skip=5 tokens=1,2 delims= " %%a in ('dir /a-d /tc %path_of_folder%') do set "dt=%%a"
    echo Created on: %%a, %%b
  )
) 
else ( 
  echo Path does not exist. 
)

pause

Where I get this output:

Path exists.
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 4, File(s)
Created on: 0, Dir(s)

Which I am sure it shows the creation dates for each of the files (4 for my example) inside the folderB.

I am looking on saving the creation date and time only for the top folder folderB; can anyone suggest/show how this can be achieved and not get all the other creation date/times as well?

Note that on the answer given on the attached link, there is a exit /b 0 right after the echo Created on: %%a, %%b command which I cannot add on my script since there are several other commands I want to execute after I get the system date and time.

  • If you hover the mouse over the tag [batch-file] and then click on info you will get to the `[batch-file] tag wiki` page with additional links and information. –  Jul 05 '17 at 18:23

2 Answers2

0
@ECHO OFF
SETLOCAL
set "path_of_folder=C:\sql2016\mssql13.sqlexpress03"

if exist "%path_of_folder%" ( 
  echo Path exists.
  for /f "skip=5 tokens=1,2,4 delims= " %%a in (
   'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." (
    set "dt=%%a"
    echo Created on: %%a, %%b
  )
) else ( 
  echo Path does not exist. 
)

GOTO :EOF

I used a different target directory and since I run from the prompt, I've replaced the pause.

Note that the variable containing the path now has no closing \, nor is it quoted. This facilitates making modifications, but means that where required, references to that variable need to be "quoted".

Simply pick up the name part of the directory in %%c. Therefore you need a listing of directories hence /ad. The directoryname of interest is . so only execute the assignment and report if the directoryname found is "."

The skip=5 is redundant and can be removed if you prefer. the if filter ensures that the . directory is reported.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Perfect, thanks! And only because it is a struggle to find some documentation online that states what each attribute in a command does (e.g. `skip` above), I would appreciate if you could provide a very short explanation or even a link that could be used to understand the `for /f "skip=5 tokens=1,2,4 delims= " %%a in ( 'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." ( set "dt=%%a"` command. –  Jul 05 '17 at 14:46
  • 1
    From the command prompt, use `for /?|more` for an explanation of how the various `for` options work - in general, *command*/? will produce documentation on the command. The `set dt=` statement sets a variable named `dt` to the date portion of the date/time reported. I have no idea why of whether it is required - it was present in your original code as posted, so I retained it. – Magoo Jul 05 '17 at 15:00
  • Quick follow-up: I am trying to create 2 variables for the date and time (right after the 9th line) like so: `set "a=%%a"` and `set "b=%%b"` but it does not work, i.e. both `a` and `b` remain empty. I looked into the `%%` syntax at https://superuser.com/questions/670992/what-does-the-percent-sign-and-in-a-batch-file-argument-mean but did not understood. Am I missing something? –  Jul 06 '17 at 10:38
  • Depends a lot on context. If the correct time and date are being displayed by the `echo` then you may be falling into the `delayed expansion` trap (Use the `search` facility on the top bar for more info). The `setlocal` at the start of the code will also back out any changes made to the environment made by the batch, so if you are looking from the command prompt at the values set by this routine, then the `setlocal` will back out the changes (done deliberately to keep the environment clean) - if you want to retain such variables, insert `enclocal&set "a=%a%"&set "b=%b%"` before the `goto :eof` – Magoo Jul 06 '17 at 13:06
0

Check dirtimejs.bat - it can print dir times independent from control panel settings:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "path_of_folder=C:\folderA\folderB"

for /f "tokens=1* delims=:" %%a in ('dirtimejs.bat "%path_of_folder%" ^| find "Created :"') do (
     echo %%a
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187