The batch script below can be used to automate file copy. You need to modify it to adapt to your needs.
echo off
echo Copy script
REM Source machines.
set SERVERS[0]=machine1
set SERVERS[1]=machine2
set SERVERS[2]=machine3
REM Source folder pattern.
set PATTERN=Folder_Pattern
REM Target location root.
set ROOT=machine_target_root
set VERBOSE=1
set "x=0"
:SymLoop
if defined SERVERS[%x%] (
REM call echo %%SERVERS[%x%]%%
call set SERVER=%%SERVERS[%x%]%%
echo %SERVER%
REM Create target folder.
mkdir %ROOT%\%SERVER%
FOR /f "tokens=*" %%i in ('DIR /a:d /b \\%SERVER%\%PATTERN%') DO (
if VERBOSE=1 (
ECHO Copy from \\%SERVER%\%%i\* to %ROOT%\%SERVER%\%%i\
)
xcopy \\%SERVER%\%%i\* %ROOT%\%SERVER%\%%i\
)
set /a "x+=1"
GOTO :SymLoop
)
:END
Showing posts with label windows script. Show all posts
Showing posts with label windows script. Show all posts
Monday, July 6, 2015
Friday, July 27, 2012
Some vbscript utilities
Here are some vbscript code examples.
--Table of contents--
1. Send email
2. Get computer name
3. Connect to database and execute query
4. Restart a windows service
5. Use of a log file
--
1. Send email
Sub sendEmail(ByRef text)
Dim objMessage
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "..."
objMessage.From = "..."
objMessage.To = "...@..."
objMessage.TextBody = "...text..."
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "..."
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =25
objMessage.Configuration.Fields.Update
objMessage.Send
End Sub
2. Get computer name
Dim objShell, strComputer
Set objShell = CreateObject("WScript.Shell")
strComputer = objShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
Set objShell = nothing
3. Connect to database and execute query
Option Explicit
on error resume next
Set objShell = CreateObject("WScript.Shell")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=SQLOLEDB.1;Data Source=localhost;Initial Catalog=<db>","<username>","<password>"
if err.number <> 0 then
WScript.Echo "error " & hex(err.number) & ": " & err.description
else
WScript.Echo "value = " & getVal()
objConnection.close
end if
on error goto 0
Set objConnection = Nothing
Set objShell = nothing
WScript.Quit(0)
Function getVal()
Dim sql, strWatchFile
sql = "select val from table"
set strWatchFile = objConnection.Execute( sql )
if err.number <> 0 then
WScript.Echo hex(err.number) & vbcrlf & err.description
getVal = -1
Exit Function
else
getVal = strWatchFile(0).value
strWatchFile.close
end if
set strWatchFile=nothing
End Function
4. Restart a windows service (by calling the batch file below)
objShell.Run "restart.bat ""<service name>""", 0, true
The three parameters are:
1) string: shell command, a batch file in this case (plus its parameter).
2) int: 1 - show window, 0 - hide window.
3) boolean: true - wait until the shell command ends, false - do not wait.
5. Use of a log file
Dim objFileSystem, objLogFile, logFileName, useLog
useLog = True
logFileName = "C:\mylog.log"
Set objShell = CreateObject("WScript.Shell")
openLog()
writeLog("hello world")
closeLog()
Set objShell = nothing
WScript.Quit(0)
Sub openLog()
If NOT useLog Then Exit Sub
Const OPEN_FILE_FOR_APPENDING = 8
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
If NOT objFileSystem.FileExists(logFileName) Then
Set objLogFile = objFileSystem.CreateTextFile(logFileName, TRUE)
Else
Set objLogFile = objFileSystem.OpenTextFile(logFileName, OPEN_FILE_FOR_APPENDING)
End If
End Sub
Sub writeLog(txt)
WScript.Echo txt
If useLog Then objLogFile.WriteLine(date & " " & time & ": " & txt)
End Sub
Sub closeLog()
If NOT useLog Then Exit Sub
objLogFile.Close
Set objLogFile = Nothing
Set objFileSystem = Nothing
End Sub
Restart windows service in a batch file
:: :: usage: restart.bat <service name> :: Note quotation marks can be used if the service name contains space. :: :: This batch script restarts a windows service (given as a parameter of the batch file). :: It does this by: :: 1) issue a stop command; :: 2) check the status of the service, go back to 1) if it is not stopped yet; :: 3) when the service has been stopped, restart it. :: :: Note: the provided service MUST exist, otherwise it will get into an infinite loop. :: :: References: :: [1] http://serverfault.com/questions/25081/how-do-i-restart-a-windows-service-from-a-script :: [2] http://boards.straightdope.com/sdmb/showthread.php?t=458812 :: [3] http://www.robvanderwoude.com/errorlevel.php :: :: @ECHO OFF if [%1]==[] goto end :stop sc stop %1 rem cause a ~2 seconds sleep before checking the service state ping 127.0.0.1 -n 2 -w 1000 > nul sc query %1 | find /I "STATE" | find "STOPPED" if errorlevel 1 goto :stop goto :start :start sc start %1 :end
Monday, October 24, 2011
Zip file in windows script
'
' http://superuser.com/questions/110991/can-you-zip-a-file-from-command-prompt
' http://www.rondebruin.nl/windowsxpzip.htm
' http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866%28v=vs.85%29.aspx
'
Dim objArgs, InputFolder, ZipFile, objShell, source, ct
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
' 4 - Do not display a progress dialog box.
objShell.NameSpace(ZipFile).CopyHere source, 4
'Keep script waiting until Compressing is done
ct = 0
Do Until objShell.Namespace(ZipFile).items.Count = objShell.NameSpace(InputFolder).items.count
WScript.sleep 1000
ct = ct + 1
Loop
WScript.Echo "Zip used " & ct & " seconds."
Set source = nothing
Set objShell = nothing
WScript.Quit(0)
Subscribe to:
Posts (Atom)
Blog Archive
-
▼
2026
(36)
-
▼
June
(19)
- C10K to C10M: from thread-per-connection model to ...
- Benchmark server performance
- Application server for php, python, java, node.js,...
- Application server for C++, Go and Rust
- Nginx as reverse proxy and load balancer
- Infrastructure running: nginx, apache, php, python...
- Flow chart of nginx+apache+uvcorn infrastructure
- Flow chart of apache+uvcorn infrastructure
- Uvicorn and Gunicorn
- What's deadsnakes PPA
- What's the optional lsb-core package
- Codex known logging bug
- Daemonsize a service
- Train text to image model, to generate images of c...
- Train a model based on OpenAI API
- Open port 8080 for WebSocket
- Add websocket support on Bluehost Ubuntu VPS for D...
- Install Claude Code on ubuntu VPS of Bluehost
- Install PostgreSQL on Mac
-
▼
June
(19)