Skip to main content
gamedev.net
🔒 Locked

Search & Replace for Files?

Started by uncle_rico Apr 5, 2006 at 5:38 PM 9 replies 900+ views
Original Post
uncle_rico
uncle_rico
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.
Tradone
Tradone
Hopefully you don't get fired.
uncle_rico
uncle_rico
Hah. If I do, they'll wait until I've completed the fix!

jk. It's not really a possibility.
Nemesis2k2
Nemesis2k2
It's some simple batch scripting. Something like the following:

for /R C:\Folder %%i in (file*.ext) do copy /Y C:\ReplacementFile.ext %%i


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.
Nemesis2k2
Nemesis2k2
Oh, take note: If there are any spaces in the filenames/paths, you need to add quotation marks around the names. IE:

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.
silencer-
silencer-
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.

@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]
Nemesis2k2
Nemesis2k2
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.
silencer-
silencer-
Well, I believe CVS--just like SVN--stores all its repository information in the .cvs subdirectories, so deleting them should be the goal here.

[edit:] If it doesn't work this way for CVS, I'm sorry. In SVN you can import respositories without the .svn subs.
uncle_rico
uncle_rico
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!
Tradone
Tradone
while your thinking "I hope I don't get fired"
your company is probably thinking "I hope he fixes it"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.