Just a thought... what's the best way to write the .bat to file using stringtofile, while maintaining line breaks needed for the batch file execution?
I have created a variable TStringList, ready to write, but I'm not sure how to pass the following into it. Does TStringList have to be written to line by line?
Edit: I'm sure there is a better way, but I finally got it to work. I had to construct the .bat as a single line as I couldn't figure out how to build the file line by line (line breaks) with Pal.
Thanks for Autoshell Chris_Kewl, I'll be using it for some things I haven't been able to do in SAM.
This PAL script is configured to send data to AutoShell.
Made by Chris "Kewl" Haslage, modified by square_eyes, old.square.eyes [at] gee mail dot com to build and execute an external batch file.
This script creates a windows batch file and runs it.
The batch file searches for the most recently created file in a directory, and copies it to a different folder.
It is meant to be part of a greater script for kicking off pre-recorded shows and came about from a need to select and queue the latest file in the folder.
Because SAM and PAL don't have this feature, the necessary commands to run the batch file are passed from PAL through HTTP to the Autoshell app running in the background.
More info on Autoshell can be found here: http://support.spacialaudio.com/forums/viewtopic.php?f=23&t=36288
Variables from other parts of your PAL script can be passed directly to the batch file or along with command line instructions passed to Autoshell.
// Settings - Change these as required
const Source = 'Z:\Source'; // Folder of mp3s to search
const Destination = 'Z:\Destination'; // Folder to copy the latest file to
const CommandSwitches = '/V /D /Y'; // XCOPY Command switches see here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
const LineBreak = ' & '; // Adds a line break that the batch file will understand. So that all code can be keept on a single line. There is no need to change this.
const BatchFile = 'Z:\Source\CopyLatestFile.bat'; // Location and name of the file if it exists, or if not, where it will be created.
var BatchContents: TStringList;
var i : Integer;
var URL : String = 'http://127.0.0.1:9000';
var PASS_KEY : String = '123456'; // Auto Generated in AutoShell
var START_CHAR : String = '?'; // Default Value is ?
var SEPARATOR : String = '|'; // Default Value is |
var END_CHAR : String = '^'; // Default Value is ^
// CODE BELOW LINE
// Get started by preparing a blank .bat file
if not fileexists(BatchFile) then
AppendStringToFile(BatchFile,'') // If the file does not exist this will create it
strtofile(BatchFile,''); // Clears the contents of the .bat file by blanking it
BatchContents := TStringList.Create; // Create a temporary list object to build in the .bat code
// Create batch file string list. Note that the final batch file must be a single line string e.g. (line 1) & (line 2) & (line 3). The below automatically add the '&' line breaks and brackets. Batch variables must be in the form !Var!
BatchContents.Add('(@echo off & setlocal enableextensions enabledelayedexpansion)'+LineBreak);
BatchContents.Add('(set myfolder_=' + Source + ')' + LineBreak);
BatchContents.Add('(for /f "delims=" %%f in (' + '''' + 'dir /o:d /a:-d /b "!myfolder_!\*.mp3"' + '''' +') do (set latest_=!myfolder_!\%%~nxf))' + LineBreak);
BatchContents.Add('(if defined latest_ (echo rem latest file "!latest_!" & xcopy "!latest_!" "' + Destination + '" ' + CommandSwitches + ') else (echo No designated files found in "!myfolder_!"))' + LineBreak);
BatchContents.Add('(Pause)' + LineBreak);
BatchContents.Add('(endlocal & goto :EOF)');
for i := 0 to BatchContents.Count-1 do // Loop through the temp list object (-1 because the first line is 0)
AppendStringToFile(BatchFile,BatchContents[i]); // Write to the .bat file on each parse
WriteLn('.bat file ready');
PAL.WaitForTime('+00:00:02'); // Small wait to make sure the OS has saved the file
//Run newly created .bat from Autoshell. This is Chris_Kewl's part.
var postResult : String = WebToStr (URL + START_CHAR + PASS_KEY + SEPARATOR +
'call "' + BatchFile + '"' + SEPARATOR +
'pause' + END_CHAR);
WriteLn('Post Status: ['+postResult+']'); // OK is good / ERR is bad
if (postResult = 'OK') then
WriteLn('Do some other code things here.'); // You can add conditional code here which runs on successful receipt of code by Autoshell.
WriteLn('An error with AutoShell has occurred.');
BatchContents.Free; // Free up your temp list to avoid memory leaks