Batch File
May 18, 2011 8:00 AM   Subscribe

Help me write a batch file to print directory information.

I found this

which physically prints the folder / file information but how do edit this code to save to notepad / future use in excel

@echo off
dir %1 /-p /o:gn > "%temp%\Listing"
start /w notepad /p "%temp%\Listing"
del "%temp%\Listing"

output colunms for all sub folders
[folder path][file name with extension][folder name]

The use of third party software is out of the question.
posted by MechEng to Computers & Internet (6 answers total) 1 user marked this as a favorite
Your question is unclear. What do you wish worked differently about this code snippet?
posted by odinsdream at 8:02 AM on May 18, 2011

The above code snippet physically prints the information. I do not want paper copy, I'm looking for a text file that I can import into excel
posted by MechEng at 8:05 AM on May 18, 2011

Two things.

1.) Take off the /p in the "start" line. That sends to printer.
2.) Remove the "del" line. that deletes the file.
posted by deezil at 8:08 AM on May 18, 2011

This doesn't even need to be a batch file. You can accomplish the whole thing in the first line, just replace the "%1" with whatever directory you want.

dir C:\MYDIR\ /-p /o:gn > "%temp%\Listing"
posted by jozxyqk at 8:10 AM on May 18, 2011

You'll probably want to fire up your CMD prompt and start with this:

dir /?

From there, try different combinations of formats to get the type of output you want, for example:

dir /w
dir /b

Then when you see the format you'd like, add a /s to the command so you get all subdirectories as well:

dir /b/s

If things are looking good, the next step is outputting it to a file:

dir /b/s > nameofthefile.txt

-or to go on you desktop-

dir /b/s > %userprofile%\desktop\nameofthefile.txt

This is the most basic way to include the dir command in a batch file. If you're looking for something more "formatted" however, I'd suggest powershell or vbscript. An example vbscript (listfiles.vbs) as below:

On Error Resume Next
Dim fso, folder, files, NewsFile,sFolder

Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = Wscript.Arguments.Item(0)
If sFolder = "" Then
Wscript.Echo "No Folder parameter was passed"
End If
Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files

For each folderIdx In files

Then to use it, you'd pass the folder name as an argument, for example:

listfiles.vbs "c:\myfolder"
posted by samsara at 9:07 AM on May 18, 2011 [1 favorite]

@echo off
dir %1 /s /-p /o:gn > "%temp%\Listing"
start /w notepad "%temp%\Listing"

excel code was used for formating of columns
posted by MechEng at 1:43 PM on May 18, 2011

« Older Because the default app just ain't cutting it.   |   Networking event success: help! Newer »
This thread is closed to new comments.