Im writing a batch script to help me collect some bandwidth data about numerous offices on our WAN. It uses random data file creator to help me avoid wan optimisers affecting the results.
How can I copy a file by spawning a standard windows copy window that shows transfer rate?
If i use the 'copy' method, it just copies silently in the cmd window and i cant see the rate.
if not exist "C:\temp\xfertest" mkdir C:\test\xfertest
rdfc C:\test\xfertest\random100.dat 100000000
copy C:\test\xfertest\random100.dat \\nat-srv-007\Deliver
exit
This vbscript wrappped in a batch file will do what you want. Save it with a .bat/.cmd extension. Or without the first line with a .vbs extension.
The technic used is based on com and works with every script language supporting it.
Just to see start/end date time, total bytes and bytes per/s it outputs these values to the console after the copy dialog window has vanished.
rem^ &#cscript //nologo //e:vbscript "%~f0" %* & exit /b
' Copy with the windows dialog box
Option Explicit
Dim cArgs : Set cArgs = WScript.Arguments
Dim iArgCnt : iArgCnt = cArgs.Count
Dim sSource : sSource = cArgs.Item(0)
Dim sDest : sDest = cArgs.Item(1)
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim oSH : Set oSH = CreateObject("shell.application")
Dim oFile, Size, dStart, dEnd
If iArgCnt <> 2 Then
Wscript.Echo "Wrong args, need SourceFile and DestFolder"
Wscript.Quit
End if
If oFS.FileExists(sSource) Then
Set oFile = oFS.GetFile(sSource)
Size = oFile.Size
If oFS.FolderExists(sDest) Then
dStart = Now()
Wscript.Echo "Copy : " & sSource & " " & sDest
Wscript.Echo "Start: " & dStart & " Size : " & Size
FolderCopyHere sSource, sDest
dEnd = Now()
Wscript.Echo "End : " & dEnd & " per/s: " & _
Int(Size / DateDiff("s", dStart, dEnd))
Else
Wscript.Echo "Destination Folder doesn't exist" & sDest
End if
Else
Wscript.Echo "Source file doesn't exist" & sSource
End if
Wscript.Quit
function FolderCopyHere(sSource,sDest)
dim oFld
set oFld = oSH.NameSpace(sDest)
if not oFld is nothing then
oFld.CopyHere(sSource)
end if
set oFld = nothing
end function
Returning this output on my pc
20:28:24 C:\Test________________________________________
> k:\Bat\CopyExpl.cmd c:\test\big.file Q:\Test
20:28:36 C:\Test________________________________________
> rem &
Copy : c:\test\big.file Q:\Test
Start: 2016-10-30 20:28:36 Size : 2147483648
End : 2016-10-30 20:29:10 per/s: 63161283
20:29:10 C:\Test________________________________________
The Rem stems from the wrapper the dialog box is widely known. HTH
Related
I have batch file & vbs file that runs exe application in hidden mode.
Now I would like to open this exe applicatio, but with parameters passed to it.
Batch file:
wscript.exe "C:\~some path~\invisible2.vbs" "C:\~some path~\Rserve_d.exe"
invisible2.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Above code makes sure it runs hidden. But now I need to pass this parameter to the Rserve_d.exe when starting it:
--RS-conf "E:\~some path~\Rconf.cfg"
Please advise. I've tried with adjustments, but it seems, that there is always something wrong in the syntax.
Build the arguments string for your command from the arguments to the script:
Function qq(str)
qq = """" & str & """"
End Function
args = ""
For i = 1 To WScript.Arguments.Count - 1
If InStr(WScript.Arguments(i), " ") > 0 Then
args = " " & qq(WScript.Arguments(i))
Else
args = " " & WScript.Arguments(i)
End If
Next
CreateObject("Wscript.Shell").Run qq(WScript.Arguments(0)) & args, 0, False
Ansgar Wiechers posted his answer before I did so he should deserve the credits. Unfortunately, I had already made the effort of posting an answer as well. To provide some additional functionality to your batch script, you could also check for the return value of the executed VBScript.
Batch file:
setlocal
set "script=c:\~some path~\invisible2.vbs"
set "program=c:\~some path~\rserve_d.exe"
set "params=--RS-conf "e:\~some path~\rconf.cfg""
cscript "%script%" //nologo "%program%" %params%
:: %errorlevel% = 0 - VBScript was executed successfully
:: %errorlevel% = 1 - Missing arguments
:: %errorlevel% = 2 - Shell object creation failed
:: %errorlevel% = 3 - Run method was unable to execute the program
VBScript:
Option Explicit
On Error Resume Next
Dim objShell,_
strCmdLine,_
intCount
If (WScript.Arguments.Count < 1) Then
WScript.Quit(1)
End If
Set objShell = WScript.CreateObject("WScript.Shell")
If (Err.Number <> 0) Then
WScript.Quit(2)
End If
For intCount = 1 To WScript.Arguments.Count - 1
strCmdLine = strCmdLine & " " & """" & WScript.Arguments.Item(intCount) & """"
Next
objShell.Run """" & WScript.Arguments.Item(0) & """" & strCmdLine, 0, False
If (Err.Number <> 0) Then
WScript.Quit(3)
End If
Good morning,
I'm a beginner in programming code, so i'm sorry if I do something wrong.
I've wrote a code in VBS for backup some files from a folder to another.
My problem it is to compare the files date in both folders and allow the copy only if the file is new or the date has been changed.
Here my code, someone can help me to find the problem please?
I have tried but it is not working
' Copy a Folder
'Const OverWriteFiles = False
Dim strSourceFolder, strDestFolder
strSourceFolder = "E:\test1"
strDestFolder = "C:\test1"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "strSourceFolder" , "strDestFolder"
For each file in StrSourceFolder
ReplaceIfNewer ("file, strDestFolder")
Next
Sub ReplaceIfNewer (SourceFile, DestFolder)
Dim DateModifiedSourceFile, DateModifiedDestFile
DateModifiedSourceFile = SourceFile.DateModified()
DateModifiedDestFile = DestFolder & "\" & SourceFile.DateModified()
If DateModifiedSourceFile < DateModifiedDestFile then
Copy SourceFile to SourceFolder Else
End If
' Verify that a Folder Exists
'Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("strDestFolder") Then
MsgBox "Backup Copy Done." & vbCrLf & (Day(Now) & "\" & Month(Now) & "\" & Year(Now)) , Vbinformation
Set objFolder = objFSO.GetFolder("strDestFolder")
Else
MsgBox "Folder does not exist." , vbCritical , "Folder does not exist."
End if
Thanks and Be patient !
.DateModified is not VBScript. Start reading here. There is DateDiff, but as Dates are Doubles under the hood, comparisons with < will work too. In code:
>> Set f = CreateObject("Scripting.FileSystemObject").GetFile(WScript.ScriptFullName)
>> dlm = f.DateLastModified
>> WScript.Echo TypeName(dlm), dlm, "(german locale)"
>> dlmn = DateAdd("s", 2, dlm)
>> WScript.Echo TypeName(dlmn), dlmn, "(german locale)"
>> WScript.Echo DateDiff("s", dlmn, dlm), DateDiff("s", dlm, dlmn), CStr(dlm < dlmn)
>> WScript.Echo CDbl(dlm)
>> WScript.Echo CDbl(dlmn)
>>
Date 22.11.2013 13:09:53 (german locale)
Date 22.11.2013 13:09:55 (german locale)
-2 2 Wahr
41600,5485300926
41600,5485532407
I need to make a vbs file that asks for the minimum file size when you drag and drop a folder to it. It's a little wierd. But then, it should return the input as a string that will be turned into an integer. Then it should look for files that are bigger than this minimum file size(all, i guess) and list their folder(if it's in a sub-folder), name and size.
I found some stuff on the internet but I'm a bit lost
Option Explicit
Dim FolderPath, objFSO, objFolder, objFile, input, objArgs
input = InputBox("Minimum size: ")
Set objArgs = Wscript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")
For i = 0 to objArgs.count
on error resume next
Set objFolder = objFSO.GetFolder(objArgs(i))
If err.number <> 0 then
ProcessFile(objArgs(i))
Else
For Each file In folder.Files
ProcessFile(file.path)
Next
End if
On Error Goto 0
Next
Function ProcessFile(sFilePath)
msgbox "Now processing file: " & sFilePath
For each objFile in objFolder.Files
WScript.Echo objFile.Name, objFile.Size & "bytes" & VbCR_
& "created: " & objFile.DateCreated & VbCR_
& "modified: " & objFile.DateLastModified
Next
You've got some issues in your code. You're using folder.files but you don't have folder declared (or defined) anywhere. The only reason you're not getting an error is because you have On Error Resume Next specified. There's no need to use On Error here and it should be removed so that you can properly debug your script. Here's a starting point for you.
' Get the folder dropped onto our script...
strFolder = WScript.Arguments(0)
' Ask for minimum file size...
intMinSize = InputBox("Minimum size: ")
' Recursively check each file with the folder and its subfolders...
DoFolder strFolder
Sub DoFolder(strFolder)
' Check each file...
For Each objFile In objFSO.GetFolder(strFolder).Files
If objFile.Size >= intMinSize Then
WScript.Echo "Path: " & objFile.Path & vbCrLf & "Size: " & objFile.Size
End If
Next
' Recursively check each subfolder...
For Each objFolder In objFSO.GetFolder(strFolder).SubFolders
DoFolder objFolder.Path
Next
End Sub
This isn't a complete script. Notice I haven't declared objFSO anywhere. I haven't checked that strFolder is a valid folder or that intMinSize is actually a number. I'll leave it up to you to fill in the missing pieces. But this should get you going.
In my batch file, I am calling a VBScript and passing it 3 parameters. The parameters are "IPADDRESS" "PicName" and "Storage", as seen below:
Batch File:
set Pathname="C:\User\username\locationOfFile
cd /d %Pathname%
cscript.exe C:\User\username\locationOfFile\myScript.vbs IPADDRESS PicName Storage
In my VB6 program, the parameter values are defined.
Lets say for instance IPADDRESS = "170.190.xxx.xxx"
Is there a way I can have the batch file read and use the value of IPADDRESS based off the declaration inside the VB6 form? The variables IPADDRESS, PicName, and Storage will be constantly changing based off an outside application the VB6 program talks to so I cant statically set it up in the batch ( ....\myScript.vbs 170.190.xxx.xxx pic1 C:\Storage)
My VBScript is given below:
Option explicit
if WScript.Arguments.Count <> 3 then
WScript.Echo "Missing parameters"
else
Dim imageMagick
Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
Dim cam_add
Dim annotate
Dim filename
Dim cmd
Dim WshShell
Dim return
cam_add = """http://" & WScript.Arguments(0) &"/image"""
annotate = """" & WScript.Arguments(1) & " - """ '& Date
filename = """" & WScript.Arguments(2) & WScript.Arguments(1) & ".jpg"""
cmd = "convert " & cam_add & " -fill gold -pointsize 45 -gravity southwest -annotate 0x0+25+25 " & annotate & " -trim +repage -verbose " & filename
WScript.Echo cmd
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "C:\Program Files\ImageMagick-6.8.0-Q16\"
return = WshShell.Run(cmd)
end if
In summation, I need to to have the batch set up like:
cscript.exe C:\User\username\locationOfFile\myScript.vbs IPADDRESS PicName Storage
so the parameter values can used according to what they are set to inside my VB6 form.
If you were to:
shell "the.bat " & IPADDRESS & " " & PicName & " " & Storage
then within the.bat each space delimited argument is available via %N where N is the ordinal number; so %1 would contain the value of IPADDRESS, %2 would contain PicName's value and so on.
To then forward to the VBScript:
cscript.exe C:\User\username\locationOfFile\myScript.vbs %1 %2 %3
(If any of the variables contain spaces, you will need to "quote" them before passing to the bat file)
(You could also probably just chdir(..) in VB6 then run CScript directly)
For something this simple there is no need for a batch file anyway. After all, you have a whole script to work with.
You can even set the CD from inside there, or set it in the VB6 program as here:
Option Explicit
Private Sub Main()
Dim CD As String
Dim IPADDRESS As String
Dim PicName As String
Dim Storage As String
Dim OrigCD As String
CD = "D:\Photo Archives"
IPADDRESS = "127.0.0.1"
PicName = "Fudd"
Storage = "C:\Storage"
'Cache CD so we can restore it.
'Change CD and drive so it is inherited.
OrigCD = CurDir$()
ChDir CD
ChDrive CD
Shell "cscript """ _
& App.Path & "\test.vbs "" """ _
& IPADDRESS & """ """ _
& PicName & """ """ _
& Storage & """", _
vbNormalFocus
'Restore CD and drive.
ChDir OrigCD
ChDrive OrigCD
'Rest of program.
End Sub
Script sample:
Option Explicit
Private CD
Private Sub Msg(ByVal Text)
With WScript
.StdOut.WriteLine Text
.StdOut.Write "Press ENTER to continue..."
.StdIn.ReadLine
End With
End Sub
If WScript.Arguments.Count <> 3 Then
Msg "Missing parameters"
WScript.Quit
End If
'Show the CD and the arguments.
With CreateObject("WScript.Shell")
CD = .CurrentDirectory
End With
With WScript.Arguments
Msg CD & vbNewLine _
& """" & .Item(0) _
& """ """ & .Item(1) _
& """ """ & .Item(2) & """"
End With
But it doesn't even look like the script is needed. You can build the command, set the CD/drive, and Shell() the built command string all from inside the VB6 program.
I have a folder on my workstation where files are added every minute.
I have to monitor this folder every now and then, to see if new files are being added.
In case there is no new file in this folder for say 5 min, we perform an action.
Can we use batch file for this purpose in such a way that if there is no new file added for last 5 min, an alert /pop up apears on window screen.
Also I m new to Batch .Please let me know the steps
It seems unlikely that you are going to accomplish what you want to do with purely a batch file.
However, you can do this in a relatively simple/small VB Script, that doesn't require any additional installation on the system.
'-------------------------------------------------------------
' Monitors a folder for new files and warns if they
' are not being created within a certain time period.
'-------------------------------------------------------------
Dim intMinutes: intMinutes = 5 ' minute threshold for warning of no new files
Dim strDrive: strDrive = "c:" ' drive to monitor
Dim strPath: strPath = "\\temp\\" ' path to monitor. remember to double slashes
Dim intTimer: intTimer = "2"
Dim strComputer: strComputer = "."
Dim objWMIService: Set objWMIService = GetObject( "winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" )
Dim strQuery: strQuery = "Select * From __InstanceOperationEvent" & " Within " & intTimer & " Where Targetinstance Isa 'CIM_DataFile'" & " And TargetInstance.Drive='" & strDrive & "'" & " And TargetInstance.Path='" & strPath & "'"
Dim colEvents: Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
Dim LastNew: LastNew = Now
WScript.Echo "Monitoring new file creation... Press [Ctrl]-[C] to exit"
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
LastNew = Now
End Select
if DateDiff("n",LastNew,Now) >= intMinutes then
' put whatever actions you want in here... the following two lines are for demo purposes only
msgbox "The last new file was " & DateDiff("n",LastNew,Now) & " minute ago!",0,"No New Files"
exit do
end if
Loop
Execute this in WScript to make it not visible or CScript to show the console window.
Replace the code inside the IF block to do what you need done (notify you of the issue).