🔒 Locked
Search & Replace for Files?
Advertisement
Original Post
Has anyone heard of a feature or command in Windows XP (or cmd prompt) that will search a whole tree for instances of files with a specific file name, and then replace those files with another? Funny background story (for those who want a long read). We use CVS here at work, but not everything we do is version-controlled through CVS. It really is a silly pipeline, but it will be better for our next project. In any case, CVS is responsible for updating certain folders in our project tree. Our file server also has an exact replica of this tree, which is used to store the stuff that isn't version-controlled through CVS. People just use a batch file to pull that data off the file server, and then they use CVS to update the other folders. Well, I accidently put some data up on that server that was bad. I needed a way to revert back to the previous versions of all that non-version-controlled stuff. So, in order to repair the situation, I went to a cooworker's computer (who hadn't updated his copy of the tree from the file server yet) and I uploaded all of HIS content back up to the server. Except...that included all of his hidden CVS folders. The root file has his user ID in the CVSROOT string and so now, when anyone in the company tries to do anything through CVS, they get an error message about their user ID or password being wrong. So, I need to edit all of these root files on each computer so that they have the correct user ID's. The problem is, there's 702 root files, so I figured it'd be easier to just delete the tree on each machine and check it out again. I've had to do that before, but not for so many people at once, and it's taking all day. Surprisingly, I'm not going to be fired for this. LOL.
Hah. If I do, they'll wait until I've completed the fix!
jk. It's not really a possibility.
jk. It's not really a possibility.
It's some simple batch scripting. Something like the following:
This script will walk the directory tree starting at C:\Folder, and for every file that begins with "file", and has the extension "ext", it will replace that file with ReplacementFile.ext located in C:\. Replace the filenames and paths as needed, save it into a text file with a .cmd extension, and execute it.
for /R C:\Folder %%i in (file*.ext) do copy /Y C:\ReplacementFile.ext %%iThis script will walk the directory tree starting at C:\Folder, and for every file that begins with "file", and has the extension "ext", it will replace that file with ReplacementFile.ext located in C:\. Replace the filenames and paths as needed, save it into a text file with a .cmd extension, and execute it.
Excellent, thanks!
Oh, take note: If there are any spaces in the filenames/paths, you need to add quotation marks around the names. IE:
Use this template instead, otherwise you might encounter problems.
for /R "C:\Some Folder" %%i in ("file 0*.ext") do copy /Y "C:\Replacement File.ext" "%%i"Use this template instead, otherwise you might encounter problems.
The only problem with that is that, from what I recall, not specifying a single character consisting of a wildcard or "." in a FOR statement will only enumerate files, not directories.
The following should do a directory-only search and move them if the path is a .cvs folder.
[edit:] Fixed. Twice.
[Edited by - silencer- on April 5, 2006 8:04:35 PM]
The following should do a directory-only search and move them if the path is a .cvs folder.
@ECHO offSETLOCALSETLOCAL ENABLEDELAYEDEXPANSIONFOR /R . %%f IN (.) DO ( SET DIR=%%f CALL SET DIR=%%DIR:~0,-2%% REM Moves the directory if the last four characters of the path == ".cvs" IF "!DIR:~-4!" == ".cvs" CALL move "!DIR!" "C:\Backup\" )ENDLOCAL[edit:] Fixed. Twice.
[Edited by - silencer- on April 5, 2006 8:04:35 PM]
The /R switch instructs the FOR expression to repeat the search within each level of the subtree nested at the target folder.
EDIT: Sorry, I misunderstood what you were saying. According to the OP, I don't believe he has to operate on folders. He only needs to replace files matching a search string within a folder tree.
EDIT: Sorry, I misunderstood what you were saying. According to the OP, I don't believe he has to operate on folders. He only needs to replace files matching a search string within a folder tree.
Thanks again guys. :)
CVS does store that information in the cvs directories, but the only piece of information I need to change is the cvsroot string that is stored in the Root files (which are found in the cvs directories). So, I basically created one Root file with the proper information and stored it in "C:\". I then plugged the file names into Nemesis's command there, and ran it, and it worked!
CVS does store that information in the cvs directories, but the only piece of information I need to change is the cvsroot string that is stored in the Root files (which are found in the cvs directories). So, I basically created one Root file with the proper information and stored it in "C:\". I then plugged the file names into Nemesis's command there, and ran it, and it worked!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement