Jump to content

Is it possible to rename directory based on file name?


Recommended Posts

Hi,

I am new to AutoIt. I want to do the following. 

- I have (movie) folders which looks like this:

The Lion King\The Lion King (1994).avi
10 Cloverfield Lane\10 Cloverfield Lane (2016).mkv
..

- What I want to do is take the file name Ex: 10 Cloverfield Lane (2016).mkv and rename the folder as 10 Cloverfield Lane (2016)

 

Is this possible? Reason I am asking is I have lot of folders and it is really time consuming.

Thank you.

Link to comment
Share on other sites

@ripcord,

Welcome to AutoIt forum.

Yes by doing the below.

Actually there are lots of function that can do what you need.^_^

By getting the file name, you can easily create folder by doing below:;)

DirCreate($sFilePath & "\" & $sFileName)

This one will get the filename as well (see help file):

#include <File.au3>

Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""

$Path = @ScriptDir & "\10 Cloverfield Lane (2016).mkv"

_PathSplit($Path, $sDrive, $sDir, $sFileName, $sExtension)
MsgBox(0,"",$sFileName)

There are lots of ways to do what you want.

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Here is a function based upon KickStarter15 code to also rename the folder using the filename minus the extension:

#include <Array.au3>
#include <File.au3>

Global $aFilePath = _FileListToArrayRec(@ScriptDir, "*.*", 1, 0, 0, 2)
    If @error Then Exit MsgBox(16, "Error", "No files found")

For $i = 1 To $aFilePath[0]
    _FolderRename($aFilePath[$i])
Next

Func _FolderRename($_sFileName)
    Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
    Local $aPathSplit = _PathSplit($_sFileName, $sDrive, $sDir, $sFileName, $sExtension)
    ;~ Remove Beginning and End BackSlash "\" from $sDir
    Local $sSourceDir = $sDrive & StringTrimRight($sDir, 1)
    Local $sTargetDir = StringTrimRight($sSourceDir, StringInStr($sSourceDir, "\", 0, -1) - 1) & $sFileName
    MsgBox(32, "Path Rename", "Rename: " & @CRLF & $sSourceDir & @CRLF & "To: " & @CRLF & $sTargetDir)
    ;~ Uncomment function below rename the folder
;~  DirMove($sSourceDir, $sTargetDir, 1)
EndFunc

 

Link to comment
Share on other sites

12 hours ago, Subz said:

Here is a function based upon KickStarter15 code to also rename the folder using the filename minus the extension:

#include <Array.au3>
#include <File.au3>

Global $aFilePath = _FileListToArrayRec(@ScriptDir, "*.*", 1, 0, 0, 2)
    If @error Then Exit MsgBox(16, "Error", "No files found")

For $i = 1 To $aFilePath[0]
    _FolderRename($aFilePath[$i])
Next

Func _FolderRename($_sFileName)
    Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
    Local $aPathSplit = _PathSplit($_sFileName, $sDrive, $sDir, $sFileName, $sExtension)
    ;~ Remove Beginning and End BackSlash "\" from $sDir
    Local $sSourceDir = $sDrive & StringTrimRight($sDir, 1)
    Local $sTargetDir = StringTrimRight($sSourceDir, StringInStr($sSourceDir, "\", 0, -1) - 1) & $sFileName
    MsgBox(32, "Path Rename", "Rename: " & @CRLF & $sSourceDir & @CRLF & "To: " & @CRLF & $sTargetDir)
    ;~ Uncomment function below rename the folder
;~  DirMove($sSourceDir, $sTargetDir, 1)
EndFunc

 

instead of @ScriptDir how can Provide the root of my folder which contains the subfolders ?

Link to comment
Share on other sites

10 minutes ago, Subz said:

Just change @ScriptDir to whatever you like, for example: "C:\Data"

Thanks. Worked. Guesss I need to work on this script a bit as this is moving files to different location instead of reanme

Link to comment
Share on other sites

8 hours ago, Subz said:

Actually AutoIt doesn't have a rename function so you use DirMove to rename the folder, if you look at the help file it mentions this in the remarks section.

Thank you. This really helps. Cheers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...