Announcement

Collapse
No announcement yet.

Help Rename File with special character

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help Rename File with special character

    Hello everyone i am having trouble to rename a specific file. although i can do a rename it manually but i want to do it in the software.

    here is the example file name
    Original file name: All I Want For Christmas Is You - Mariah Carey【cover 】.mp3

    the files name when loaded it will replaces all special character into a question mark ?

    since the file has special character the media player cannot play the music. i try to rename it on the software before loaded in media player but i have no success..

    Here is my code after browse the file:

    nD = String.SplitPath(mp3[1]).Drive;
    nF = String.SplitPath(mp3[1]).Folder;
    nSp = nD..nF

    sText = String.SplitPath(mp3[1]).Filename;
    sExt = String.SplitPath(mp3[1]).Extension;
    tText = sText..sExt;


    _target1 = String.Find(sText, "?", 1, false);
    if _target1 > 0 then


    nName = String.Replace(sText, "?", "", false);

    File.Rename(nSp..tText, nSp..nName..sExt);

    --Check to see if an error occurred.
    error = Application.GetLastError();
    -- If an error occurred, display the error message.
    if (error ~= 0) then
    Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
    end

    MediaPlayer.Load("Plugin1", nSp..nName..sExt);

    end

    Anyhelp..? Thanks...

  • #2
    Hey Telco you should check your paths, looks like a lot of errors
    nSp = nD..":\"..nF

    also maybe try something like this
    Code:
    files = File.Find(mypath, "*.sat.txt");
    error = Application.GetLastError();
    if (error ~= 0) then
    Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
    else
    if (files == nil) then
    Dialog.Message("Info", "No files *.sat.txt found in " .. mypath, MB_OK);
    else
    for i, path in pairs(files) do
    -- replace '.sat.txt' with '.txt' but only if it's at the end of the path
    local newPath = string.gsub(path, '%.sat%.txt$', '.txt');
    File.Rename(path, newPath);
    end
    end
    end

    Comment


    • #3
      Hi colc thanks for your reply.. we lost internet for more than a week.. i will check your solutions and back to you.. thank you so much..

      Comment


      • #4
        I'm trying to rename a file as per the example above, but fail to get it to work; I've tried to follow the above exercise, but am still failing.

        Code:
        -- folder has a file named F00C0T20210824201342538.jpg (images from a security camera that get uploaded hourly)
        -- the numbers in the filename "20210824201342538" can vary after F00C0T*.jpg
        -- In this project, every hour we want to rename F00C0T*.jpg to yard.jpg
        
        mypath = Dialog.FolderBrowse("Select Folder", "F:\\Temp\\pix"); -- this is the testing folder where images are located; change later
        
        files = File.Find(mypath, "\\F00C0T*.jpg");
        error = Application.GetLastError();
        if (error ~= 0) then
        Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
        else
        if (files == nil) then
        Dialog.Message("Info", "No image files with F00C0T found in " .. mypath, MB_OK);
        else
        for i, path in pairs(files) do
        -- replace 'numbers.jpg' with '.jpg' but only if it's at the end of the path
        local newPath = string.gsub(path, '%C0T*%.jpg$', '.jpg');
        File.Rename(path, newPath);
        end
        end
        end
        
        if File.DoesExist (mypath.."\\F00.jpg") then
        
        File.Delete(mypath.."\\yard.jpg", false, false, true);
        Application.Sleep(10000);
        File.Rename(mypath.."\\F00.jpg", mypath.."\\yard.jpg");
        
        else
        
        Dialog.Message("Info", "Files not found", MB_OK);
        end
        The issue here is that the file F00C0T*.jpg isn't being renamed. Perhaps I'm missing something here - any help appreciated.

        John
        Classic IT Support
        https://www.classicit.net
        https://www.message7.org

        Comment


        • #5
          Try this to perform the task you described:
          Code:
          local sSourceFolder = "F:\\Temp\\pix"; -- this is the testing folder where images are located; change later
          local sTargetFilename = "yard.jpg";
          
          -- folder has a file named F00C0T20210824201342538.jpg (images from a security camera that get uploaded hourly)
          -- the numbers in the filename "20210824201342538" can vary after F00C0T*.jpg
          local tFiles = File.Find(sSourceFolder, "F00C0T*.jpg");
          error = Application.GetLastError();
          if (error ~= 0) then
             Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
          else
             if (tFiles == nil) then
                Dialog.Message("Info", "No image files with F00C0T found in " .. sSourceFolder, MB_OK);
             else
                for i = 1, #tFiles do
                   -- In this project, every hour we want to rename F00C0T*.jpg to yard.jpg
                   if File.DoesExist(sSourceFolder .. "\\" .. sTargetFilename) then
                      File.Delete(sSourceFolder .. "\\" .. sTargetFilename);
                   end
                   local tPath = String.SplitPath(tFiles[i]);
                   File.Rename(tFiles[i], tPath.Drive .. tPath.Folder .. sTargetFilename);
                end
             end
          end
          Ulrich

          Comment


          • #6
            Thank you Ulrich. This indeed works. I will carefully study the above, and of course, learn from it. Again, much appreciated!
            Classic IT Support
            https://www.classicit.net
            https://www.message7.org

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎