I really love FAR manager and I use it all the time. So first of all thanks to all the developers.
I was thinking about file/folder synchronization and what would it need to handle (symbolic links, hard links, ACLs, attributes, NTFS encrypted files, open files, network shares... )
Then I had an idea: It would be nice if FAR could copy open files. I searched "shadow copy farmanager" but only found one topic. (I was hoping for a plugin or something)
What I came up with is a User Menu entry to copy selected files:
@echo off
rem Copy open files using Volume Shadow Copy Service (FAR Manager User Menu script)
rem Author Szikra Istvan, 2011.05.08.
rem e-mail: szikra.istvan@freemail.hu
rem web: http://szikraistvan.no-ip.info/
rem http://foton.no-ip.com/blog/
rem call the same batch file as callback function with Shadow Copy Device parameter
if "%callback%"=="%~dpnx0" goto callback
if a%3==a (
:usage
echo Usage: %~nx0 ^<source path^> ^<destination path^> ^<filename list file^>
goto :EOF
)
SetLocal
set log="%~dpn0.log"
set vssvbs="%~dpn0.vbs"
set callback=%~dpnx0
set srcd=%~d1
set srcp=%~pnx1
set dst=%2
set filelist=%~3
rem ----- Logging -------
echo. >> %log%
echo --------- %date% %time% --------- >> %log%
echo %* >> %log%
type %3 >> %log%
call cscript /nologo %vssvbs% %srcd%
rem call cscript /nologo %vssvbs% %srcd% >> %log%
EndLocal
goto :EOF
rem ------------------ CallBack ---------------------
:callback
if a%1==a (
echo "Error in callback parameter!"
echo Should be called: %~nx0 ^<shadow copy device^>
goto :EOF
)
:copy
for /F "tokens=* delims= " %%f in (%filelist%) do (
echo copy "%srcd%%srcp%%%f" %dst%
rem xcopy /X /O /H /E /K /R /G /Y "%1%srcp%%%f" %dst%
echo copy /y "%1%srcp%%%f" %dst%
copy /y "%1%srcp%%%f" %dst%
)
' Copy open files using Volume Shadow Copy Service (FAR Manager User Menu script)
' Author Szikra Istvan, 2011.05.08.
' e-mail: szikra.istvan@freemail.hu
' web: http://szikraistvan.no-ip.info/
' http://foton.no-ip.com/blog/
if WScript.Arguments.Count<1 then
WScript.Echo "Parameters: <drive|mountpoint> [callback]"
WScript.Echo "If callback parameteris not specified %callback% environment variable will be used."
WScript.Echo "Callback will be called with the created Shadow Copy Name (\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy***\)"
WScript.Quit -1
end if
strVolume = WScript.Arguments(0)
if Len(strVolume) < 2 then ' only drive letter
strVolume = strVolume & ":\"
end if
if Right(strVolume,1) <> "\" then 'missing trailing backslash
strVolume = strVolume & "\"
end if
Set wshShell = CreateObject( "WScript.Shell" )
strRunCmd = wshShell.ExpandEnvironmentStrings( "%callback%" )
strLogFile = wshShell.ExpandEnvironmentStrings( "%log%" )
if WScript.Arguments.Count>1 then
strRunCmd = WScript.Arguments(1)
end if
if Len(strRunCmd)<1 then
strRunCmd = "VSCS.cmd"
end if
if Len(strLogFile)<1 then
strLogFile = "VSCS.vbs.log"
end if
WScript.echo "Callback: " & strRunCmd
WScript.echo "Log: " & strLogFile
strComputer = "."
Set svc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set shadow = svc.Get("Win32_ShadowCopy")
errResult = shadow.Create(strVolume, "ClientAccessible", strShadowID)
'The Volume parameter can be specified as a volume drive letter, mount point, or volume globally unique identifier (GUID) name.
if errResult <> 0 Then
WScript.echo "Error:" & errResult
else
WScript.echo "Created Shadow successfully"
Set colItems = svc.ExecQuery("Select * From Win32_ShadowCopy Where ID = '" & strShadowID & "'")
For Each objItem in colItems
WScript.Echo "Path to files is " & objItem.DeviceObject
WScript.Echo "Executing Callback..."
'Set oExec = wshShell.Exec( strRunCmd & " " & objItem.DeviceObject & " >> " & strLogFile )
Set oExec = wshShell.Exec( strRunCmd & " " & objItem.DeviceObject )
Set oExecOut = oExec.StdOut
Do While oExec.Status = 0
WScript.Sleep 100
While Not oExecOut.AtEndOfStream
strLine = oExecOut.ReadLine
WScript.Echo strLine
Wend
Loop
WScript.Echo "Callback Status " & oExec.Status
objItem.Delete_
Next
end if
I tested it on mail.pst, apache logs.. and it seem to work fine. Unfortunately xcopy doesn't seem to work with \\?\ paths.
Hope Shadow Copy will get into FAR 3.0
--
Istvan Szikra
Re: Copy open files using Volume Shadow copy Service
Posted: Mon 09 May, 2011 20:55
by alexmitin
Then I had an idea: It would be nice if FAR could copy open files. I searched "shadow copy farmanager" but only found one topic. (I was hoping for a plugin or something)
You forgot to mention that the user should have admin rights to use your script.
Re: Copy open files using Volume Shadow copy Service
Posted: Thu 12 May, 2011 17:02
by szir
Yes, I was referring to that topic
And you are right, Volume Shadow Copy Service requires admin rights to create shadow copy. Technically (I believe) Local System, Administrator, Network Service, Local Service have sufficient access rights. (on 2008 server additional users can be trusted with the appropriate privileges) http://technet.microsoft.com/en-us/libr ... S.10).aspx
Copy open files using Volume Shadow copy Service
Posted: Sun 26 Jan, 2020 11:06
by Kohler
Tried it, seems also that user needs admin rights to use script.
szir wrote: ↑Sun 08 May, 2011 19:32
What I came up with is a User Menu entry to copy selected files: