Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
tinyevil

Batch file to export file name and created date

Recommended Posts

Of course I could,...

You see :), no need to make Coffefiend UNemployed :w00t::ph34r:

And now, for NO apparent reason, an unneededly complex script using WMIC! :yes:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
PUSHD
CD /D %1
set "thisfolder=%CD:~2%"
for /F "skip=1 tokens=*" %%A in (
'"wmic datafile where (path='%thisfolder:\=\\%\\') get InstallDate, Name"'
) do (
SET Line=%%A
CALL :parse_date
)
POPD

:parse_date
SET Y=%Line:~0,4%
SET M=%Line:~4,2%
SET D=%Line:~6,2%
SET Name=%Line:~27%
ECHO %D%/%M%/%Y% - %Name%
GOTO :EOF

jaclaz

Share this post


Link to post
Share on other sites

Here is a VBS Script that, you Drag & Drop the folder that you want to list in below format. This will

go threw all Folders And Sub Folders, and list all contents.

Source Folder Path

1\ Name

2:\ Created

3\ Accessed

4\ Modified

Save As List_File_Directory.vbs

'-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.'-> Object For Run TimeDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> Varibles For Script Run TimeDim Ar, Dr, Ln, Ts, TxtAr = Chr(160) & Chr(187) & Chr(160)Ln = "--------------------------------------------------------"'-> Makes Sure Only One Object Drag And DropSelect Case WScript.Arguments.CountCase 0call Msg(vbTab & "Error No Folder" & vbCrLf & _"To Use This Script Drag And Drop An" & vbCrLf & _"Single Folder Onto This Script.","Error 1")Case 1'-> Filter Out File From FolderIf Right(InStr(WScript.Arguments.Item(0),"."),6) Thencall Msg(Space(26) & "File Drag Drop" & vbCrLf & _" You Have Drag & Drop A File Onto This Script." & vbCrLf & _"Script Requires Only One Folder To Be, Drag &" & vbCrLf & _"Drop To Make Active","Error 3")ElseDr = WScript.Arguments.Item(0)call Msg("Preparing To List This Folder : " & Dr, "List Contents", 4128)Txt = Act.SpecialFolders("DeskTop") & "\List_Item.txt"Set Ts = Fso.CreateTextFile(Txt)Ts.writeline vbTab & "Scan Time" & Ar & Time()Ts.writeline vbTab & "Scan Date" & Ar & Date()Ts.writeline vbTab & "Scan Path" & Ar & DrRecursive(Fso.GetFolder(Dr))Ts.WriteLine LnTs.CloseAct.Run(Txt),1,Truecall Msg("Did You Want To Keep This File : " & Fso.GetFile(Txt).Name & vbCrLf & _"No To Delete This File, Yes To Keep File If Nothing Is Select" & vbCrLf & _"In 5 Seconds, This Script Will Close And Save The File","Yes To Keep - No To Delete",4132)End IfCase Elsecall Msg(Space(25) & "Error Exceeds Limit" & vbCrLf & _" User Has Drag And Drop " & WScript.Arguments.Count & _" Objects On To This Script." & vbCrLf & "This Script Was Meny To" & _" Process Only One Folder, At" & vbCrLf & " Script Run Time", _"Error 2", 4128)End Select'-> Msgbox Function With 5 Second TimeOutFunction Msg(Tx, Tn, Btn)Select Case BtnCase 4128Act.Popup Tx, 5, Tn, 4128Case 4132If Act.Popup(Tx, 5, Tn, 4132) = 7 Then Fso.DeleteFile(Txt), TrueEnd selectEnd Function'-> Recusive Threw Folder And All Sub FoldersFunction Recursive(Folder)Ts.WriteLine LnTs.WriteLine " Folder Path " & Ar & FolderFor Each Obj In Folder.FilesTs.WriteLine LnTs.WriteLine " File Name " & Ar & Obj.NameTs.WriteLine " Date Created " & Ar & Obj.DateCreatedTs.WriteLine " Last Accessed" & Ar & Obj.DateCreatedTs.WriteLine " Last Modified" & Ar & Obj.DateCreatedNextFor Each Dir In Folder.subFoldersRecursive(Dir)NextEnd Function

Share this post


Link to post
Share on other sites

Here is a VBS Script that, you Drag & Drop the folder that you want to list in below format. This will ....

Very good :), everything is back to normality :thumbup:

Now that the OP issue is solved, can we go a step ahead (or aside :unsure:)?

The WMIC thingie I posted is seemingly "absurd" (using the "containing folder and "Installdate" :w00t:), but I got to it because I had issues with:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET TargetDir="%*"
SET Targetdir
PUSHD
CD /D %TargetDir%
REM DIR
Pause
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B %TargetDir%') DO CALL :getCreationDate "%%A"
POPD
goto :eof

:getCreationDate
set FILE=%~f1
set FILE="%FILE:\=\\%"
FOR /F "skip=1 tokens=*" %%B IN (
'"wmic datafile where (name=%FILE%) get creationdate, name"'
) DO (
SET Line=%%B
SET Line=!Line:~6,2!/!Line:~4,2!/!Line:~0,4! !Line:~26!
ECHO !LINE!
)
goto :eof

Namely the batch is "borked" if a name conataining brackets is found.

I tried a few alternative things in the FOR loop, including attempting using the usebackq parameter, but could not find any solution. :(

jaclaz

Share this post


Link to post
Share on other sites
but I got to it because I had issues with:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET TargetDir="%*"
SET Targetdir
PUSHD
CD /D %TargetDir%
REM DIR
Pause
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B %TargetDir%') DO CALL :getCreationDate "%%A"
POPD
goto :eof

:getCreationDate
set FILE=%~f1
set FILE="%FILE:\=\\%"
FOR /F "skip=1 tokens=*" %%B IN (
'"wmic datafile where (name=%FILE%) get creationdate, name"'
) DO (
SET Line=%%B
SET Line=!Line:~6,2!/!Line:~4,2!/!Line:~0,4! !Line:~26!
ECHO !LINE!
)
goto :eof

Namely the batch is "borked" if a name conataining brackets is found.

I tried a few alternative things in the FOR loop, including attempting using the usebackq parameter, but could not find any solution. :(

jaclaz

First of all you are asking your script to do too much work!

Instead of creating a command to list each file and performing another command on each file, you can work directly on the input directory.

Once you've reduced the work you simply need to change the way you format your WMIC syntax, (remember too that the comma needs escaping)

Try this:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
SET "P_=%~1"
SET "P_=\%P_:*\=%\"
FOR /F "USEBACKQ TOKENS=1*" %%# IN (`WMIC DATAFILE WHERE^
"DRIVE='%~d1' AND PATH='%P_:\=\\%'" GET CREATIONDATE^, NAME^|FIND /I "%~d1"`
) DO CALL :_O "%%#" "%%$"
PAUSE & GOTO :EOF
:_O
SET "D_=%~1"
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~2

Share this post


Link to post
Share on other sites

Try this:

I'll do and report, as soon as I am back to the machine I normally work with, thanks :).

But though - as always - very nice :thumbup, it is a (much better) workaround.

I mean your script still uses "directory" info and not "filename", set aside the OP request, if instead of looking for the created date of the contents of a directory I want to look for the created date of a single file, I need to use the "WHERE Name=" approach.

What I am missing (and I want to understand fully) is the right way to escape/quoting everything in the FOR loop.:unsure:

I'll study your approach, if I get it right is:

  • use USEBACKQ
  • enclose the whole set of arguments of WHERE in double quotes
  • enclose the values of each single comparison inside the WHERE in in single quotes
  • escape the commas (besides - as always - the pipe symbol)

and see if it works with the filename containing brackets

jaclaz

Share this post


Link to post
Share on other sites

I mean your script still uses "directory" info and not "filename", set aside the OP request, if instead of looking for the created date of the contents of a directory I want to look for the created date of a single file, I need to use the "WHERE Name=" approach.

My reading of your intention was to provide the date created for each file within a 'input' directory. Just drag and drop a directory onto the script, it should do just that.

Share this post


Link to post
Share on other sites

Working perfectly :):thumbup

Here is the very slighlty modified version that deals with single file:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
IF NOT EXIST %1 GOTO :EOF
SET "P_=%~1"
FOR /F "SKIP=1 USEBACKQ TOKENS=1*" %%# IN (
`WMIC DATAFILE WHERE "NAME='%P_:\=\\%'" GET CREATIONDATE^, NAME`
) DO CALL :_O "%%#" "%%$"
PAUSE & GOTO :EOF
:_O
SET "D_=%~1"
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~2

jaclaz

Share this post


Link to post
Share on other sites
Here is the very slighlty modified version that deals with single file:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
IF NOT EXIST %1 GOTO :EOF
SET "P_=%~1"
FOR /F "SKIP=1 USEBACKQ TOKENS=1*" %%# IN (
`WMIC DATAFILE WHERE "NAME='%P_:\=\\%'" GET CREATIONDATE^, NAME`
) DO CALL :_O "%%#" "%%$"
PAUSE & GOTO :EOF
:_O
SET "D_=%~1"
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~2

You could improve on that too because there's no need to create a command which gives you something you already know, i.e. the files name.

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
SET "P_=%~1" & SET "D_="
FOR /F "USEBACKQ" %%# IN (`WMIC DATAFILE WHERE "NAME='%P_:\=\\%'" GET^
CREATIONDATE^|FIND "+"`) DO SET D_=%%~#
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~nx1
PAUSE

You could obviously replace %~nx1 with %~f1 or %P_% if you prefer

Share this post


Link to post
Share on other sites

You could improve on that too because there's no need to create a command which gives you something you already know, i.e. the files name.

Yep, of course :).

I am a bit perplexed by the

FIND "+"

I had gathered that the offset could be both + and - (plus and minus) :unsure:

http://technet.microsoft.com/library/ee198928.aspx

jaclaz

Share this post


Link to post
Share on other sites

So to adjust the code to work in the US you change the "+" to "-"? And if you wanted to make the code general purpose that would work everywhere?

Cheers and Regards

Share this post


Link to post
Share on other sites

if you wanted to make the code general purpose that would work everywhere?

Then you also have to worry about difference in the date/time formats as well when you parse that text (it changes with different cultures/locales). There's so many ways to accomplish that stuff in most languages. A couple quick PowerShell examples:

For output with a standard ISO time format:

ls C:\path\here\|sort CreationTime|%{$_.CreationTime.ToString("s")+" "+$_.Name}

For output with a custom YYYY-MM-DD format (good for sorting):

ls C:\path\here|sort CreationTime|%{$_.CreationTime.ToString("yyyy-MM-dd")+" "+$_.Name}

Same ISO format output, but with code written in a different way:

ls C:\path\here|sort CreationTime|%{[string]::Format("{0:s} {1}",$_.CreationTime,$_.Name)}

There's *so* many other ways to do it, using so many languages. It's very simple in in VBScript as well, except for the sorting part where you have to write your own sort function...

Share this post


Link to post
Share on other sites
So to adjust the code to work in the US you change the "+" to "-"? And if you wanted to make the code general purpose that would work everywhere?

Well you could go for a findstr covering both options:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
SET "P_=%~1" & SET "D_="
FOR /F "USEBACKQ" %%# IN (`WMIC DATAFILE WHERE "NAME='%P_:\=\\%'" GET^
CREATIONDATE^|FINDSTR [+-]`) DO SET D_=%%~#
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~nx1
PAUSE

Or find something unique to the output line only, I'd be inclined to think that a 0 will always be there:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
IF %1' EQU ' GOTO :EOF
SET "P_=%~1" & SET "D_="
FOR /F "USEBACKQ" %%# IN (`WMIC DATAFILE WHERE "NAME='%P_:\=\\%'" GET^
CREATIONDATE^|FIND "0"`) DO SET D_=%%~#
ECHO=%D_:~6,2%/%D_:~4,2%/%D_:~,4% %~nx1
PAUSE

Share this post


Link to post
Share on other sites

I thought you were in Italy, which is a plus offset

As a very respectable member said not so long ago :whistle: :

Your second attempt made assumptions and is therefore discounted as a proper solution.

:lol:

Seriously:

What is the issue with "SKIP=1"?

What if the "^,Name" is kept (though redundant) and one uses "FIND "\"?

jaclaz

Share this post


Link to post
Share on other sites

I'm not assuming that you're in Italy, your flag is Italian and IP address is Italian, (telecomitalia.it). Since I was replying directly to you, there was no reason not to structure my response directly to you.

The output of WMIC cannot be guaranteed to produce a specific number of lines, therefore the prudent thing to do is to skip all but your intended line, not skip the first line and keep all others. You can output information you don't need in order to have something definite to FIND if you wish, it's your script, but since you know when you formulate your GET's that none of the headers will contain a +, - or numeric value, it would appear a pointless addition.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...