Rilevare se un file è già aperto in javascript / hta
-
05-07-2019 - |
Domanda
Sto cercando di correggere un .hta creato da qualcuno nella mia azienda 3 o 4 anni fa. In questo momento se apri un file che qualcun altro ha già aperto, perderai tutto il lavoro che hai fatto su di esso e dovrai rifarlo manualmente. Quindi stavo pensando solo di verificare se il file è già aperto e quindi bloccare la modifica, o semplicemente fare un popup che dice "ehi, se provi a salvare rimarrai deluso". C'è un modo semplice per verificare se il file è già aperto in JavaScript?
Il codice per aprire il file è ...
function FileOpen( strSetup )
{
if( ! strSetup )
{
strSetup = ShowDialog( "choose-setup", {"DialogTitle" : "Load Setup"} );
}
if( strSetup )
{
if( FileSystem.FileExists( App.Paths.Configs + "/" + strSetup + ".setup" ) )
{
var fFile = FileSystem.GetFile( App.Paths.Configs + "/" + strSetup + ".setup" );
App.Config = LoadXMLDocument( fFile.Path );
// SaveCurrentConfig();
RefreshView( "setup-summary" );
}
else
{
alert( "Could not find setup '" + strSetup + "'" );
}
}
}
e il codice per LoadXMLDocument è ...
//-----------------------------------------------------------------------------
// FUNCTION : LoadXMLDocument - Loads an XML document
// params : strPath - the path/file of the document to load
// : bCritical- if set true, we die if the document doesn't load
// returns : an XML dom object on success, false otherwise
//-----------------------------------------------------------------------------
function LoadXMLDocument( strPath, bCritical )
{
var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0" );
xmlDoc.setProperty( "SelectionLanguage", "XPath" );
if( ! FileSystem.FileExists( strPath ) )
{
Error( "'" + strPath + "' is not a valid file path" );
if( bCritical ) Abort();
return( false );
}
var fFile = FileSystem.GetFile( strPath );
xmlDoc.load( fFile.Path );
if( xmlDoc.documentElement == null )
{
Error( "Could not load XML document '" + fFile.Path + "'" );
if( bCritical ) Abort();
return( false );
}
return( xmlDoc );
}
Soluzione
Vedo che questo è un post molto vecchio ma perché non usare VBS? Se si tratta di un HTA, sono supportati con l'esecuzione insieme:
objFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = "C:\test.txt"
If objFSO.FileExist(strFilePath) Then
MsgBox "I win"
End If
Altri suggerimenti
Ecco cosa controllo di revisione . Non è diverso da altre forme dello stesso problema che non comporta. file hta.