MSFN Forum: Batch file to export file name and created date - MSFN Forum

Jump to content


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Batch file to export file name and created date Rate Topic: -----

#1 User is offline   tinyevil 

  • Group: Members
  • Posts: 5
  • Joined: 30-May 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 30 May 2012 - 06:04 AM

Hello all.

I have the following batch command that returns all files within the specified directory and its sub folders.

for /r C:\Users\myusername\mydirectory %%g in (*) do echo %%~nxg>>ListofFiles.txt


This works as you'd expect - I'm happy with it. However, i would now like to include the 'Created Date' and/or 'Modified Date' in the export.

I've searched this forum and google, but i (genuinely) can't find the resolution.

If possible, i would also like to amend the script so that the command returns ONLY files within the specified directory, and does NOT return the files within its sub folders.

Any help would be greatly appreciated!


#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 30 May 2012 - 06:19 AM

View Posttinyevil, on 30 May 2012 - 06:04 AM, said:

If possible, i would also like to amend the script so that the command returns ONLY files within the specified directory, and does NOT return the files within its sub folders.

Any help would be greatly appreciated!


What happens with:
FOR /F %%A IN ('DIR /B /A:-D C:\Users\myusername\mydirectory') DO ECHO %%~nxA  %%~tA >>ListofFiles.txt


jaclaz

#3 User is offline   tinyevil 

  • Group: Members
  • Posts: 5
  • Joined: 30-May 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 30 May 2012 - 06:27 AM

First observation, it seems to have trimmed the file names. For a file named "Firefox Setup.exe", your script has returned only "Firefox". However for a file named "AppDocUse.zip", your script has returned it all. I'm thinking this has something to do with the space in the file name?

Second observation, it has not returned the created date for any of the files.

Thanks Jaclaz.

#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 May 2012 - 09:29 AM

Depending upon your Operating System you may be happy with the output of FORFILES:
FORFILES /P C:\USERS\MYUSERNAME\MYDIRECTORY /S /C "CMD /C ECHO=@FILE @FDATE">LISTOFFILES.TXT


#5 User is offline   tinyevil 

  • Group: Members
  • Posts: 5
  • Joined: 30-May 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 30 May 2012 - 09:56 AM

Hello,

I'm using Win 7 x86, and that script gave me pretty much exactly what i wanted. One tiny thing, not sure if it's feasible or not, but the date being returned is actually the 'Date Modified' date rather than the 'Date Created' date. Is it possible to return this value?

Also, the script returns file information for anything within the specified directory and all its subfolders - is it possible for it to return file information for only the specified directory?

#6 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 May 2012 - 10:24 AM

Unfortunately @FDATE returns the last Modified Date, for the Date Created you'd need an alternative method. The provided method will ignore the subdirectories if you remove the /S switch.

#7 User is offline   tinyevil 

  • Group: Members
  • Posts: 5
  • Joined: 30-May 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 30 May 2012 - 10:29 AM

Yes, removing /S resolved that issue.

I will keep persuing an alternative method for the 'Created Date', thank you for your help :)

#8 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 30 May 2012 - 11:13 AM

View Posttinyevil, on 30 May 2012 - 06:27 AM, said:

First observation, it seems to have trimmed the file names. For a file named "Firefox Setup.exe", your script has returned only "Firefox". However for a file named "AppDocUse.zip", your script has returned it all. I'm thinking this has something to do with the space in the file name?

Second observation, it has not returned the created date for any of the files.

Thanks Jaclaz.


Yep. :(

Try this instead:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
PUSHD C:\Users\myusername\mydirectory
FOR /F "tokens=* delims=" %%A IN ('DIR /B /A:-D') DO ECHO "%%~nxA" %%~tA
POPD



The "created" date is not "available" normally (the one expanded by ~t is the "last modified" one).

BUT, try this:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,* delims= " %%A IN ('DIR /T:C /A:-D C:\Users\myusername\mydirectory ^|FIND "/"') DO (
SET Cdate=%%A
SET Fname=%%B
SET Fname=!Fname:~24!
ECHO !FnamE!@!CDate!
)

(the above assumes that your date settings use "/" as separator)

jaclaz

#9 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 May 2012 - 11:15 AM

Well if you're using Windows 7 then it would be foolish to not mention Powershell

Here's an example line which should give you a listing also ordered according to that Creation Date and Time.
GCi C:\Users\myusername\mydirectory | Select-Object Name, CreationTime | Sort CreationTime | Out-File ListOfFiles.txt


#10 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 30 May 2012 - 03:10 PM

View PostYzöwl, on 30 May 2012 - 11:15 AM, said:

Well if you're using Windows 7 then it would be foolish to not mention Powershell

Here's an example line which should give you a listing also ordered according to that Creation Date and Time.
GCi C:\Users\myusername\mydirectory | Select-Object Name, CreationTime | Sort CreationTime | Out-File ListOfFiles.txt


If you start handing out powershell scripts, then what am I going to do around here now? I'm afraid I've just been made redundant. :unsure: Good job though.

#11 User is offline   tinyevil 

  • Group: Members
  • Posts: 5
  • Joined: 30-May 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 31 May 2012 - 01:47 AM

View PostYzöwl, on 30 May 2012 - 11:15 AM, said:

Well if you're using Windows 7 then it would be foolish to not mention Powershell

Here's an example line which should give you a listing also ordered according to that Creation Date and Time.
GCi C:\Users\myusername\mydirectory | Select-Object Name, CreationTime | Sort CreationTime | Out-File ListOfFiles.txt



Am i right understanding that i simply paste this into a .bat file? If so, it didn't produce an export file. I've double checked the directory, and it is right.

Thank you for all your help so far!

#12 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 31 May 2012 - 01:55 AM

View PostCoffeeFiend, on 30 May 2012 - 03:10 PM, said:

If you start handing out powershell scripts, then what am I going to do around here now? I'm afraid I've just been made redundant. :unsure: Good job though.

Sure, this is a possibly preoccupying issue, but the real issues are:
WHY hasn't Yzöwl posted a better batch snippet than mine? :w00t:
but, much more than that:
WHERE THE HECK is gunsmokingman? :ph34r: Why hasn't he yet posted a vbs snippet for this? :unsure:

;)

jaclaz

#13 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 31 May 2012 - 09:48 AM

View Posttinyevil, on 31 May 2012 - 01:47 AM, said:

Am i right understanding that i simply paste this into a .bat file? If so, it didn't produce an export file. I've double checked the directory, and it is right.

Thank you for all your help so far!

No you either run them directly in a Powershell Window (as opposed to a CMD Window). You can find Windows Powershell quickly by entering POW in the search files dialog on your Start Menu.
An alternative is to write it as you would a BAT / CMD file but give it the extension PS1,

View Postjaclaz, on 31 May 2012 - 01:55 AM, said:

WHY hasn't Yzöwl posted a better batch snippet than mine? :w00t:

I did, it murdered your first attempt using FORFILES and answered the question asked at the time of posting! Your second attempt made assumptions and is therefore discounted as a proper solution.

#14 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 31 May 2012 - 10:21 AM

View PostYzöwl, on 31 May 2012 - 09:48 AM, said:

I did, it murdered your first attempt using FORFILES and answered the question asked at the time of posting! Your second attempt made assumptions and is therefore discounted as a proper solution.

Well, you could have bettered it nonetheless, like:

Quote

@ECHO OFF
FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sDate 2^>NUL') DO SET sDate=%%A
IF NOT "%sDate%"=="/" ECHO Hallo you have a WRONG setting for date separator&PAUSE&GOTO :EOF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,* delims= " %%A IN ('DIR /T:C /A:-D C:\Users\myusername\mydirectory ^|FIND "/"') DO (
SET Cdate=%%A
SET Fname=%%B
SET Fname=!Fname:~24!
ECHO !FnamE!@!CDate!
)


;)

jaclaz

#15 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 31 May 2012 - 11:11 AM

Of course I could, but I'd have probably tried to look for a different method of identifying the lines required/not required.

Perhaps:
@ECHO OFF
>ListOfFiles.txt TYPE NUL
FOR /F "TOKENS=1,3*" %%a IN (
   'DIR/TC/A-D C:\Users\myusername\mydirectory^|FINDSTR/BVC:" "') DO (
	>>ListOfFiles.txt ECHO=%%~a  %%~c)

This post has been edited by Yzöwl: 31 May 2012 - 12:03 PM
Reason for edit: Solution for jaclaz altered to suit OP


#16 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 31 May 2012 - 11:56 AM

View PostYzöwl, on 31 May 2012 - 11:11 AM, said:

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

#17 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 31 May 2012 - 02:01 PM

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.

Quote

Source Folder Path
1\ Name
2:\ Created
3\ Accessed
4\ Modified


Save As List_File_Directory.vbs
'-> Object For Run Time
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles For Script Run Time
Dim Ar, Dr, Ln, Ts, Txt
 Ar = Chr(160) & Chr(187) & Chr(160)
 Ln = "--------------------------------------------------------"
'-> Makes Sure Only One Object Drag And Drop
  Select Case WScript.Arguments.Count
   Case 0
    call 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 Folder
    If Right(InStr(WScript.Arguments.Item(0),"."),6) Then
    call 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")
    Else
     Dr = 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 & Dr
     Recursive(Fso.GetFolder(Dr))
     Ts.WriteLine Ln
     Ts.Close
     Act.Run(Txt),1,True 
     call 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 If
   Case Else 
    call 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 TimeOut
   Function Msg(Tx, Tn, Btn)
    Select Case Btn
     Case 4128
      Act.Popup Tx, 5, Tn, 4128
     Case 4132
      If Act.Popup(Tx, 5, Tn, 4132) = 7 Then Fso.DeleteFile(Txt), True 
    End select
   End Function
'-> Recusive Threw Folder And All Sub Folders
   Function Recursive(Folder)
     Ts.WriteLine Ln 
     Ts.WriteLine " Folder Path  " & Ar & Folder
    For Each Obj In Folder.Files
     Ts.WriteLine Ln 
     Ts.WriteLine " File Name    " & Ar & Obj.Name
     Ts.WriteLine " Date Created " & Ar & Obj.DateCreated
     Ts.WriteLine " Last Accessed" & Ar & Obj.DateCreated
     Ts.WriteLine " Last Modified" & Ar & Obj.DateCreated
    Next
    For Each Dir In Folder.subFolders
     Recursive(Dir)
    Next
   End Function



Rename List_File_Directory.vbs.txt to List_File_Directory.vbs to make active

Attached File(s)



#18 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 01 June 2012 - 04:57 AM

View Postgunsmokingman, on 31 May 2012 - 02:01 PM, said:

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

#19 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 01 June 2012 - 11:59 AM

View Postjaclaz, on 01 June 2012 - 04:57 AM, said:

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


#20 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,409
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 01 June 2012 - 12:43 PM

View PostYzöwl, on 01 June 2012 - 11:59 AM, said:

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 topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy