tinyevil Posted May 30, 2012 Posted May 30, 2012 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.txtThis 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!
jaclaz Posted May 30, 2012 Posted May 30, 2012 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.txtjaclaz
tinyevil Posted May 30, 2012 Author Posted May 30, 2012 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.
Yzöwl Posted May 30, 2012 Posted May 30, 2012 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
tinyevil Posted May 30, 2012 Author Posted May 30, 2012 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?
Yzöwl Posted May 30, 2012 Posted May 30, 2012 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.
tinyevil Posted May 30, 2012 Author Posted May 30, 2012 Yes, removing /S resolved that issue.I will keep persuing an alternative method for the 'Created Date', thank you for your help
jaclaz Posted May 30, 2012 Posted May 30, 2012 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 OFFSETLOCAL ENABLEEXTENSIONSPUSHD C:\Users\myusername\mydirectoryFOR /F "tokens=* delims=" %%A IN ('DIR /B /A:-D') DO ECHO "%%~nxA" %%~tAPOPDThe "created" date is not "available" normally (the one expanded by ~t is the "last modified" one).BUT, try this:@ECHO OFFSETLOCAL ENABLEEXTENSIONSSETLOCAL ENABLEDELAYEDEXPANSIONFOR /F "tokens=1,* delims= " %%A IN ('DIR /T:C /A:-D C:\Users\myusername\mydirectory ^|FIND "/"') DO (SET Cdate=%%ASET Fname=%%BSET Fname=!Fname:~24!ECHO !FnamE!@!CDate!)(the above assumes that your date settings use "/" as separator)jaclaz
Yzöwl Posted May 30, 2012 Posted May 30, 2012 Well if you're using Windows 7 then it would be foolish to not mention PowershellHere'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
CoffeeFiend Posted May 30, 2012 Posted May 30, 2012 Well if you're using Windows 7 then it would be foolish to not mention PowershellHere'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.txtIf 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. Good job though.
tinyevil Posted May 31, 2012 Author Posted May 31, 2012 Well if you're using Windows 7 then it would be foolish to not mention PowershellHere'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.txtAm 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!
jaclaz Posted May 31, 2012 Posted May 31, 2012 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. 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? but, much more than that:WHERE THE HECK is gunsmokingman? Why hasn't he yet posted a vbs snippet for this? jaclaz
Yzöwl Posted May 31, 2012 Posted May 31, 2012 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? 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.
jaclaz Posted May 31, 2012 Posted May 31, 2012 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=%%AIF NOT "%sDate%"=="/" ECHO Hallo you have a WRONG setting for date separator&PAUSE&GOTO :EOFSETLOCAL 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
Yzöwl Posted May 31, 2012 Posted May 31, 2012 (edited) 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 NULFOR /F "TOKENS=1,3*" %%a IN ( 'DIR/TC/A-D C:\Users\myusername\mydirectory^|FINDSTR/BVC:" "') DO ( >>ListOfFiles.txt ECHO=%%~a %%~c) Edited May 31, 2012 by Yzöwl Solution for jaclaz altered to suit OP
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now