2017-01-12 1 views
1

Je tente d'automatiser l'extraction d'un ensemble de fichiers compressés (.ARJ) dans différents répertoires.Exécution d'une commande externe à l'aide de chaînes chargées à partir d'un fichier texte

J'utilise actuellement 2 fichiers texte pour stocker 2 bits d'information:

  1. l'emplacement actuel et le nom des fichiers compressés (ARJFileNames.txt) échantillon - D: _Work_Splunk_TestBed \ Branch00 \ LOAN.ARJ
  2. l'emplacement cible pour les fichiers à extraire à (ARJFileLocations.txt) échantillon - D: _Work_Splunk_TestBed \ Branch00

Je suis en train d'utiliser la commande WScript.Shell pour exécuter W inRAR pour extraire les fichiers de leur emplacement actuel vers un emplacement ciblé. Mon problème est quand j'appelle la commande externe de la boucle je ne peux pas sembler obtenir la syntaxe juste pour attacher les chaînes que je tire des dossiers de texte en tandem avec l'appel réellement à WinRar et à son commutateur/commander.

Voici mon code actuel:

'Declaring Constants 
Const ForReading = 1, ForWriting = 2, ForAppending = 3 
'Declaring Variables 
Dim fso, strFilePath, strFileName, fFilePath, fFileName, objShell, WinRAR, strCMD, SevenZip, ARJ 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set objShell = WScript.CreateObject ("WScript.shell")  

'Open Text Files for use 
Set strFilePath  = fso.OpenTextFile("D:\_Work\_Splunk\_TestBed\ARJFileLocations.txt", ForReading, TristateFalse) 
Set strFileName = fso.OpenTextFile("D:\_Work\_Splunk\_TestBed\ARJFileNames.txt", ForReading, TristateFalse) 


Do Until strFilePath.AtEndOfStream 
    fFilePath = strFilePath.ReadLine 'Get the location of the ARJ file 
    fFileName = strFileName.ReadLine 'Get the target location for ARJ file contents 
    'Storing the command as 1 string' 
    strCMD = "winrar x -y " & " " & fFileName & " " & fFilePath 
    'Running the command in CLI' 
    objShell.Run strCMD 
Loop 

'Cleaning Up 
Set strFilePath = Nothing 
Set strFileName = Nothing 
Set objShl = Nothing 

Répondre

1

référence Read Concatenation Operator (&).

La ligne de commande devrait apparaître en fin de compte comme il le ferait si vous l'avez saisi à l'invite de commande (vérifier par Wscript.Echo Command):

Command = """" & WinRAR & "\WinRAR.exe"" X " & fDLocation & " " & fTLocation 
'   ↑↑↑↑      ↑↑ 
' results to 
' "D:\Program Files\WinRAR\WinRAR.exe" X ARJLocation TargetLocation 
' ↑         ↑ 

Utilisation suivant si fDLocation ou fTLocation contiennent des espaces

Command = """" & WinRAR & "\WinRAR.exe"" X """ & fDLocation & """ """ & fTLocation & """" 
'           ↑↑     ↑↑ ↑↑     ↑↑↑↑ 
' results to 
' "D:\Program Files\WinRAR\WinRAR.exe" X "ARJ Location" "Target Location" 
' ↑         ↑ ↑   ↑ ↑    ↑ 

plus , Je envisagerais d'exécuter le script et le programme WinRAR.exe de manière synchrone (voir article Run Method (Windows Script Host)) comme suit:

Dim intRunResult 
Do Until strARJLocations.AtEndOfStream 
    fDLocation = strARJLocations.ReadLine  'Get the location of the ARJ file' 
    fTLocation = strTargetLocation.ReadLine 'Get the target location for ARJ file contents' 
    Command = """" & WinRAR & "\WinRAR.exe"" X """ & fDLocation & """ """ & fTLocation & """" 
    intRunResult = objShell.Run (Command, 1, True) 
Loop