Advertisement
plirof2

ZX SGD parser rename folder to 8.3

Oct 28th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Chatgpt
  2. I want to make a php program that will scan a folder. find the file that has .tap extension. The filename will be in the format filename.tap. I want in the variable $filename8 to shrink the filename so the whole filename is DOS compatible (8.3). Then I want to rename the *scr files by adding the $filename8 prefix. Then I want the *.scr file that contains the "run" keyword to be copied to a new file names $filename8.scr. Also if there is a *.txt file I want this also to be copied to $filename8.txt.
  3. --
  4. You can achieve this by using PHP's file handling functions. Below is a sample script that scans a folder for .tap files, generates a DOS-compatible filename (8.3), renames .scr files by adding the prefix, and copies relevant .scr and .txt files.
  5.  
  6. <?php
  7.  
  8. // Specify the directory to scan
  9. $directory = 'path/to/your/folder';
  10.  
  11. // Scan the directory for .tap files
  12. $files = scandir($directory);
  13.  
  14. foreach ($files as $file) {
  15.    // Check for .tap files
  16.    if (pathinfo($file, PATHINFO_EXTENSION) === 'tap') {
  17.        // Get the filename without extension
  18.        $filenameWithoutExt = pathinfo($file, PATHINFO_FILENAME);
  19.        
  20.        // Generate the DOS-compatible filename (8.3 format)
  21.        $filename8 = substr($filenameWithoutExt, 0, 8); // Get first 8 characters
  22.        $filename8 = strtoupper($filename8); // Convert to uppercase for DOS compatibility
  23.  
  24.        // Rename .scr files
  25.        $scrFiles = glob("$directory/*.scr");
  26.        foreach ($scrFiles as $scrFile) {
  27.            $scrFileName = pathinfo($scrFile, PATHINFO_FILENAME);
  28.            $newScrFileName = "$directory/$filename8-$scrFileName.scr";
  29.            rename($scrFile, $newScrFileName);
  30.            
  31.            // Check if the .scr file contains the "run" keyword
  32.            if (strpos(file_get_contents($newScrFileName), 'run') !== false) {
  33.                copy($newScrFileName, "$directory/$filename8.scr");
  34.            }
  35.        }
  36.  
  37.        // Copy .txt files
  38.        $txtFiles = glob("$directory/*.txt");
  39.        foreach ($txtFiles as $txtFile) {
  40.            copy($txtFile, "$directory/$filename8.txt");
  41.        }
  42.    }
  43. }
  44.  
  45. Explanation:
  46. Directory Scanning: The script scans the specified directory for files.
  47. Finding .tap Files: It checks if each file has a .tap extension.
  48. Generating 8.3 Filename: It truncates the filename to the first 8 characters and converts it to uppercase to ensure DOS compatibility.
  49. Renaming .scr Files: It finds all .scr files, renames them to include the $filename8 prefix, and checks if they contain the keyword "run". If they do, it copies them as $filename8.scr.
  50. Copying .txt Files: It also copies all .txt files to the new format $filename8.txt.
  51. Notes:
  52. Make sure to adjust the $directory variable to point to the correct folder.
  53. Ensure that the script has appropriate permissions to read, rename, and copy files in the specified directory.
  54. Test the script in a safe environment to avoid accidental data loss.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement