ngocthang26 Posted September 23, 2019 Posted September 23, 2019 Thank so much view my topic. I have problem when want to combine many file txt have both name. Example: A.txt -> ( A,B,C ) A (1).txt -> (D,E,F) A (2).txt -> (1,2,3) B.txt -> ( 2 ) B (1).txt -> ( 3 ) With code have result, All line code combine with name original A.txt -> ( A,B,C,D,E,F,1,2,3 ) B.txt -> ( 2,3 ) Thanks anyone.
rudi Posted September 23, 2019 Posted September 23, 2019 Hello, I just can guess, what you want to achive. All these files are in *ONE* folder, and shall be renamed to a continuous row A.TXT B.TXT ... or shall the content of all A.TXT A(1).txt ... go into a new A.TXT file? Could you pls. give an example, how the names currently are, and how you want them to look like, when the script did its job? regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE!
mikell Posted September 23, 2019 Posted September 23, 2019 Try this. Wildcards are your friends - the help file too $hSearch = FileFindFirstFile("A.txt") $sFileName = FileFindNextFile($hSearch) $sResult = FileRead($sFileName) & "," $hSearch = FileFindFirstFile("A *.txt") While 1 $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop $txt = FileRead($sFileName) $sResult &= $txt & "," WEnd $hTxt = FileOpen("result_A.txt", 2) ; 2 = overwrite $sResult = StringTrimRight($sResult, 1) ; remove the trailing comma FileWrite($hTxt, $sResult) FileClose($hTxt) FileClose($hSearch)
abberration Posted September 23, 2019 Posted September 23, 2019 (edited) And here's what I would do (assuming his files and data are literally what he stated): #include <file.au3> #include <array.au3> $array = _FileListToArrayRec(@ScriptDir & "\files", "*.txt", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH) For $i = 1 To $array[0] ; Rename the files that have no number to have a " (0)" to put them alphabetical order. If NOT StringInStr($array[$i], "(") Then $string = StringSplit($array[$i], ".", 2) FileMove(@ScriptDir & "\files\" & $array[$i], @ScriptDir & "\files\" & $string[0] & " (0)" & ".txt") EndIf Next $array = _FileListToArrayRec(@ScriptDir & "\files", "*.txt", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH) For $i = 1 To $array[0] ; Make the new files in the parent directory. $fileName = StringLeft($array[$i], 1) $data = FileRead(@ScriptDir & "\files\" & $array[$i]) $data = StringReplace($data, "-> ", "") FileWrite(@ScriptDir & "\" & $fileName & ".txt", $data) Next $array = _FileListToArrayRec(@ScriptDir, "*.txt", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH) For $i = 1 To $array[0] ; Replace all the )( with commas and clean up stray ) and (. $data = FileRead(@ScriptDir & "\" & $array[$i]) $makeCommas = StringReplace($data, ")(", ",") $cleanup = StringReplace($makeCommas, ")", "") $cleanup = StringReplace($cleanup, "(", "") _FileWriteToLine($array[$i], 1, $cleanup, True) Next I put the files in a folder called files and made the output in the script directory. Edited September 23, 2019 by abberration Added a line of code Easy MP3 | Software Installer | Password Manager
BrewManNH Posted September 24, 2019 Posted September 24, 2019 The old DOS command line had a way to do this with the copy command. you could do something like this: copy a.txt+a (1).txt+a (2).txt a_new.txt This would combine the 3 files into a new file called a_new.txt. 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 GudeHow 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
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