TRUNCATE TABLE master..sysdatabases

...and other neat SQL Server tricks
posts - 51, comments - 60, trackbacks - 14

Quick Tip #1: Using the for command to get directory info

Sometimes I need to import file information into SQL Server, or just get a list of files, sizes, etc. Normally I use the dir command, but it generates a fixed layout that is sometimes difficult to parse. Here's an easier way to get file name, size, and date in a comma-delimited format:

for %a in (*.*) do @echo %a,%~za,%~ta
...or...
for %a in (*.*) do @echo %a,%~za,%~tam

Depending on which version of Windows you're running, you may prefer the 2nd version, it will add an "m" to the time display so that times show as "AM" or "PM". Windows 2003 would use the first version, as it already displays AM and PM.

You'll notice in the command the %~z and %~t prefixes, these are flags to display the size and time of the file name stored in %a. For more flags you can use, visit your local Windows Help file on the Start menu and look up the for command.

Print | posted on Wednesday, February 02, 2005 10:35 PM |

Feedback

Gravatar

# re: Quick Tip #1: Using the for command to get directory info

Hi:

Thanks for your tip,
May I know how to get all sub-directories listing as well?.

Thanks in advance

2/1/2006 4:01 PM | It is helpful, but
Gravatar

# re: Quick Tip #1: Using the for command to get directory info

If you want just the directory names:

for /D %a in (*.*) do @echo %a,%~za,%~tam

If you want the files in each directory too:

for /R %a in (*.*) do @echo %a,%~za,%~tam

As I said, there's a lot that for can do if you look in the Windows help file.
2/1/2006 5:32 PM | robvolk
Gravatar

# re: Quick Tip #1: Using the for command to get directory info

THIS IS FINE BUT I HAVE THE SAME REQUIREMENT IN LINUX, HELP ME.
7/27/2006 2:20 AM | linux pls help
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET