Undisputed Posted January 15, 2023 Posted January 15, 2023 i have a script that tries to batch translate the text files in a folder from chinese to enlgish. the script runs successfully but doesnt create translated files expandcollapse popup#Require # Microsoft Azure Account and Bing Translator Text API Subscription Key # AutoIt v3 ; Create the FileListToArray() function Func FileListToArray($folder, $search) Local $file, $i = 1 Local $files[1] = 0 Local $fileHandle = FileFindFirstFile($folder & $search) If @error = 0 Then $file = FileFindNextFile($fileHandle) While $file <> "" $files[$i] = $file $i += 1 ReDim $files[$i] $file = FileFindNextFile($fileHandle) WEnd FileClose($fileHandle) Else MsgBox(16, "Error", "The folder does not exist or has no files matching the specified search criteria.") Exit EndIf Return $files EndFunc ; Function to translate text using Bing Translator API Func TranslateText($text, $from_lang, $to_lang) Local $subscriptionKey = "" Local $url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=" & $from_lang & "&to=" & $to_lang Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") $oHTTP.Open("POST", $url) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHTTP.SetRequestHeader("Ocp-Apim-Subscription-Key", $subscriptionKey) $oHTTP.Send('[{"Text": "' & $text & '"}]') If $oHTTP.Status <> 200 Then MsgBox(16, "Error", "An error occurred while connecting to the Bing Translator API. HTTP status code: " & $oHTTP.Status & " " & $oHTTP.StatusText) Exit EndIf Local $response = $oHTTP.ResponseText Local $json = JSON_Decode($response) If @error <> 0 Then MsgBox(16, "Error", "An error occurred while decoding the JSON response from the Bing Translator API.") Exit EndIf Return $json[0]["translations"][0]["text"] EndFunc # Main Script ; Specify the folder containing the text files Local $folder = @WorkingDir & "\test\" ; Get an array of all the files in the folder Local $files = FileListToArray($folder, "*.txt") ; Define the source and target languages Local $from_lang = "zh" Local $to_lang = "en" ; Loop through the array of files For $i = 1 To UBound($files)-1 ; Read the text file Local $text = FileRead($folder & $files[$i]) If @error <> 0 Then MsgBox(16, "Error", "An error occurred while reading the text file:" & $folder & $files[$i]) Exit EndIf ; Translate the text Local $translated_text = TranslateText($text, $from_lang, $to_lang) ; Write the translated text to a new file FileOpen($folder & "translated_" & $files[$i], 2) While @error = 5 Sleep(1000) FileOpen($folder & "translated_" & $files[$i], 2) WEnd FileWrite($folder & "translated_" & $files[$i], $translated_text) If @error <> 0 Then MsgBox(16, "Error", "An error occurred while writing the translated text to the file:" & $folder & "translated_" & $files[$i]) Exit EndIf Next ; Notify the user that the translation is complete MsgBox(0, "Translation Complete", "All files in the folder have been translated and saved with the prefix 'translated_'.")
Nine Posted January 15, 2023 Posted January 15, 2023 Seems to me that you are getting zero file from FileListToArray. I could be wrong though since I do not have your computer. But overall, you need to learn how to debug your own scripts. Put some ConsoleWrite and _ArrayDisplay after key statements to follow the path of your script and understand why it does not work as expected. SOLVE-SMART and AutoBert 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Trong Posted January 16, 2023 Posted January 16, 2023 (edited) The script with debug would look like this: Edited January 16, 2023 by VIP LINK Regards,
Undisputed Posted January 16, 2023 Author Posted January 16, 2023 i get No files translated in the folder: here are some files to try it on test.rar
rudi Posted January 16, 2023 Posted January 16, 2023 Hi. There is an existing function to list files to an array. I cannot test your translation part of your script, as I do not have... #Require # Microsoft Azure Account and Bing Translator Text API Subscription Key # AutoIt v3 Due to notepad++.exe your files are UTF-8, so modify this code to PUT the content to the translation engine: #include <FileConstants.au3> #include <Debug.au3> #include <File.au3> $Dir = @ScriptDir $aFiles = _FileListToArray(@ScriptDir, "*.txt") _DebugArrayDisplay($aFiles) For $i = 1 To $aFiles[0] $h = FileOpen($Dir & "\" & $aFiles[$i], $FO_UTF8) $Content = FileRead($h) FileClose($h) MsgBox(0, $aFiles[$i], $Content) Next Earth is flat, pigs can fly, and Nuclear Power is SAFE!
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