:huh: Buenas tardes:
Estoy tratando de usar las variables de entorno de Windows en un vbs script, mediante Environment.
Para crear, copiar, mover, eliminar, etc, archivos y carpetas.
Ya use la opción buscar y no encontré algún manual de uso de Environment.
Podría alguien ayudarme a aprender el uso y aplicación de Environment?
Gracias.
Se te olvidó buscar bien: http://lmgtfy.com/?q=variables+entorno+vbs (http://lmgtfy.com/?q=variables+entorno+vbs)
CitarSet WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WshSysEnv("VariableSystem") = "valor1"
Set WshUserEnv = WshShell.Environment("USER")
WshUserEnv("VariableUser") = "val"
Por lo que veo, tienes razón.
Aunque no creo que se me HAYA OLVIDADO buscar bien, mejor dicho no busque bien.
Mil gracias por tu ayuda
Editando:
Como podrás darte cuenta todas las opciones aparecen como visitadas, leídas y es porque ya me pase 2 dias leyendo incluso buscando en ingles y no logro encontrar nada.
Nuevamente mil gracias.
Realmente no sé por qué iba a saber las 'opciones' que visitaste ·_·
Mil gracias
Leo Gutiérrez:
En otra ocasion me orientaste con una duda que tenia, nuevamente solicito tu ayuda, podrias decirme que estoy haciendo mal en estos scripts? :
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
Set objfso = createobject("scripting.filesystemobject")
Objfso.copyfile "C:\archivo.txt", "strHomeFolder", true
Set objShell = CreateObject("WScript.Shell")
userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")
Set objfso = createobject("scripting.filesystemobject")
Objfso.copyfile "C:\archivo.txt", "userProfilePath", true
Cuando los ejecuto por separado, ambos me crean los archivos strHomeFolder y userProfilePath respectivamente en la carpeta donde tengo el archivo vbs, pero no me realizan la copia de archivo.txt a %UserProfile%
Gracias.
usa las etiquetas GeSHi cuando publiques codigo en este caso puedes usar visual basic creo yo
no se realmente nada de vbs pero veo esto
Set WshSysEnv = WshShell.Environment("SYSTEM")
y tu tienes
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
me huele que es sin los "%"
engel lex:
Lo intente de las 2 formas y no lo logre, gracias por tu interes.
Sin embargo encontré otra forma de hacerlo, este es el código, espero le sea útil a alguien mas.
Aquí el código para copiar el archivo C:\archivo.txt a %UserProfile% y finalmente abrir archivo.txt, esta probado y funciona perfectamente, deshabilite el On Error Resume Next para en caso de que presentara un error, poder ubicarlo.
'On Error Resume Next
dim path
path=CreateObject("WScript.Shell").ExpandEnvironmentStrings("%UserProfile%")
dim objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(path & ("\archivo.txt")) = False Then
objFSO.CopyFile "C:\archivo.txt", path & "\"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run(""""&path & "\archivo.txt""")
End If
Probare con las demás variables de entorno.
No omito mencionar que la información (orientación) la obtuve de esta pagina:
http://community.spiceworks.com/scripts/show/732-copy-and-run-a-file-if-doesn-t-exist-in-userprofile
Saludos a todos.
Edito:
Lo probé con "%UserProfile%", "%PROGRAMFILES%", "%WINDIR%" y "%ALLUSERSPROFILE%"
Funciona perfectamente.
Ya te han explicado que debes utilizar las etiquetas GeShi, porfavor, respeta las normas.
Option Explicit
Dim objFSO, objShell, fullpath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
fullpath = objShell.ExpandEnvironmentStrings("%UserProfile%\Archivo.txt")
Select Case objFSO.FileExists(fullpath)
Case False ' El archivo no existe.
objFSO.CopyFile "C:\Archivo.txt", fullpath
objShell.Run(fullpath)
Case else ' El archivo existe.
' Do Nothing
End Select
WScript.Quit(0)
Saludos!
Eleкtro:
Perdón por lo de las etiquetas, prometo no repetirlo.
Gracias por el código.