Jump to content

script for searching and replace a file


Recommended Posts

is there someone that can write a batch or a vbs script for me?

the script must scan multipule harddrives in search of a file, lets say bla.dll

it must replace the "bla.dll" and back up the old one like "bla.dll.bak"

and ad some message like file not found in the script

the new "bla.dll" is in the same folder as the batch and or vbs file

can a script wizard help me with this?

thanks for reading this ;)

sorry for the verry bad english :blushing:

Link to comment
Share on other sites


Using VBScript and the FileSystemObject to Replace Batch Files

http://windows.oreilly.com/news/vbscriptpr_0201.html

Code example

http://www.windowsdevcenter.com/examples/w...iptpr_code.html

The code is self explainatory and will help you a fair bit :)

i did find that site with google to but that is not what i'm looking for

but what i need is scan more hd's, backup, and replace a file

i have no experience with vbs scripting so i need some help with it

Link to comment
Share on other sites

well i'll help as much as i can

for a start, the code can be modified like this

CheckFolder (objFSO.getfolder("c:\")), "folder_name\filename"

just add the addition line for say for example "D: Drive"

CheckFolder (objFSO.getfolder("d:\")), "folder_name\filename"

change the extension in the CheckFolder function code from

strSearch = ".bat"

to your desired file type for example "DLL"

strSearch = ".dll"

And for this section:

If UCase(strTemp) = UCase(strSearch) Then
'Got one
[b]Obviously we need to find an example to copy the original, create the backup and then once thats been done, copy the new file[/b]
End If

I'll look for some neccesary functions and examples for you so you can understand some more :)

Link to comment
Share on other sites

cool keep it comming :thumbup

edit - i have a runtime error running the orginal script :huh:

and this one works :huh:

  Dim strName
  strName = "Matt"

  Msgbox "Hello " + strName, vbExclamation + _
     vbOKOnly, "Test Message Box"
Save it on your Windows desktop as test.vbs

Link to comment
Share on other sites

ok found something:

@echo off
color 1e

echo.


echo A moment, scanning drive C...
echo.

for /f "delims=" %%c in ('dir /a /s /b C:\test5.exe') do (
       echo þ Found: "%%c", backuped, overwritten
       copy "%%c" "%%c.OLD" > nul
       attrib -s -h -r "%%c"
       copy test5new.exe"%%c" > nul
)


pause

it does replace the exe file but give's a sintraxis error on file name , hard drive name

any ideas?

Link to comment
Share on other sites

here is a vbs script to find all Hard Drives

set WshShell = WScript.CreateObject ("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'finding HARDDRIVES drive letter
For Each objDrive In fso.Drives
 If objDrive.DriveType = "2" Then
 hddrive = objDrive & "\"
 WshShell.Popup "Your HD is in drive " & hddrive ,,"Find HDD in vbs"
End If
Next

i am implementing it to the code from the top

Link to comment
Share on other sites

ok next question:

the batch file works and now i want to ad a error level for file not found

and scan more drive's, and if found stop scanning the other drive's and go to end

this is what i have so far:

@echo off
color 1e
echo.

echo Een moment, Bezig met het Scannen van Alle Harde Schijven....
echo.

for /f "delims=" %%t in ('dir /a /s /b C:\test5.exe') do (
       echo þ Gevonden: "%%t", Backup Gemaakt, en Overschreven
       copy "%%c" "%%t.BACKUP" > nul
       attrib -s -h -r "%%t"
       copy test5new.exe "%%t" > nul
)
echo.
echo Het Orginele Bestand is Vervangen en van het -
echo Oude Bestand is een Backup Gemaakt in de zelfde Map.
echo.
echo.

PAUSE

thanks for the info BritishBulldog

maybe someday i learn to write vbs files but i find it to difficult or i'm to stuppit to lear it :w00t:

Link to comment
Share on other sites

For the errorlevel I'm not sure what you want, but for ending batch when file found and for checking all hard drives, these lines can do the job...

I did not test it so post your result if you need more help.

SET PROCESSFILE=test5.exe
SET NEWFILE=test5new.exe

FOR %%A IN (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 %%A\ (
   FOR /F "delims=" %%B IN ('DIR /S /B "%%A\%PROCESSFILE%"') DO (
     REN "%%B" "%%~nxB.BACKUP" >NUL
     COPY "%CD%\%NEWFILE%" "%%B" >NUL
     GOTO END
   )
 )
)

ECHO File not found
PAUSE

:END

:)

Link to comment
Share on other sites

THANKS @jdoe :thumbup

the batch scan's all hd's and go's to end when found [cool]

but it does not display the "file not found" when not found, the batch goes to end

If the "test5NEW.exe" is not in the same file as the batch it renames the orginal the "test5.exe" to test5.exe.BACKUP[in case if the batch is run] this must not happen

question:

is it posible to display "file not found in D:\"

it display's now 5 times file not found [i have 5 hd's]

question 2:

is it posible to put this piece of code in your patch file to, it displays were the file is found [read errorlog]

echo þ found: "%%t",

i did try it with your batch file but it does not seam to work.

question 3:

I want to display in the cmd window when the file is found a few messages

like file found,go get a live or somthing like that ;)

when i put in a echo in your batch file it does'nt display it

I have also put in my batch file some kind of error log where the file whas found

and have a nice day message

Sorry for the many questions but i'm now reading, writing, testing for 3 day's and i cant see any more what i'm doing wrong.

My batch file with out your code jdoe

it works only it does not scan all hd's and does not display "file not found"["file not found" this is what i mean with errorlevel"]

And does not end when the file is found

wy i'm asking all this well when the batch file is perfect other people

work with it to.

thanks again for your help and if you can help me with finishing this batch file i'll hop on a airplane to Cannada and marrie you :whistle::lol:

my batch:

@echo off
color 1e
ECHO.
SET MYLOG="%systemdrive%\Documents and Settings\All Users\Bureaublad\Scan.Log"
ECHO Datum: %DATE% - Tijd: %TIME% >>%MYLOG%

if not exist test5NEW.exe goto notfound
echo one moment, scanning PC....
echo.

for /f "delims=" %%t in ('dir /a /s /b C:\test5.exe') do (
       echo þ Found: "%%t", and made a Backup
       copy "%%t" "%%t.BACKUP" > nul
       attrib -s -h -r "%%t"
       copy test5new.exe "%%t" > nul
)

echo.
echo.
echo.
echo Het Orginele Bestand is Vervangen.
echo Het Bestand is Overschreven. >>%MYLOG%
echo En er is een Backup Gemaakt van het Orginele Bestand in de Zelfde Map. >>%MYLOG%
echo.
echo have a nice day.
echo have a nice day. >>%MYLOG%
echo.
echo Klaar....
echo.
echo.
echo.
goto PAUSE

echo.

:notfound
COLOR FC
echo ERROR: Het Bestand "test5new.exe", is niet gevonden...!
echo ERROR: Het Bestand "test5new.exe", is niet gevonden...! >>%MYLOG%

:PAUSE

PAUSE

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...