I want to use dir command and have filename as 1st column, creation date time as 2nd column and modified date time as 3rd column.
How could I achieve this?
http://www.computerhope.com/dirhlp.htm
This shows /T might be used but not sure how to use it because dir /TCAW doesn't list the format I need.
As Christopher Painter says, the DIR command cannot do this directly.
But there is a simple command line one liner that displays your information without header or footer info. This command lists only files. It works as long as your locality displays time stamps using 3 space delimited components. For example, my U.S. time stamps display as mm/dd/yyyy hh:mm:ss am.
for /f "tokens=1-4*" %A in ('dir /a-d /tc^|findstr "^[0-9]"') do #echo %E %A %B %C %~tE
Change /a-d to /a if you want to include directories. Or simply remove /a-d entirely if you want both files and directories but you want to exclude hidden and system files/directories.
Here is the same command in a nicely formatted batch script:
#echo off
for /f "tokens=1-4*" %%A in (
'dir /a-d /tc^|findstr "^[0-9]"'
) do echo %%E %%A %%B %%C %%~tE
I don't like the output format with the file name in the front because the width of the name varies - it is hard to read the output because the columns don't line up. I prefer to put the file name at the end:
#echo off
for /f "tokens=1-4*" %%A in (
'dir /a-d /tc^|findstr "^[0-9]"'
) do echo %%A %%B %%C %%~tE %%E
DIR doesn't support what you are trying to do. the /T:Fileld ID can only show one set of time information at a time. I'm not sure what you are trying to do but I see you have some C# experience. You could write your only console app that outputs the way you out and call that instead.
# ECHO OFF
(FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
FOR /f "tokens=1-3*" %%x IN ('dir /a-d /tc "%%~a"^|findstr "^[0-9]"') DO (
ECHO "%%a",%%~ta,%%x %%y %%z
)
)) > DIR.txt
TYPE DIR.txt
the answer in refered to : Windows batch file to create csv list of file and dates
Related
I have some files that I would like to sort through and keep the newest file.
I cannot do it by file attributes date modified or created, which I could do no problem.
Here is the naming convention of the files. FileABC_YYYYMMDD.txt
FileABC_20190201.txt
FileABC_20190125.txt
FileABC_20190118.txt
FileABC_20190111.txt
FileABC_20190104.txt
You can see that the date stamp is in the filename itself. These files are generated weekly. So I'd like to have a batch file loop through them and delete all but most currently dated file. I have really searched for how to do this best and I'm not finding much so I need ideas. I prefer a pure cmd solution but I'm open to powershell solutions as well.
What I am trying on my own is to parse out the date with...
#echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%G IN ('dir/b /a-d "C:\Users\thomas.maus\Documents\Tom\dev\Test Batch Files\dev\sortbyFileDateName\FileABC_*.txt"') do (
set fileName=%%G
Set theDate=!fileName:~8,8!
echo !theDate!
)
Then I want to take those dates somehow from the results of the loop and do something like
if "%theDate%" GEQ "*****not sure what to put here*****" (
del *all the old files, also not sure what to put here*
)
How about this?
#echo off
for /f "skip=1" %%i in ('dir /o-n /b *.txt') do del %%i
If you just want to test it (see what it would delete) first, do:
#echo off
for /f "skip=1" %%i in ('dir /o-n /b *.txt') do echo %%i
If you do not care about the file dates but only the dates in the file names, you could do the following, given that the part FileABC is always the same and does not contain any _ on its own:
pushd "C:\Users\thomas.maus\Documents\Tom\dev\Test Batch Files\dev\sortbyFileDateName" && (
for /F "skip=1 delims= eol=|" %%F in ('
dir /B /A:-D "FileABC_????????.txt" ^
^| sort /R
') do (
del "%%F"
)
popd
)
Although sort /R does alphabetic sorting, this works because of your chosen date format, which ensures that alphabetic order equals alphanumeric one.
We just loop through the files, sorted by date in decending order, then skip the first file, now being the latest:
#for /f "skip=1" %%a in ('dir /b /o-d *.txt') do #echo #del %%a
Important!
This example will only echo the delete command as a safe measure so you do not delete files you should not have. To perform the actual delete, remove #echo from the line.
To understand more about the functions we used, run the following from cmd.exe
for /?
dir /?
As an additional option, just in case the filename prefix changes throughout and only the _YYYYMMDD.txt remains constant, you can still peform the task using that date as it is already alphabetically sortable.
Here's an example:
#Echo Off
Set "SrcDir=%UserProfile%\Documents\Tom\dev\Test Batch Files\dev\sortbyFileDateName"
For /F "Delims==" %%A In ('Set $ 2^>Nul') Do Set "%%A="
Set "_="
For /F Delims^=^ EOL^= %%A In ('Where "%SrcDir%":*_????????.txt 2^>Nul'
) Do Set "_=%%~nA" & Call Set "$%%_:~-8%%=%%A"
If Not Defined _ Exit /B
For /F "Tokens=1* Delims==" %%A In ('Set $ 2^>Nul') Do Set "_=%%B"
For /F Delims^=^ EOL^= %%A In ('Where "%SrcDir%":*_????????.txt^|Find /V "%_%"'
) Do Del /A /F "%%A"
This uses the fact that the Set command will output variables in alphabetical order.
Lines 2 to 4 just define and undefine the variables we will be using.
Lines 5 and 6 is a single line split over two lines for readability. This will set variables using the last eight characters of the files basenames, to the value of the full filename.
Line 7 is included to exit the script, just in case no .txt files with a basename ending with an underscore followed by eight characters were found in the directory set at line 2.
Line 8 is the special one here, it outputs each variable and corresponding value in alphabetical order. The output is set to a variable, which overwrites itself until the loop ends. This means that the newest file, last one alphabetically, is held with the value of the file named with the newest date.
Lines 9 & 10 are once again a single line split over two for readability. This loops over all matching files in the directory again and uses the Find command to exclude outputting the one which matches that held in the variable as the file with the newest date. Each file output is simply deleted using the Del command.
Please note that this script assumes you only have a single file with each date, as you've only stated that the files are generated weekly.
Want to write :
List item
A batch file which gives the most recent file based on date.
work in two steps: a) get the newest file and b) use it's date to filter your files:
#echo off
REM get the newest file (to get it's date):
for /f "delims=" %%a in ('dir /a-d /b /od /tw') do set "day=%%~ta"
REM show files, filtered by the found day:
for /f "tokens=4 delims= " %%a in ('dir /a-d^|find "%day:~0,10%"') do echo %%a
Windows, Command Prompt, need to generate a .txt file output containing of all files from a big and complex dir tree with one (1) line for each files as:
CreationDateYYYYMMDD-HHMMSS, LastModifiedYYYYMMDD-HHMMSS, filesize[no K commas], filename.ext
for example:
20100101-174503, 20120202-191536, 1589567, myfile.ext
The list should not contain lines of dir name entries, etc., only filenames, even if the same file is present in more than once. Time in 24 hours format.
dir /s/t:c/t:w/-c > filelist.txt
command does not exactly works this way.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\program files"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
FOR %%d IN (timewritten timecreated) DO SET "%%d="
FOR %%k IN (-d s h) DO (
IF NOT DEFINED timewritten FOR /f "tokens=1,2 delims= " %%d IN ('dir /tw %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timewritten=%%d %%e"
IF NOT DEFINED timecreated FOR /f "tokens=1,2 delims= " %%d IN ('dir /tc %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timecreated=%%d %%e"
)
ECHO !timecreated! !timewritten! %%~za %%~nxa
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
Interesting problem. This code processes it by
First, applying the standard directory-list for filenames on the tree from the relative root (%sourcedir%) to %%a
Using the full filename in %%a, set timewritten and timecreated from an ordinary dir list targeting the file in question.
It appeared that %%~ta didn't play nicely to extract the timestamp for hidden and system files, so I decided to build them from the ordinary dir listing with the appropriate t setting, specifically listing with /a-d, /as and /ah and filtering for the line which matched the filename, which seemed to extract the data appropriately.
I left the date/time in raw format. It should be an easy task to extract the various elements and construct the report in the format you want.
This question is a dupe of the SO post cmd dir /b/s plus date, but posting what worked for me:
#echo off
REM list files with timestamp
REM Filename first then timestamp
for /R %I in (*.*) do #echo %~dpnxI %~tI
#echo off
REM list files with timestamp
REM Timestamp first then name
for /R %I in (*.*) do #echo %~tI %~dpnxI
The above are the versions that you would directly paste into a command prompt.
If you want to use these in a batch file and log the output, you could do something like:
rem: Place the following in a batch file such as DirectoriesBareWithTS.cmd.
rem: As the first step in the batch file, net use to the directory or share you want the listing of
rem: Change to your target directory
Y:
for /R %%I in (*.mp4) do #echo %%~tI %%~dpnxI
Then you can pipe the output to a log file when you execute:
DirectoriesBareWithTS.cmd > C:\temp\GiantLongDirListing.log
You could then import that log into Excel.
Host: Win 7
I have a product that I updated today (patched if you will). I would like to see specifically which files were added to the base installation directory tree.
The expected result would ONLY display creation timestamp and full file path/name. I do not want to see directory summary information or any other breaks in the data. The output would be a very straight forward looking listing like this:
08/06/2012 11:02 AM c:\my_product_install_path\folder3\new_file_3.xml
08/06/2012 11:01 AM c:\my_product_install_path\folder2\new_file_2.c
08/06/2012 11:01 AM c:\my_product_install_path\new_file_1.h
...
...
I think from this top-down listing of the full install directory tree, I can easily see the new files from the old files by scrolling down and looking for the first file with time older than X.
How can I do this either at the Windows command shell (or in a cygwin shell)?
"dir /s /b" is very close but it does not display the creation time stamp.
You might think that "dir /s /b /T:C" would work since /T:C is for displaying the creation time but no. The /T:C option does not "overide" the /b option to display the timestamp So, somehow I need to get the benefits of the /b option with the timestamp added in.
This is a cumbersome native solution for the command line, but it works :-)
for /d /r %F in (.) do #pushd "%F"&(for /f "eol= delims=" %S in ('2^>nul dir /tc /a-d *') do #for /f "tokens=1-4*" %A in ("%S") do #echo %A %B %C %~fE)&popd
It looks better when formatted across multiple lines in a batch file.
#echo off
for /d /r %%F in (.) do (
pushd "%%F"
for /f "eol= delims=" %%S in ('2^>nul dir /tc /a-d *') do (
for /f "tokens=1-4*" %%A in ("%%S") do #echo %%A %%B %%C %%~fE
)
popd
)
You might want to sort the list in reverse chronological order so that all the new files are clustered at the top. It is trivial to do a chronological sort within each directory using the DIR /ODN option. But it is quite tricky to sort chronologically across all the directories. That requires parsing the time stamp info and reformatting it in a way that allows SORT to produce a chronological listing. Unfortunately, parsing the time stamp is very locale dependent.
Here is a batch solution that works with the OP's locale setting. It sorts first chronologically by creation timestamp, then alphabetically by full file path.
#echo off
setlocal
set "tempFile=%temp%\listCreateTime%random%.txt"
>"%tempFile%" (
for /d /r %%F in (.) do (
pushd "%%F"
for /f "eol= delims=" %%S in ('2^>nul dir /tc /a-d *') do (
for /f "tokens=1-6* delims=/ " %%A in ("%%S") do (
if "%%D"=="12:00" (
echo %%C%%A%%B%%E00:00%%~fG*%%A/%%B/%%C %%D %%E %%~fG
) else (
echo %%C%%A%%B%%E%%D%%~fG*%%A/%%B/%%C %%D %%E %%~fG
)
)
)
popd
)
)
for /f "tokens=2 delims=*" %%A in ('sort /r "%tempFile%"') do echo %%A
del "%tempFile%"
It is also possible to use WMIC to get and sort the information. It is much simpler because the timestamp is already formatted in a way that allows SORT to sort it chronologically, so there is no need to parse. It is also locale independent - it should work on any Windows machine in the world that supports WMIC. But this method is slower, and the ouput is not as easy to read. Of course extra coding could be added to parse out substrings from the timestamp and reformat to a more familiar form.
#echo off
setlocal disableDelayedExpansion
set "tempFile=%temp%\listCreateTime%random%.txt"
>"%tempFile%" (
for /d /r %%F in (.) do (
set "folder=%%~pnxF\"
set "drive=%%~dF"
setlocal enableDelayedExpansion
2>nul wmic datafile where "drive='!drive!' and path='!folder:\=\\!'" get name, creationDate|findstr /brc:[0-9]
endlocal
)
)
sort /r "%tempFile%"
del "%tempFile%"
The timestamp is in YYYYMMDDhhmmss.ddddddzzzz
YYYY = year
MM = month
DD = day
hh = hour in 24 hour format
mm = minutes
ss = seconds
dddddd = microseconds
zzzz = timezone info expressed as number of minutes difference from GMT
I need to generate lists of all files that has been created, modified and accessed respectively on a windows system.
I have found the command forfiles here http://technet.microsoft.com/sv-se/library/cc753551(v=ws.10).aspx but apparently this does not use the created and accessed timestamps, I need those also (in separate lists).
I have also looked into using the dir command but I have only found references to sorting and not filtering in the help sections.
Your question have some unclear details, so I made some assumptions.
The Batch file below create three separated text files: created.txt, modified.txt and accessed.txt, with the lists of the files in current folder; each list have the respective date in YYYYMMDDHHMMSS format followed by the file name, so it may be easily processed.
#echo off
setlocal DisableDelayedExpansion
for %%f in (created modified accessed) do if exist %%f.txt del %%f.txt
set "folder=%CD:~2%"
for /F "skip=1 tokens=1-3*" %%a in (
'"wmic datafile where (path='%folder:\=\\%\\') get InstallDate, LastModified,
LastAccessed, Name"'
) do (
for /F "delims=." %%A in ("%%a") do echo %%A %%d>> created.txt
for /F "delims=." %%B in ("%%b") do echo %%B %%d>> modified.txt
for /F "delims=." %%C in ("%%c") do echo %%C %%d>> accessed.txt
)
rem Process each list this way:
for /F "tokens=1*" %%a in (created.txt) do echo %%a - %%b
In the subject there was also "between dates" :) How about that?
forfiles doesn't allow to put 2 date conditions ('+' and '-').
I also have a problem with a date condition with unequalness in wmic.
for example:
wmic datafile where (path='%folder:\=\\\\%\\\\' and lastmodified ^> "20120713" and lastmodified ^< "20120714") get Name
returns nothing (I needed an escape ('^') before '<' and '>' to run the command).
wmic datafile where (path='%folder:\=\\\\%\\\\' and lastmodified ^> "20120713") get Name
returns only files modified after 20120714000000 (in the wmic notation).