finding a text file and replacing it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hiho
Posts: 6
Joined: 21 Dec 2012 04:49

finding a text file and replacing it

#1 Post by hiho » 21 Dec 2012 04:56

Hi guys,

I've been trying to make a batch file which searches for 2 files (gamemenu.res & browse.vdf) and then replace them with my own version of the files, but since searching the whole system every time will be eating too much resources, it thought it would be better to make a list of the locations (C:\some-folders\...\, D:\some-folders\..., E:\some-folders\...). Could you please help me? I read a lot of tutorials and tried to make my own, but...

Thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: finding a text file and replacing it

#2 Post by foxidrive » 21 Dec 2012 07:16

Try this: (untested)

c:\mypath is where you have stored your files.

Code: Select all

@echo off
for %%a in (
"C:\some-folders\"
"D:\some-folders\"
"E:\some-folders\"
) do (
dir /b "%%a\gamemenu.res" >nul 2>&1 && copy /y /b "c:\mypath\gamemenu.res" "%%a"

for %%a in (
"C:\some-folders\"
"D:\some-folders\"
"E:\some-folders\"
) do (
dir /b "%%a\browse.vdf" >nul 2>&1 && copy /y /b "c:\mypath\browse.vdf" "%%a"

hiho
Posts: 6
Joined: 21 Dec 2012 04:49

Re: finding a text file and replacing it

#3 Post by hiho » 21 Dec 2012 07:24

Hi foxydrive, thanks for trying to help me. But this is the main problem - I don't know the exact location of the files e.g which partion, folders...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: finding a text file and replacing it

#4 Post by foxidrive » 21 Dec 2012 07:26

Who is going to create the list of locations? If you don't want to search the drives then someone has to create the list.


Be more precise in what you want to do...

hiho
Posts: 6
Joined: 21 Dec 2012 04:49

Re: finding a text file and replacing it

#5 Post by hiho » 21 Dec 2012 08:05

I was hoping the batch file to scan the partions one time and then to make the list.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: finding a text file and replacing it

#6 Post by foxidrive » 21 Dec 2012 08:15

So you want these two files to be located on your computer, and note where they are? You can do that with the file search function.
Then you can use the batch file above...

Otherwise how do you want to proceed? Please explain what you want the batch file to do.

hiho
Posts: 6
Joined: 21 Dec 2012 04:49

Re: finding a text file and replacing it

#7 Post by hiho » 21 Dec 2012 08:25

Ok. What I want the batch file to do is :
1. Search for the files on the current HDD.
2. Make a list of the locations of those files - doesn't matter where the list is saved (c:\list.txt, d:\1.txt... doesnt matter).
3. Delete the files from those locations.
4. Copy/paste my version (3 + 4 = replacing them).

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: finding a text file and replacing it

#8 Post by DigitalSnow » 21 Dec 2012 15:03

Here is a script that will scan all the available HDD partitions and write out the matching files to a text file. This will handle steps 1 and 2.

Search Script (Steps 1 & 2):

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "xOutput=C:\Output.txt"
>%xOutput% ( <nul set /p "=" )

for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
   pushd "%%~D:\" 2>nul
   if !ErrorLevel! EQU 0 (
      for /f "delims=" %%F in ('dir /a:-d /b /s gamemenu.res browse.vdf') do echo.%%~fF>>%xOutput%
   )
   popd
)
endlocal
pause



Replace Script (Steps 3 & 4):

Code: Select all

@echo off
setlocal
for /f "usebackq delims=" %%L in ("C:\Output.txt") do (
   if "%%~nxL"=="gamemenu.res" xcopy "C:\Temp\gamemenu.res" "%%~fL" /l /r /y
   if "%%~nxL"=="browse.vdf" xcopy "C:\Temp\browse.vdf" "%%~fL" /l /r /y
)
endlocal
pause


Remove the /l option from the xcopy commands when you want to actually commit to the changes. The /l option causes it to only preview what it would copy and not actually copy the files.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: finding a text file and replacing it

#9 Post by foxidrive » 21 Dec 2012 17:09

DigitalSnow wrote: if "%%~nxL"=="gamemenu.res" xcopy "C:\Temp\gamemenu.res" "%%~fL" /l /r /y
if "%%~nxL"=="browse.vdf" xcopy "C:\Temp\browse.vdf" "%%~fL" /l /r /y



You should include the /i ignore case switch in the if command as the files may have any capitalisation.

hiho
Posts: 6
Joined: 21 Dec 2012 04:49

Re: finding a text file and replacing it

#10 Post by hiho » 22 Dec 2012 02:48

Hi DigitalShadow and foxidrive. Thanks for the effort to try to help me. I appreciate it!

I've made a batch file with the first code and ran it - it made a file named "Output.txt", but it was empty - and the cmd window showed "No files found" which is impossible because I have at least 3 files named "GameMenu.res".

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: finding a text file and replacing it

#11 Post by foxidrive » 22 Dec 2012 04:02

Try this version: it will probably say no files found several times as it tests all drives.

Code: Select all

@echo off
set "xOutput=C:\Output.txt"
del "%xOutput%" 2>nul

for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%D:\ dir /a:-d /b /s %%D:\gamemenu.res %%D:\browse.vdf  >>"%xOutput%"
)
pause

hiho
Posts: 6
Joined: 21 Dec 2012 04:49

Re: finding a text file and replacing it

#12 Post by hiho » 22 Dec 2012 13:23

Thank you both of you. Works like a charm.

Happy Holidays and Best Wishes!

Post Reply