Tats Posted May 28, 2018 Posted May 28, 2018 Hi, Good Day! I have a problem and having difficulty implementing it. I want to copy the files in a certain folder, rename it, and when it exist in the destination folder it will add an index counter. Example source folder having files like: AA_123.TXT BB_123.TXT CC_123.TXT DD_123.TXT After file copy the target folder would be like this: 123.TXT 123_1.TXT 123_2.TXT 123_3.TXT How to achieve this? Thanks in advance
FrancescoDiMuro Posted May 28, 2018 Posted May 28, 2018 Hi @Tats, and welcome to the AutoIt forum Did you take a look to _FileListToArray() function, and FileMove() function? And... Your source file(s) is/are "AA_123.txt", "BB_123.txt", and you want to remove the "AA_", "BB_" string and add "1", "2", ... to the file name? Post some script, so we can see what's wrong with it Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Tats Posted May 29, 2018 Author Posted May 29, 2018 (edited) Hi @FrancescoDiMuro, Here are the files from my Source Folder for example, where TxID---- is auto incremented. TxID0332_TQ27.txt TxID0333_TQ1.txt TxID0334_TQ27.txt TxID0335_TQ1.txt TxID0336_TQ27.txt TxID0337_TQ1.txt TxID0338_TQ302.txt Func CopyLog() Local $sDestPath = "D:\CopiedLogs" Local $Count = 0 Local $sSrcLogFolder = _FileListToArray("D:\Logs", "*", $FLTA_FILES) If @error = 4 Then ;do nothing Else For $i = 1 to $sSrcLogFolder[0] Local $iLen = StringLen($sSrcLogFolder[$i]) Local $iStartPos = StringInStr($sSrcLogFolder[$i],"TQ") Local $iLenToGet = $iLen - ($iStartPos - 1) Local $sLogFileName = StringRight($sSrcLogFolder[$i],$iLenToGet) Local $sTrimFilename = StringTrimRight($sLogFileName, 4) Local $FileCount = GetFileCount($sDestPath, $sLogFileName) If $FileCount > 0 Then $Count += 1 Local $FileLocation = $sDestPath & "\" & $sTrimFilename &"_" & $Count & ".TXT" FileCopy("D:\Logs\" & $sSrcLogFolder[$i], $FileLocation, $FC_NOOVERWRITE + $FC_CREATEPATH) Else Local $FileLocation = $sDestPath & "\" & $sLogFileName FileCopy("D:\Logs\" & $sSrcLogFolder[$i], $FileLocation, $FC_NOOVERWRITE + $FC_CREATEPATH) EndIf Next MsgBox(0,"","Done Copy...") EndIf EndFunc The result of my script is TQ1.txt TQ1_2.txt TQ1_4.txt TQ27.txt TQ27_1.txt TQ27_3.txt TQ302.txt Like windows copy/paste function, if it exist in the destination path, it will simply add copy, copy (1)...so on. But mine is to add _index. I want to achieve a result like this TQ1.txt TQ1_1.txt TQ1_2.txt TQ27.txt TQ27_1.txt TQ27_2.txt TQ302.txt Thanks for your help Edited May 29, 2018 by Tats
KickStarter15 Posted May 29, 2018 Posted May 29, 2018 @Tats, Is this what you want? try applying the below code on your code and see. Local $iIndex = 0 If FileExists($sDestination) Then Dim $szDrive, $szDir, $szFName, $szExt, $iIndex _PathSplit($sSource, $szDrive, $szDir, $szFName, $szExt) While 1 $iIndex = $iIndex + 1 $sFileTemp = $sDestination & $szFName & "_" & $iIndex & $szExt If Not FileExists($sFileTemp) Then ExitLoop WEnd FileCopy($sSource, $sFileTemp, 8) EndIf 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.
Tats Posted May 29, 2018 Author Posted May 29, 2018 (edited) @KickStarter15 applied your code to mine (with minor modification) and it works as what I needed. Thanks Edited May 29, 2018 by Tats
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now