Find and RePlace Files - Not Text [resolved]

Any other tech-related topics
Message
Author
Stoik
Posts: 83
Joined: Fri Jan 29, 2016 12:25 pm

Find and RePlace Files - Not Text [resolved]

#1 Post by Stoik »

I need a tool that can FIND and REPLACE FILES,
NOT the TEXT inside the files (there are plenty of those).

In my case, I have a massive folder "C:\Portable\"
where I keep all of my portable applications.

Lets say, inside there are many files "Do Not Empty this Folder.odt" scattered all over the place.
They are all the same (same warning text inside them).

Now, I want to replace them with "Do Not Empty this Folder.txt" to save place.
I can find them easily with something like Everything,
but then I have to replace them manually one by one.

In other words, I need a file finding program,
that can not only find files by certain criteria (usually, name plus/minus extension),
but then also replace them.

Does anyone know of such a tool ?

I have seen some tools or batch files that can replace a single file at an exactly predefined location,
but not a tool that than first find multiple files at unknown locations - before replacing them.
Last edited by Stoik on Mon Feb 01, 2016 3:50 am, edited 1 time in total.

NickR
Posts: 105
Joined: Thu Aug 26, 2010 6:37 am

Re: Find and RePlace Files - Not Text

#2 Post by NickR »

Hi
Whats your OS?
What File System is the disk?


To Clarify:

Across multiple folders
you want to RENAME all files called
"Do Not Empty this Folder.odt" to
"Do Not Empty this Folder.txt"

and CLEAR those file's contents,
which is a known text string,
to make the file size 0 bytes

-------------
Beware Bulk renaming files is a lot more dangerous to your system than most people realise.
think of 'rename' as 'delete' in sheep's clothing
backup and take great care

Stoik
Posts: 83
Joined: Fri Jan 29, 2016 12:25 pm

Re: Find and RePlace Files - Not Text

#3 Post by Stoik »

NickR, thanks for Your reply.

My OS is Windows 8 (64-bit), my disk is NTFS.

But no,
I do NOT want to RENAME the files,
I do NOT want to change their CONTENT,
I want to actually REPLACE the FILES.

Maybe the same file name ("Do Not Empty this Folder")
caused this confusion, but one is ODT, the other is TXT.

Here is a better example :
Most of the portable apps from John T. Haller have a ReadMe.txt
inside the Apps sub-folder; they have the same text (content).
Imagine, I want to replace all of these 50 "*\Apps\ReadMe.txt" with
my own file "*\Apps\Do Not Tweak This Folder.rtf" that gives a more detailed explanation,
but instead of doing this manually 50 times (delete each "ReadMe.txt" & copy the "Do Not Tweak This Folder.rtf")
I can do it with a "find & replace files" program with one or two clicks.

This is just one example,
You can come up with many other uses
when You are working on a project.

User avatar
webfork
Posts: 10818
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Find and RePlace Files - Not Text

#4 Post by webfork »

Two things (neither terribly helpful):

* I seem to recall a thread a few years back that focused on updating multiple, specific files with their newer versions e.g. lame_enc.dll but I can't seem to track it down.

* I imagine there are some tools that network admins have to fix this out there as updating lots of programs with one or more files is a fairly common practice.

User avatar
SYSTEM
Posts: 2041
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Find and RePlace Files - Not Text

#5 Post by SYSTEM »

It is easy enough to write a simple program for this, so I did so. :)

Download: https://drive.google.com/uc?export=down ... ENrQWRTYlE
How to use
How to use
It requires .NET Framework 2.0 to run. Source code:

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FindAndReplaceFiles
{
    class Program
    {
        const string PROGRESS_TEXT = "{0:d}/{1:d} files replaced";

        static void Main(string[] args)
        {
            string sourceFile;
            do
            {
                Console.Write("Enter the source file: ");
                sourceFile = Console.ReadLine();
            }
            while (!File.Exists(sourceFile));
            string targetDirectory;
            do
            {
                Console.Write("Enter the directory from which to search: ");
                targetDirectory = Console.ReadLine();
            }
            while (!Directory.Exists(targetDirectory));
            string searchPattern;
            {
                Console.WriteLine("Enter the search pattern.");
                Console.WriteLine("Wildcards are permitted.");
                searchPattern = Console.ReadLine();
            }

            string[] targetFiles = Directory.GetFiles(targetDirectory, searchPattern, SearchOption.AllDirectories);
            Console.WriteLine();
            int leftmostColumn = Console.CursorLeft;
            Console.Write(PROGRESS_TEXT, 0, targetFiles.Length);
            string sourceFileName = Basename(sourceFile);

            for (int i = 0; i < targetFiles.Length; ++i)
            {
                File.Delete(targetFiles[i]);
                File.Copy(sourceFile, String.Format("{0}\\{1}", GetDirectory(targetFiles[i]), sourceFileName));
                Console.SetCursorPosition(leftmostColumn, Console.CursorTop);
                Console.Write(PROGRESS_TEXT, i + 1, targetFiles.Length);
            }
        }

        static string GetDirectory(string filePath)
        {
            return filePath.Substring(0, filePath.LastIndexOf('\\'));
        }

        static string Basename(string filePath)
        {
            return filePath.Substring(filePath.LastIndexOf('\\') + 1);
        }
    }
}
My YouTube channel | Release date of my 13th playlist: August 24, 2020

hamasaki
Posts: 197
Joined: Tue Apr 23, 2013 11:16 pm

Re: Find and RePlace Files - Not Text

#6 Post by hamasaki »

Can`t you do it with Advanced Renamer? I use that all the time to batch rename files.

http://www.portablefreeware.com/index.php?id=1848

Or did you need the original file to stay and a new one created alongside it?

Stoik
Posts: 83
Joined: Fri Jan 29, 2016 12:25 pm

Re: Find and RePlace Files - Not Text

#7 Post by Stoik »

Hi, everyone !

I am the one who created this topic (question). Thank You all for Your replies.
Here is my comment to all of You (from top to bottom) ...

WebFork :
You are right, I imagine this would be something very useful to network administrators.
Or a programmer who wants to replace all his standard notes (e.g. disclaimer, etc) in all of his projects.

System :
Man (of Lady), You are a genius !!! I tried it, and it works like a charm. Just two comments ...
1. When I do the final "Enter", Your program does the job beautifully,
but I did not get the confirmation (2/2 files replaced) like in Your screen-shot (but this is totally minor).
2. Much more importantly, could You make a simple GUI for it, so it is easier + faster to use,
also that would avoid typing errors - by entering the items (names & paths) via copy & paste, or via drag & drop.
The 3 boxes to fill might have these 3 clear names:
1. "Directory where to search (path), sub-directories will be included",
2. "Which files do You want to find (name)", & 3. "Which file do You want to replace it with (path with name)".
Thank You, thank You, thank You !

Hamasaki :
I want to remove (replace) the old file, not just rename it, so Advanved Renamer is not it.
Otherwise, Advanced Renamer is my absolutely favorite program.

User avatar
SYSTEM
Posts: 2041
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Find and RePlace Files - Not Text

#8 Post by SYSTEM »

Stoik wrote: System :
Man (of Lady)
Man. :P
Stoik wrote: I tried it, and it works like a charm. Just two comments ...
Great. :)
Stoik wrote: 1. When I do the final "Enter", Your program does the job beautifully,
but I did not get the confirmation (2/2 files replaced) like in Your screen-shot (but this is totally minor).
Did you launch the program by double-clicking it? In that case, the CMD window closes as soon as the program terminates, and therefore you don't have time to see the progress message. (In any case, the message is there only for the case where there is a lot of files and replacing them takes some time.)
Stoik wrote: 2. Much more importantly, could You make a simple GUI for it, so it is easier + faster to use,
also that would avoid typing errors - by entering the items (names & paths) via copy & paste, or via drag & drop.
The 3 boxes to fill might have these 3 clear names:
1. "Directory where to search (path), sub-directories will be included",
2. "Which files do You want to find (name)", & 3. "Which file do You want to replace it with (path with name)".
No thanks. A GUI program is much more work than a simple command-line program. I'm not willing to do it.

By the way, you can paste into CMD windows. Pasting via Ctrl-V is only possible in Windows 10, but in earlier versions of Windows you can click the icon at the top-left corner of the window and select Edit -> Paste.
My YouTube channel | Release date of my 13th playlist: August 24, 2020

Stoik
Posts: 83
Joined: Fri Jan 29, 2016 12:25 pm

Re: Find and RePlace Files - Not Text

#9 Post by Stoik »

Hi, System !

Regarding GUI
:
Fair enough, I thank You for the freebie part that You already did contribute.
I hate those bloggers, who get something for free and then bitch about the one of two things that a missing.
I am not a programmer, that is why I was looking for outside help.
But I will try to muddle though with AutoHotKey to create some simple GUI.
If I succeed, I will share it with You all.

Greetings,
Stoik.

Stoik
Posts: 83
Joined: Fri Jan 29, 2016 12:25 pm

Re: Find and RePlace Files - Not Text

#10 Post by Stoik »

Actually, I found the answer to my own question,
a very satisfactory solution - that settles it for me..

Two easy steps (& GUI based) :
1. find all the files You want to delete with a standard "find files" program (e.g. File Searchy, or UltraFileSearch),
then in the results window apply "Select All" & "Delete",
2. paste the new file You want disseminated with the program "N2nCopy" to the many folders where it needs to go.

N2nCopy is a great little tool from Jose Antonio :
http://joseantonio.comule.com/misproyec ... jects.html

As far as I can tell,
N2nCopy is portable, stealth (it does not save any settings), and has no "bulky" dependencies (like Net Framework).

I did not find it on TPFC,
so it may be a consideration as an entry (as a result of this discussion).

User avatar
webfork
Posts: 10818
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Find and RePlace Files - Not Text

#11 Post by webfork »

Stoik wrote:Actually, I found the answer to my own question
Good to hear. Would you mind editing your first post on the topic and putting "[resolved]" at the end? So ..

Code: Select all

Re: Find and RePlace Files - Not Text [resolved]

User avatar
tactictoe
Posts: 283
Joined: Thu Dec 10, 2015 10:56 am
Location: A galaxy far far downunder
Contact:

Re: Find and RePlace Files - Not Text [resolved]

#12 Post by tactictoe »

I was reading this request, and decide to grant a version with a GUI. Why? Because I get bored during my break sometimes and my brain is a crazy binary thinking machine all the time and all weather, :shock:
So I decided to write a portable software called Search and delete that permits a little bit more than Webfork work (which is excellent, btw).
SAR01.png
You can find this exercise my brain file here:
- 32bits version: http://www.geocities.ws/tactictoe/Delph ... 32bits.zip
- 64bits version: http://www.geocities.ws/tactictoe/Delph ... 64bits.zip

and it's home page link: http://www.geocities.ws/tactictoe/SearchAndReplace.html

Don't tell the Portable FREEWARE collection does never spoil it's user, :D :mrgreen:

User avatar
SYSTEM
Posts: 2041
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: Find and RePlace Files - Not Text [resolved]

#13 Post by SYSTEM »

tactictoe wrote: So I decided to write a portable software called Search and delete that permits a little bit more than Webfork work (which is excellent, btw).
I think you mean my work...

Anyway, it's really kind of you to create a GUI program for this. Thank you. :)
My YouTube channel | Release date of my 13th playlist: August 24, 2020

User avatar
tactictoe
Posts: 283
Joined: Thu Dec 10, 2015 10:56 am
Location: A galaxy far far downunder
Contact:

Re: Find and RePlace Files - Not Text [resolved]

#14 Post by tactictoe »

I think you mean my work...
Oops my mistake, I do not compute very well at 1:30am. Please accept my apologize.

User avatar
tactictoe
Posts: 283
Joined: Thu Dec 10, 2015 10:56 am
Location: A galaxy far far downunder
Contact:

Re: Find and RePlace Files - Not Text [resolved]

#15 Post by tactictoe »

I start to like this software, so I decided to update it.
Hope that it will help peoples, :wink:
SAR01.png

Reason for Edit: had upload the wrong snapshot

Post Reply