• 0

[Batch] List files in current/sub-directories


Question

Not sure if this could be considered programming, but here goes.

I'm trying to make a batch file that I can move into a folder, run it, and have it output all files in the current directory and all subdirectories.

Now here's the harder part. Right now I have just "dir/s/b > list.txt", but that lists files with the full path.

I want it to output the path starting with the current directory.

Example: I put it in a folder on my desktop. Right now it shows this when run:

C:\Documents and Settings\Michael\Desktop\ITSINHERE\foo.txt

C:\Documents and Settings\Michael\Desktop\ITSINHERE\Folder\bar.txt

I want it to ouput like this:

ITSINHERE\foo.txt

ITSINHERE\Folder\bar.txt

1 answer to this question

Recommended Posts

  • 0

Try this VBS script, all you have to do is drag a folder on to it. It will then list all

the contents of the folder and all it sub folders. It will then create a text file on

your desktop with the full paths to each file.

Save As ListDir.vbs

  Quote
 Option Explicit
 On Error Resume Next
 Dim Act :Set Act = CreateObject("Wscript.Shell")
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim A, File1, File2, Obj, StartFolder, Subfolder, Ts, Text
 Dim Ln1, Ln2
 Ln1 = Chr(171) & " ------------------------------------------------------------------ " & Chr(187)
 Ln2 = Chr(160) & Chr(187) & Chr(160)
  If Wscript.Arguments.Count = 0 Then
   Msgbox _
   "Please Drag And Drop A Folder On This Script" & vbCrLf & _
   "To Get A List Of The Directory Contents.",4128,"Error No Folder"
  Else
   For Each Obj In WScript.Arguments
	If Right(InStr(Obj,"."),4) Then 
	'Do Nothing It A File
	Else
	 Set StartFolder = Fso.GetFolder(Obj)
	 Text = Act.SpecialFolders("Desktop") & "\List_" & StartFolder.Name & ".txt"  
	 Set Ts = Fso.CreateTextFile(Text)
	  Ts.WriteLine Ln1 & vbCrLf & Obj & vbCrLf & Ln1
	  For Each File1 In StartFolder.Files
	   Ts.WriteLine Ln2 & File1.Path 
	  Next
	 ShowSubfolders Fso.GetFolder(Obj)
	End If 
   Next
   Ts.Close
	Act.Run(Chr(34) & Text & Chr(34)),1,True
	A = MsgBox("Did You Want To Keep This File?" & vbCrLf & Text, 4132, "Keep Or Delete")
   If A = 7 Then Fso.DeleteFile(Text),True 
  End If 

   Sub ShowSubFolders(Folder)
	For Each Subfolder in Folder.SubFolders
	 Ts.WriteLine Ln1 & vbCrLf & Subfolder.Path & vbCrLf & Ln1
	  For Each File2 In Subfolder.Files
	   Ts.WriteLine Ln2 & File2.Path
	  Next
	 ShowSubFolders Subfolder
	Next
   End Sub
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.