Replace a file in a folder tree with an updated version?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

jesped wrote: 2023-11-24, 19:25 UTCAny idea what can be wrong and what more I can try?
There was an idea to use sfk, but there are also partial problems with it.
I made .vbs with checking the version, modification date and size if the date matches:

Code: Select all

'——————————————————————————— VBS ———————————————————————————
' Purpose:    Update files in the folder tree of the
'             target panel with the selected same-name files

' Parameters: %WL "%T"

' Source URL: http://ghisler.ch/board/viewtopic.php?p=445730
'————————————————————————————————————— Author: Flasher © ———
Option Explicit
Dim A, C, oFSO, oDic, oRgx, List, F, V, TPath, Once
Set A = WSH.Arguments: C = A.Count: If C = 0 Then WSH.Quit
If C <> 2 Then MsgBox "Specify 2 parameters!", 262192: WSH.Quit
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDic = CreateObject("Scripting.Dictionary")
Set List = oFSO.OpenTextFile(A(0),,,-1)
Set oRgx = New RegExp: oRgx.Global = 1
oRgx.Pattern = "\d*?(\d{1,5}\b)"
Do: F = "\\.\" & List.ReadLine
   If Right(F, 1) <> "\" Then
      V = oFSO.GetFileVersion(F)
      If V <> "" Then V = "|" & Ver(V)
      With oFSO.GetFile(F)
         oDic.Add F & "|" & .Size, .DateLastModified & V
      End With
   End If
Loop Until List.AtEndOfStream: List.Close
If Len(A(1)) > 3 Then TPath = "\\.\" & A(1): Once = 1 Else TPath = A(1)
On Error Resume Next: Recursion oFSO.GetFolder(TPath)
CreateObject("WScript.Shell").Popup "          Done!", 1,_
" Update files in the folder tree     ", 4160
Set List = Nothing: Set oFSO = Nothing: Set oDic = Nothing

Sub Recursion(oDir)
   Dim K, F2, Check, oFile, FDate, TDate, D
   If IsEmpty(Once) Then If Len(oDir.Path) > 3 Then _
   Set oDir = oFSO.GetFolder("\\.\" & oDir.Path): Once = 1
   For Each K in oDic
      F = Split(K, "|")
      TPath = oFSO.BuildPath(oDir.Path, "\")
      F2 = TPath & oFSO.GetFileName(F(0))
      If oFSO.FileExists(F2) Then
         Check = 0
         If InStr(oDic(K), "|") Then
            Check = StrComp(Split(oDic(K), "|")(1), Ver(oFSO.GetFileVersion(F2)), 1)
            If Check = 1 Then oFSO.CopyFile F(0), TPath, 1
         End If
         If Check = 0 Then
            Set oFile = oFSO.GetFile(F2)
            FDate = oFile.DateLastModified
            TDate = CDate(Split(oDic(K), "|")(0))
            If TDate > FDate Then
               oFSO.CopyFile F(0), TPath, 1
            ElseIf TDate = FDate Then
               If F(1) <> oFile.Size Then _
               oFSO.CopyFile F(0), TPath, 1
            End If
         End If
      End If
   Next
   For Each D in oDir.SubFolders: Recursion D: Next
End Sub

Function Ver(V)
   Ver = Mid(oRgx.Replace(Replace("." & V, ".", ".0000"), "$1"), 2)
End Function
Save the code to %COMMANDER_PATH%\Scripts\UpdateFilesInTargetTree.vbs, drag it to the button bar and specify the parameters in a new button from the script header.
Last edited by Fla$her on 2023-11-28, 10:41 UTC, edited 8 times in total.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 150
Joined: 2005-02-07, 03:28 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *jesped »

Fla$her wrote: 2023-11-25, 22:52 UTC specify the parameters in a new button from the header.
I don't understand this last part...
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

The header is the comments on top of the code.
Fixed version comparison.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 150
Joined: 2005-02-07, 03:28 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *jesped »

Gives an error on line 24:

Image: https://i.postimg.cc/FFbWdR93/image.png
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

Check it now.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 150
Joined: 2005-02-07, 03:28 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *jesped »

Same, but at line 29 now:

Image: https://i.postimg.cc/C5vyDC3r/image.png
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

Understood. I didn't check first on the drive root. See now.
Last edited by Fla$her on 2023-11-27, 03:06 UTC, edited 1 time in total.
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Replace a file in a folder tree with an updated version?

Post by *beb »

2jesped
Out of curiosity, why do you guys keep torturing each other while there's a functional command?
beb wrote: 2023-11-25, 21:34 UTC 2jesped try the below PowerShell-based one-liner command for your button:

Code: Select all

[em_test_tc_update_existing_files_command]
cmd=Powershell -ExecutionPolicy Bypass;
param=Get-Content """%UL"""|ForEach-Object -process {foreach ($file in (gci """%T""" -recurse)) {if ($file.name -eq (Split-Path $PSitem -leaf)) {Copy-Item $PSitem $file.fullname}}}
Or in the form of a pure button:

Code: Select all

TOTALCMD#BAR#DATA
Powershell -ExecutionPolicy Bypass;
Get-Content """%UL"""|ForEach-Object -process {foreach ($file in (gci """%T""" -recurse)) {if ($file.name -eq (Split-Path $PSitem -leaf)) {Copy-Item $PSitem $file.fullname}}}
wcmicon2.dll,63
Replace the files in the folder structure of the passive|panel with the ones selected in the active panel

1
-1
Did you even try this?
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

2beb
No one is torturing anyone. Powershell starts slowly, shows the console, and is absent on XP. I suggested a more optimal solution with a version comparison, among other things, which I originally proposed. You don't have that. %UL, unlike %WL, doesn't support long (260+) paths. Also you don't compare the size. And as far as I know, powershell by default also has a problem with MAX_PATH. I also have a handy pop-up window informing about the completion.
Of course, you can fix everything except the first one, but it is the most valuable.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 150
Joined: 2005-02-07, 03:28 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *jesped »

Fla$her wrote: 2023-11-27, 03:18 UTC 2beb
No one is torturing anyone.
I hope not. :lol:

I am really grateful for all the work you are doing with this.

Now it says error in Line 52 Char 16

Image: https://i.postimg.cc/k47twZX5/image.png
Bohny
New Member
New Member
Posts: 1
Joined: 2023-08-04, 00:24 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Bohny »

jesped wrote: 2023-11-27, 05:42 UTC
Fla$her wrote: 2023-11-27, 03:18 UTC 2beb
No one is torturing anyone.
I hope not. :lol:

I am really grateful for all the work you are doing with this.

Now it says error in Line 52 Char 16

Image: https://i.postimg.cc/k47twZX5/image.png
start path ->null
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

2jesped
There is no torture in bringing the code to a working state. It’s a hobby after all. :wink:
I'll still try to go back to '.' instead of '?', because the first error occurred for a different reason. Check it.
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Replace a file in a folder tree with an updated version?

Post by *beb »

2jesped
2Fla$her
Good luck then. The path is made by walking, by walking you make a path.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
User avatar
jesped
Member
Member
Posts: 150
Joined: 2005-02-07, 03:28 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *jesped »

It works now on a Folder, but it gives this error when used on drive root:

Line: 58
Char: 4
Error: Permission denied

Image: https://i.postimg.cc/CLbnTSBK/image.png

Also, the pop-up window that says "Done" is immediately closed. Don't know if that is intentional.
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Replace a file in a folder tree with an updated version?

Post by *Fla$her »

Hmm. I tried pressing the button when there are drive roots on both sides. There were no errors.
Are you writing about the target panel? If yes, are there any hidden/system folders (like 'System Volume Information' or something) on it?
If On Error Resume Next is added between line 28 and 29, will the update in the subfolders happen?

Yes, the pop-up window differs from the usual one in that it appears for a set period of time (I specified 1 second).
Replacement usually happens quickly, so the probability of missing the moment is too small.
Overquoting is evil! 👎
Post Reply