Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Chatgpt
- 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.
- --
- 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.
- <?php
- // Specify the directory to scan
- $directory = 'path/to/your/folder';
- // Scan the directory for .tap files
- $files = scandir($directory);
- foreach ($files as $file) {
- // Check for .tap files
- if (pathinfo($file, PATHINFO_EXTENSION) === 'tap') {
- // Get the filename without extension
- $filenameWithoutExt = pathinfo($file, PATHINFO_FILENAME);
- // Generate the DOS-compatible filename (8.3 format)
- $filename8 = substr($filenameWithoutExt, 0, 8); // Get first 8 characters
- $filename8 = strtoupper($filename8); // Convert to uppercase for DOS compatibility
- // Rename .scr files
- $scrFiles = glob("$directory/*.scr");
- foreach ($scrFiles as $scrFile) {
- $scrFileName = pathinfo($scrFile, PATHINFO_FILENAME);
- $newScrFileName = "$directory/$filename8-$scrFileName.scr";
- rename($scrFile, $newScrFileName);
- // Check if the .scr file contains the "run" keyword
- if (strpos(file_get_contents($newScrFileName), 'run') !== false) {
- copy($newScrFileName, "$directory/$filename8.scr");
- }
- }
- // Copy .txt files
- $txtFiles = glob("$directory/*.txt");
- foreach ($txtFiles as $txtFile) {
- copy($txtFile, "$directory/$filename8.txt");
- }
- }
- }
- Explanation:
- Directory Scanning: The script scans the specified directory for files.
- Finding .tap Files: It checks if each file has a .tap extension.
- Generating 8.3 Filename: It truncates the filename to the first 8 characters and converts it to uppercase to ensure DOS compatibility.
- 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.
- Copying .txt Files: It also copies all .txt files to the new format $filename8.txt.
- Notes:
- Make sure to adjust the $directory variable to point to the correct folder.
- Ensure that the script has appropriate permissions to read, rename, and copy files in the specified directory.
- Test the script in a safe environment to avoid accidental data loss.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement