Add Directory Name To All Files In All Subdirectories

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Roonyroo
Posts: 36
Joined: 29 Jan 2014, 20:29

Add Directory Name To All Files In All Subdirectories

25 Jun 2014, 05:57

I'm trying to add the 2nd root directory name to all files, in all preceding subfolders

ie grab a list of the directory names in a root directory ie all the top level directories in the root directory

ie under directory apples theres 3 directories labelled a1 b2 c3

I then want all the files in all subdirectories under a1 to be labelled as a1 apples.wav & all the files in all subdirectories under b2 to be labelled as b2 asdf.wav etc
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Add Directory Name To All Files In All Subdirectories

25 Jun 2014, 10:22

Code: Select all

FileSelectFolder, MyFolder
Loop, % MyFolder "\*", 2, 0	; 2 = Only folders are retrieved (no files).
{
	;MsgBox, % A_LoopFileName "`n" A_LoopFileLongPath
	CurrentFolder := A_LoopFileName
	Loop, % A_LoopFileLongPath "\*", 1, 0	; 1 = All files and folders that match the wildcard pattern are retrieved.
	{
		SplitPath, A_LoopFileLongPath, OutFileName, OutDir
		FileMove, % A_LoopFileLongPath, % OutDir "\" CurrentFolder " " OutFileName
		;MsgBox, % OutDir "\" CurrentFolder " " OutFileName
	}
}
return
Roonyroo
Posts: 36
Joined: 29 Jan 2014, 20:29

Re: Add Directory Name To All Files In All Subdirectories

26 Jun 2014, 10:46

Thats a great example thnx

I modified the script to read only the top directories, I just need to know how to prefix the directory name to all the files in a directory

thnx

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
MyFolder = D:\s
Loop, % MyFolder "\*", 2, 0 ; 2 = Only folders are retrieved (no files).
{
    ;MsgBox, %A_LoopFileLongPath%
    CurrentFolder := A_LoopFileName
MsgBox, %CurrentFolder% 
    
 
}
return
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Add Directory Name To All Files In All Subdirectories

26 Jun 2014, 15:38

Roonyroo wrote:I just need to know how to prefix the directory name to all the files in a directory

Code: Select all

; Renames all the files in the selected folder to include the folder name
; Does not recurse (go deeper) into child folders

FileSelectFolder, MyFolder			; Select a folder
SplitPath, MyFolder, FolderName		; Get the folder name
Loop, % MyFolder "\*", 0			; 0 = (default) Folders are not retrieved (only files).
{
	FileMove, % A_LoopFileLongPath, % MyFolder "\" FolderName " " A_LoopFileName
	MsgBox, % MyFolder "\" FolderName " " A_LoopFileName
}
return
Roonyroo
Posts: 36
Joined: 29 Jan 2014, 20:29

Re: Add Directory Name To All Files In All Subdirectories

26 Jun 2014, 16:46

Brilliant ...

I tried the following but I keep getting s10871115145424835001gte065_UG_e-guitar 1_d#_1.wav

instead of D:\s\1087\1115\1454\248\350\1087gte065_UG_e-guitar 1_d#_1.wav

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
MyFolder = D:\s
Loop, % MyFolder "\*", 2, 0 ; 2 = Only folders are retrieved (no files). 0 dont recurse into subfolders
{
    ;MsgBox, %A_LoopFileLongPath%
x1:=A_LoopFileLongPath
   CurrentFolder :=A_LoopFileName
;MsgBox, %CurrentFolder% 
 
   Loop, % A_LoopFileLongPath "\*", 0, 1 ; 1= Only files are retrieved (no files). 1 recurse into 

subfolders
{
x:=A_LoopFileLongPath
 xs:=A_LoopFileName
xsl = %A_LoopFileDir%\%currentfolder%%xs%
clipboard = %xsl%
FileMove, %x%, %xsl%
msgbox %xsl% %A_LoopFileDir% x = %x%

}

}
return

esc::exitapp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: madensuyu1, peter_ahk and 355 guests