Jump to content
youtuber

Change folder names and file names

Recommended Posts

youtuber

How can I do this for folder and file names I want to remove autoit word?

My folders

Spoiler

ZwYD_xenQviMttD2b4Kv7Q.png

My Folder and Files list

All files folder\Test1 - Autoit\Test1 - Autoit.txt

All files folder\Test2 - Autoit\Test2 - Autoit.txt

My codes

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


Global $sFilters = "*.txt"

_AlcopyFilefolder()

Func _AlcopyFilefolder($sDest = @UserProfileDir)

If @error Then SetError(1, 0, 0)

Local $aFiles = _FileListToArrayRec(@ScriptDir, $sFilters, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If @error Then Return
For $i = 1 To $aFiles[0]
        $sNew_Name = StringRegExpReplace($aFiles[$i], "- Autoit", "")
FileCopy($aFiles[$i], $sDest & "\Copy Folder" & "\" & $sNew_Name, $FC_CREATEPATH)

Next
EndFunc

 

Share this post


Link to post
Share on other sites
AdamUL

Look at using FileMove.  From the help file.  

Quote

Because AutoIt lacks a "FileRename" function, use FileMove() to rename a file!

 

Adam

  • Like 1

Share this post


Link to post
Share on other sites
mexykanu

I don't quite understand what you are trying to do. If you want to rename your files, you could use FileMove, for both Files and Directories.

  • Like 1

Share this post


Link to post
Share on other sites
youtuber

Ok

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


Global $sFilters = "*.txt"

_AlcopyFilefolder()

Func _AlcopyFilefolder($sDest = @UserProfileDir)

If @error Then SetError(1, 0, 0)

Local $aFiles = _FileListToArrayRec(@ScriptDir, $sFilters, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If @error Then Return
For $i = 1 To $aFiles[0]
        $sNew_Name = StringRegExpReplace($aFiles[$i], "- Autoit", "")
;~ FileCopy($aFiles[$i], $sDest & "\Copy Folder" & "\" & $sNew_Name, $FC_CREATEPATH)
FileMove($aFiles[$i], $sDest & "\Copy Folder" & "\" & $sNew_Name, $FC_CREATEPATH)
Next
EndFunc

 

Share this post


Link to post
Share on other sites
VIP
6 hours ago, mexykanu said:

I don't quite understand what you are trying to do. If you want to rename your files, you could use FileMove, for both Files and Directories.

! FileMove only for FILES not for DIRECTORIES

DirMove("source dir", "dest dir" [, flag = 0] ) ;Moves a directory and all sub-directories and files.
FileMove("source", "dest" [, flag = 0] ) ; Moves one or more files.

 Solution:

Func _FileFolderMove($iSource, $iDest, $iOverwrite = 1)
    If StringRight($iSource, 1) == '\' Then $iSource = StringTrimRight($iSource, 1) ; Remove Backslash from path
    If StringRight($iDest, 1) == '\' Then $iDest = StringTrimRight($iDest, 1) ; Remove Backslash from path 
    If Not FileExists($iSource) Then Return SetError(-1, 0, 0)
    Local $iReturn = 0
    If (StringInStr(FileGetAttrib($iSource), "D") = 0) Then ; check path is file or folder
        $iReturn = FileMove($iSource, $iDest, 8 + $iOverwrite) ; if is file
    Else
        $iReturn = DirMove($iSource, $iDest, $iOverwrite) ; if is folder
    EndIf
    Return SetError(@error, @extended, $iReturn)
EndFunc   ;==>_FileFolderMove:  DAO VAN TRONG - TRONG.WIN

 


Regards,
 

Share this post


Link to post
Share on other sites
mexykanu
1 hour ago, VIP said:

! FileMove only for FILES not for DIRECTORIES

You are correct, please excuse me. :D It was late.

Share this post


Link to post
Share on other sites
youtuber
17 hours ago, mexykanu said:

I don't quite understand what you are trying to do. If you want to rename your files, you could use FileMove, for both Files and Directories.

I want to remove the Autoit word for both Files and Directories

StringRegExpReplace($iSource[$i], "- Autoit", "")

#include <File.au3>

$DestinationFolder = @UserProfileDir & "\All Files Folder\"

 _FileFolderMove(@ScriptDir, $DestinationFolder)

Func _FileFolderMove($iSource, $iDest, $iOverwrite = 1)
    If StringRight($iSource, 1) == '\' Then $iSource = StringTrimRight($iSource, 1) ; Remove Backslash from path
    If StringRight($iDest, 1) == '\' Then $iDest = StringTrimRight($iDest, 1) ; Remove Backslash from path
    If Not FileExists($iSource) Then Return SetError(-1, 0, 0)
    Local $iReturn = 0
    If (StringInStr(FileGetAttrib($iSource), "D") = 0) Then ; check path is file or folder
        For $i = 1 To $iSource[0]
        $iReturn = FileMove(StringRegExpReplace($iSource[$i], "- Autoit", ""), StringRegExpReplace($iDest[$i], "- Autoit", ""), 8 + $iOverwrite) ; if is file
        Next
        Else
        For $i = 1 To $iSource[0]
        $iReturn = DirMove(StringRegExpReplace($iSource[$i], "- Autoit", ""), StringRegExpReplace($iSource[$i], "- Autoit", ""), $iOverwrite) ; if is folder
        Next
        EndIf
        Return SetError(@error, @extended, $iReturn)
EndFunc   ;==>_FileFolderMove:  DAO VAN TRONG - TRONG.WIN

 

Edited by youtuber

Share this post


Link to post
Share on other sites
Earthshine

you can use the command line REN (Rename) command to rename files and folders. first do your copy. then rename the target.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
youtuber
29 minutes ago, Earthshine said:

dosya ve klasörleri yeniden adlandırmak için komut satırı REN (Yeniden adlandır) komutunu kullanabilirsiniz. önce kopyanızı yapın. sonra hedefi yeniden adlandırın.

I want to know how to do this with autoit

Share this post


Link to post
Share on other sites
Earthshine

here is an example. 

$CMD = "rename c:\path1 c:\path2"
RunWait(@ComSpec & " /c " & $CMD)
Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
youtuber
26 minutes ago, Earthshine said:

here is an example. 

$CMD = "rename c:\path1 c:\path2"
RunWait(@ComSpec & " /c " & $CMD)

Okay, how am I going to add that time?

StringRegExpReplace($CMD, "- Autoit", "");File folder names Remove Autoit word?

 

Share this post


Link to post
Share on other sites
Earthshine

looks like you are hung up on regular expressions. you could build the array of paths you want to rename and execute that command on array elements where you have computed the output name already. there are plenty of string functions to do this.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
youtuber

Something like this should be sub folders and files

#include<file.au3>

Dim $sourceFolder = @ScriptDir

$aFiles = _FileListToArray($sourceFolder, "*.txt", 1)
For $i = 1 To $aFiles[0]
    If @error Then ExitLoop
    $resultFile = StringRegExpReplace($aFiles[$i], "- Autoit", "")
    FileMove($sourceFolder & "\" & $aFiles[$i], $sourceFolder & "\" & $resultFile)
Next

$aFolder = FileFindFirstFile($sourceFolder & "*")
 While 1
    $folderFind = FileFindNextFile($aFolder)
    $resultFolder = StringRegExpReplace($folderFind, "- Autoit", "")
    If @error Then ExitLoop
    DirMove($sourceFolder & "\" & $resultFolder, $sourceFolder & "\" & $resultFolder)
 WEnd
 FileClose($aFolder)

 

Or

#include<file.au3>

Dim $sourceFolder = @ScriptDir

$aFiles = _FileListToArrayRec(@ScriptDir, "*")
For $i = 1 To $aFiles[0]
    If @error Then ExitLoop
    $resultFile = StringRegExpReplace($aFiles[$i], "- Autoit", "")
If (StringInStr(FileGetAttrib($aFiles), "D") = 0) Then
    FileMove($sourceFolder & "\" & $aFiles[$i], $sourceFolder & "\" & $resultFile)
Else
DirMove($sourceFolder & "\" & $aFiles[$i], $sourceFolder & "\" & $resultFile)
EndIf
Next

 

Edited by youtuber

Share this post


Link to post
Share on other sites
youtuber

I want to delete the autoit word from all txt files including subfolders.
I do not understand me :(

#include<file.au3>

Dim $sourceFolder = @ScriptDir
$sFilters = "*.txt"

_moveall()

Func _moveall($sDest = @ScriptDir)
    If @error Then SetError(1, 0, 0)
    Local $aFiles = _FileListToArrayRec(@ScriptDir, $sFilters, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
    If @error Then Return
    For $i = 1 To $aFiles[0]
        $resultFile = StringRegExpReplace($aFiles[$i], "- Autoit", "")
        FileMove($aFiles[$i] & "\" & $aFiles[$i], $sourceFolder & "\" & $resultFile)
    Next
EndFunc

 

Share this post


Link to post
Share on other sites
BrewManNH

Try using StringReplace instead o StringRegexpReplace.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
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

×