Jump to content

Batch file to export file name and created date


Recommended Posts

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!

Link to comment
Share on other sites


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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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,

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.

Link to comment
Share on other sites

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:

@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

Link to comment
Share on other sites

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)

Edited by Yzöwl
Solution for jaclaz altered to suit OP
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...