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: 2328
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-22, 01:07 UTCWhere do I set that?
Click.
jesped wrote: 2023-11-22, 01:07 UTCI also need to update .fx .fxh and .addon64 Reshade shaders
As for the button, for several selected files, you will need to replace "%Q%N" with "%S".
jesped wrote: 2023-11-22, 01:07 UTC which I don't think would have an easy way to compare their version numbers.
In the condition, it's possible to set a comparison of versions where they are, and where they are not, compare by date and size.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 151
Joined: 2005-02-07, 03:28 UTC

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

Post by *jesped »

Tried now in another SSD disk with games (S:) and it doesn't replace all the files:

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

Also, it creates a %~fd folder...

Image: https://i.postimg.cc/z3m4Dhzc/image.png
User avatar
Stefan2
Power Member
Power Member
Posts: 4159
Joined: 2007-09-13, 22:20 UTC
Location: Europa

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

Post by *Stefan2 »

jesped wrote: 2023-11-21, 04:30 UTC Is there a way for Total Commander to replace files in a folder tree with an updated version?

Let's say I want to automatically update a Reshade shader with the new version in all the subfolders of my C:\Games where that file already exists...

or update the nvngx_dlss.dll Nvidia DLSS library...


You may want to try PowerShell or VBS:


This script will do:
- For each sub-folder / and there for each file ...
- If FileName = sItem Then
- - - GetParentFolderName
- - - Copy "sItem" to "ParentFolderName\sItem , overwrite existing: true
- End If


STEPS:
-- save this script as "something.VBS".
-- copy this VBS to the wanted top-main-folder.
-- have the "sitem" in that folder with the VBS too.
-- adjust
oStartFolder = "." ' dot=current folder (try with an TEST folder and with a few sub folders and files first)
sItem = "shader.exe" 'Name of the file to find and name of the file to copy (must be in same folder as this script, else we had to adjust this script)
-- double click the VBS to execute it.


Code: Select all

'----------------------
' 2023-11-22 by Stefan
' Replace a file in a folder tree with an updated version?
' https://ghisler.ch/board/viewtopic.php?p=445580#p445580
'
'  This script will do:
'  - For each sub-folder / and there for each file ...
'  - If FileName = sItem Then
'  - - - GetParentFolderName of sItem 
'  - - - Copy "sItem" to  "ParentFolderName\sItem" , overwrite existing: true
'  - End If
'
'STEPS:
'-- save this script as "something.VBS".
'-- copy this VBS to the wanted top-main-folder.
'-- have the "sitem" in that folder with the VBS too.
'-- adjust
'oStartFolder = "."    ' dot=current folder (try with an TEST folder and with a few sub folders and files first)
'sItem = "shader.exe"  'Name of the file to find and name of the file to copy (must be in same folder as this script, else we had to adjust this script)
'-- double click the VBS to execute it.
'----------------------
Set oFSO = CreateObject("Scripting.FileSystemObject")
'----------------------
' User Settings
'// the folder to work in:
'oStartFolder =  "C:\Work\2023" '(try with an TEST folder and with a few sub folders and files first)
 oStartFolder = "." '(the current folder of this script)
'// the item or token to work on:
sItem = "shader.exe"  'Name of file to find and name of file to copy (must be in same folder as this script, else we had to adjust this script)
'----------------------
'get files from oStartFolder:
If(1=0) Then
		Set oFolder = oFSO.GetFolder(oStartFolder)
		'Wscript.Echo oFolder.Path
		Set colFiles = oFolder.Files
		For Each oFile in colFiles
			'Wscript.Echo oFile.Name
			'Wscript.Echo oFile.Path
									' oFile.Extension
									' oFile.FileName
									' oFile.FileSize
									' oFile.FileType
									' oFile.LastAccessed
									' oFile.LastModified
			funForEachFileDo oFile.Path
		Next
End If
'----------------------
'get files from all sub folders:
strOUTvar = ""
ShowSubfolders oFSO.GetFolder(oStartFolder)
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        Set oFolder = oFSO.GetFolder(Subfolder.Path)
        Set colFiles = oFolder.Files
        For Each oFile in colFiles
						funForEachFileDo oFile.Path
        Next
        'Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

'----------------------

Function funForEachFileDo(inputStr)
			'debug:
			'Wscript.Echo inputStr

			'alternative a:
			'strOUTvar = strOUTvar & inputStr & vbCRLF
			'alternative b:
					'If UCase(oFSO.GetExtensionName(oFile.name)) = "TXT" Then
					'If oFSO.GetBaseName(oFile.name) = "BASENAME" Then
					'If oFSO.GetFileName(oFile.name) = "FILENAME.ext" Then
					If oFSO.GetFileName(inputStr) = sItem Then
							'Wscript.Echo inputStr
							'strOUTvar = strOUTvar & inputStr & vbCRLF
							strGetParentFolderName = oFSO.GetParentFolderName(inputStr)
							'Wscript.Echo inputStr & vbLF & strGetParentFolderName
							'oFSO.CopyFile(Source, Dest [,Overwrite (True/False)]
							oFSO.CopyFile sItem, strGetParentFolderName & "\" & sItem , true
					End If
End Function ' funForEachFileDo()

'----------------------

'Output, write to file in current working dir:
If(1=0) Then
		If(Len(strOUTvar) > 0) Then
			Set NewFile = oFSO.CreateTextFile("___Logfile.txt", True)'True=overwrite
			NewFile.WriteLine(strOUTvar)
			NewFile.Close
		End If
End If
'----------------------

Wscript.Echo "done"

'EOF

HTH?
Fla$her
Power Member
Power Member
Posts: 2328
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-22, 14:51 UTC Tried now in another SSD disk with games (S:) and it doesn't replace all the files:

Image: https://i.postimg.cc/JzCWMftz/image.png
Because you are trying to do this from a list with search results where there is no common parent folder, and the working directory in this case is %COMMANDER_PATH%. In addition, the file modification date is older than the one on the left.
In addition, from such a list, %N (or %O.%E) returns the full name instead of the name, which will be an error for the robocopy syntax.
jesped wrote: 2023-11-22, 14:51 UTC Also, it creates a %~fd folder...

Image: https://i.postimg.cc/z3m4Dhzc/image.png
If you didn't change anything in the button, then this couldn't happen.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 151
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-22, 19:46 UTC Because you are trying to do this from a list with search results where there is no common parent folder, and the working directory in this case is %COMMANDER_PATH%. In addition, the file modification date is older than the one on the left.
No, no. The search results on the left are just to show you that some files weren't updated, and those files have an older modified date.

The source files are the ones in the right panel, I pressed the button having S:\ drive listed on the left (inactive) panel.

As you can see by the search results, the SuperDepth3D.fx file in the right (source) has a Modified date of November 21, 2023, while the one in the left has a Modified date of November 1, 2020.
Fla$her wrote: 2023-11-22, 19:46 UTC
jesped wrote: 2023-11-22, 14:51 UTC Also, it creates a %~fd folder...
Image: https://i.postimg.cc/z3m4Dhzc/image.png
If you didn't change anything in the button, then this couldn't happen.
This is how it looks now:

Command: %ComSpec% /q/c for /r
Parameters: "%T" %%d in (.) do >nul robocopy . "%%~fd" "%S" /log+:update.log /np /ns /ndl /njh /njs /v /xo /xl

Maybe it has to do with the target drive/folder being "S:\" (root) now instead of the previous "A:\Games" ?
Last edited by jesped on 2023-11-22, 20:25 UTC, edited 2 times in total.
User avatar
jesped
Member
Member
Posts: 151
Joined: 2005-02-07, 03:28 UTC

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

Post by *jesped »

Stefan2 wrote: 2023-11-22, 15:07 UTC You may want to try PowerShell or VBS:

This script will do:
Thanks for the help, man. But Fla$her solution is more handy for my needs. Having a button that works with a live selection and a target folder is all I can ask for.

If we can't find out why it's not working right in my other ssd I will test your script.
Fla$her
Power Member
Power Member
Posts: 2328
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-22, 20:00 UTC No, no. The search results on the left are just to show you that some files weren't updated
Can you show the text from update.log?
jesped wrote: 2023-11-22, 20:00 UTC This is how it looks now:
Try this:

Code: Select all

TOTALCMD#BAR#DATA
%ComSpec% /q/v/c for /r
"%T" %%d in (.) do set "d=%%d"&robocopy . "!d:~,-2!" "%S" /ndl /nfl /njh /njs /xo /xl
wcmicon2.dll,63
Replace the files in the folder structure of the passive|panel with the ones selected in the active panel

1
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 151
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-22, 20:34 UTC Try this:
It is really weird, because it *does* work for the files inside the green area... but it doesn't for the ones in the red area, even when they clearly have an earlier Modified date.

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

There is no log now.
Fla$her
Power Member
Power Member
Posts: 2328
Joined: 2020-01-18, 04:03 UTC

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

Post by *Fla$her »

2jesped
Are these files definitely not busy? Can they be removed?
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 151
Joined: 2005-02-07, 03:28 UTC

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

Post by *jesped »

Yes, they can be deleted (and recovered):

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

Previous button log is this one: https://drive.google.com/file/d/11l7Q_IKANpkgZDsTu-LHVumruCkDlPrp/view?usp=sharing
User avatar
jesped
Member
Member
Posts: 151
Joined: 2005-02-07, 03:28 UTC

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

Post by *jesped »

Also, another weird thing. Many of the folders, but not all, had their attributes changed:

Image: https://i.postimg.cc/J01SskS6/image.png
Fla$her
Power Member
Power Member
Posts: 2328
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-23, 01:38 UTC Yes, they can be deleted (and recovered):
You need to delete files past the Recycle Bin. To the Recycle Bin is not a deletion, but a move in essence.
jesped wrote: 2023-11-23, 01:38 UTC Previous button log is this one: https://drive.google.com/file/d/11l7Q_IKANpkgZDsTu-LHVumruCkDlPrp/view?usp=sharing
There is only "lonely", i.e. folders do not contain files of the same name.
jesped wrote: 2023-11-23, 01:55 UTC Also, another weird thing. Many of the folders, but not all, had their attributes changed:
We are facing this here in the task of deleting empty folders. You need to find out what attributes such folders had before using the button.
Overquoting is evil! 👎
User avatar
jesped
Member
Member
Posts: 151
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-23, 16:36 UTC
jesped wrote: 2023-11-23, 01:38 UTC Yes, they can be deleted (and recovered):
You need to delete files past the Recycle Bin. To the Recycle Bin is not a deletion, but a move in essence.
Mmm... but when they can be deleted to the recyble bin that means they are not locked or in use by other app. When I get that with other files they can't be deleted, moved or renamed. I have to restart TC, Explorer or even Log Off/Restart Windows.
Fla$her wrote: 2023-11-23, 16:36 UTC There is only "lonely", i.e. folders do not contain files of the same name.
So it seems it didn't found them then, but the folders with the files inside are there.
Fla$her wrote: 2023-11-23, 16:36 UTC We are facing this here in the task of deleting empty folders. You need to find out what attributes such folders had before using the button.
The folders had no specific attributes and nothing weird. Only thing I can think of is that I customized their icons... 🤔

Any idea what can be wrong and what more I can try?
User avatar
beb
Senior Member
Senior Member
Posts: 436
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 »

jesped wrote: 2023-11-23, 01:55 UTC Also, another weird thing. Many of the folders, but not all, had their attributes changed...
jesped wrote: 2023-11-24, 19:25 UTC The folders had no specific attributes and nothing weird. Only thing I can think of is that I customized their icons...
This is a downside of using rococopy, and there's no workaround.
Still, since you're customizing folders' icons, they gain the 'read-only' attribute and obtain a hidden desktop.ini file inside with the icon source path.
Regarding the latter, you can restore the 'read-only' attribute of those folders (so you can see their customized icons again) as follows:

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
for /f %%i in ('dir /b/a/s "." ^| find /i "desktop.ini"') do (set d=%%~dpi
attrib !d:~,-1! +r
attrib !d:~,-1!)
Explanation:
- as soon as the script finds a "desktop.ini" in a directory it sets the 'read-only' attribute for it.
- "." means the current directory from where the script begins its job; so the cmd should be placed accordingly, or the "." should be changed to the desirable starting path.
As for myself, after trying robocopy for a bit, I decided to not use it anymore. There always a better solution could be found.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
User avatar
beb
Senior Member
Senior Member
Posts: 436
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 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}}}
What it does:
- takes a file under the cursor, or all the selected files (if any) in the active TC pane [source files];
- searches for the above file(s) in the directory structures within the passive/target TC pane [target files];
- if the said target files are found they will be replaced with the said source files.

If I understood it correctly that is what you need, and that's it.
Spoiler
I tested the said command within the following environment:

PowerShell_make_test_environment.ps1

Code: Select all

# PowerShell : update existing target files if any : test environment

# target files: within system %TEMP% folder
'make test environment...'
$probe = "$env:Temp\probe"
$files = @("$probe\test\SIZE.txt","$probe\test\text.en.txt","$probe\test\text.readme.txt")
New-Item $files -force -Type file -value 'file' > $null

# source files: wherever the script runs
$files = @("SIZE.txt","text.en.txt","text.readme.txt")
New-Item $files -force -Type file -value 'updated' > $null
Last edited by beb on 2023-11-26, 06:03 UTC, edited 1 time in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Post Reply