Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

List of files and folders



  • Please log in to reply
13 replies to this topic
redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013

Hello, I would like to create a list of files and folders from the working directory.

For each folder, I would like to add a tab for files and folder contained in this folder (compared to the folder parent).

 

I try this :

Loop, %A_WorkingDir%\*.*, 1, 0
	{
		FileAppend, %A_LoopFileName%`n, Liste.txt
		Loop, %A_LoopFileName%\*.*, 1, 0
			{
				FileAppend, %A_TAB%%A_LoopFileName%`n, Liste.txt
			}
	}
Exit

It works only for one level, and if I active the recursive option the list created contains more than one time the same information.

 

Could someone help me please ?

 

Edit : or like a treeview



JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007
✓  Best Answer

You better do the recursion yourself:

Global strFile := A_ScriptDir . "\Liste.txt"
FileDelete, %strFile%

SetWorkingDir, C:\Program Files
Sub(A_WorkingDir . "\*.*", 0)

run, %strFile%
ToolTip,
Exit

Sub(strDir, intLevel)
{
	ToolTip, Level #%intLevel% %strDir%
	Loop, %intLevel%
		strTabs := strTabs . A_Tab
	Loop, %strDir%, 2 ; scan folders
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
		Sub(A_LoopFileFullPath . "\*.*", intLevel + 1) ; Recurse to subdirectory
	}
	Loop, %strDir%, 0 ; now scan files only
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
	}
}



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

tested and works great!

 

You better do the recursion yourself:

Global strFile := A_ScriptDir . "\Liste.txt"
FileDelete, %strFile%

SetWorkingDir, C:\Program Files
Sub(A_WorkingDir . "\*.*", 0)

run, %strFile%
ToolTip,
Exit

Sub(strDir, intLevel)
{
	ToolTip, Level #%intLevel% %strDir%
	Loop, %intLevel%
		strTabs := strTabs . A_Tab
	Loop, %strDir%, 2 ; scan folders
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
		Sub(A_LoopFileFullPath . "\*.*", intLevel + 1) ; Recurse to subdirectory
	}
	Loop, %strDir%, 0 ; now scan files only
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
	}
}



redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013

 

You better do the recursion yourself:

Global strFile := A_ScriptDir . "\Liste.txt"
FileDelete, %strFile%

SetWorkingDir, C:\Program Files
Sub(A_WorkingDir . "\*.*", 0)

run, %strFile%
ToolTip,
Exit

Sub(strDir, intLevel)
{
	ToolTip, Level #%intLevel% %strDir%
	Loop, %intLevel%
		strTabs := strTabs . A_Tab
	Loop, %strDir%, 2 ; scan folders
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
		Sub(A_LoopFileFullPath . "\*.*", intLevel + 1) ; Recurse to subdirectory
	}
	Loop, %strDir%, 0 ; now scan files only
	{
		FileAppend, %strTabs%\%A_LoopFileName%`n, %strFile%
	}
}

Thanks, I don't really understand all the script but it works ! (even if it's a little bit complicated to differenciate the levels when a lot of files & folders are contained)



JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007

tested and works great!

 

Thanks  :-)

 

Thanks, I don't really understand all the script but it works ! (even if it's a little bit complicated to differenciate the levels when a lot of files & folders are contained)

 

You're welcome. The key of this sript is this line:

 

Sub(A_LoopFileFullPath . "\*.*", intLevel + 1) ; Recurse to subdirectory

 

 

Sub is a "recursive" fuctions. It means that, before processing the files in a folder, it calls itself to run in every subfolders found in this folder. Since it can recurse numerous times, the intLevel keeps count of how deep is the folder being scanned.  intLevel is incremented (+1) when it enters in a subfolder and, after Sub entirely processed a folder, it exits and intLevel retreives its previous value (-1) when Sub returns to its previous instance.

 

intLevel allows some formatting by inserting intLevel number of A_Tab before the file or folder name. Of course, the formatting could be improved with some more coding. Have fun!

 

Hope this helps.



redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013
You're welcome. The key of this sript is this line:

 

Sub is a "recursive" fuctions. It means that, before processing the files in a folder, it calls itself to run in every subfolders found in this folder. Since it can recurse numerous times, the intLevel keeps count of how deep is the folder being scanned.  intLevel is incremented (+1) when it enters in a subfolder and, after Sub entirely processed a folder, it exits and intLevel retreives its previous value (-1) when Sub returns to its previous instance.

 

intLevel allows some formatting by inserting intLevel number of A_Tab before the file or folder name. Of course, the formatting could be improved with some more coding. Have fun!

 

Hope this helps.

 

But is it possible to customize the presentation like a "|-" before each file/folder ?



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

or maybe adding a folder selector GUI?



JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007

But is it possible to customize the presentation like a "|-" before each file/folder ?

 

Good idea. 

 

 

or maybe adding a folder selector GUI?

 

Yes. The Treeview GUI control can also be a good option. Look at the working example at the end of this page:

http://www.autohotke...ds/TreeView.htm



redking974
  • Members
  • 205 posts
  • Last active: Feb 11 2015 08:31 PM
  • Joined: 06 Apr 2013

Good idea.

 

But do you know how :D ?



JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007

But do you know how grin.png ?

 

Yes. But so many other things to code also ;-) I think you have a good start with what has been published above. Good luck! Do not hesitate if you have questions along the way.



AllYourBaseAreBelongToUs
  • Members
  • 4 posts
  • Last active:
  • Joined: 28 Oct 2015

Can anyone tell me what the period in this line does?

strTabs := strTabs . A_Tab


space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
A period is used to concatenate (join) two strings. See http://autohotkey.co...s.htm#Operators- the dot is not mandatory.

AllYourBaseAreBelongToUs
  • Members
  • 4 posts
  • Last active:
  • Joined: 28 Oct 2015

A period is used to concatenate (join) two strings. See http://autohotkey.co...s.htm#Operators- the dot is not mandatory.

Thanks, I was trying to use + operator like in Java...



tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
and then i took an arrow to the knee
Never lose.
WIN or LEARN.