MSFN Forum: script for searching and replace a file - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

script for searching and replace a file Rate Topic: -----

#1 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 27 November 2004 - 01:55 PM

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:


#2 User is offline   Doggie 

  • I'm very inactive :(
  • Group: Patrons
  • Posts: 2,676
  • Joined: 13-October 02

Posted 27 November 2004 - 06:33 PM

Using VBScript and the FileSystemObject to Replace Batch Files
http://windows.oreil...iptpr_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 :)

#3 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 27 November 2004 - 06:52 PM

Doggie, on Nov 28 2004, 01:33 AM, said:

Using VBScript and the FileSystemObject to Replace Batch Files
http://windows.oreil...iptpr_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

#4 User is offline   Doggie 

  • I'm very inactive :(
  • Group: Patrons
  • Posts: 2,676
  • Joined: 13-October 02

Posted 27 November 2004 - 07:00 PM

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 :)

#5 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 27 November 2004 - 07:04 PM

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


#6 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 27 November 2004 - 08:35 PM

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?

#7 User is offline   Doggie 

  • I'm very inactive :(
  • Group: Patrons
  • Posts: 2,676
  • Joined: 13-October 02

Posted 28 November 2004 - 12:00 AM

arent you missing " <= around the drive name and file name?

#8 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 28 November 2004 - 05:24 AM

Doggie, on Nov 28 2004, 07:00 AM, said:

arent you missing  "  <= around the drive name and file name?

i did mis a space in:
copy test5new.exe"%%c" > nul

copy test5new.exe "%%c" > nul

btw works now and backup the old test5.exe to test5.exe.OLD in the same file

#9 User is offline   BritishBulldog 

  • Member
  • PipPip
  • Group: Members
  • Posts: 271
  • Joined: 11-October 04

Posted 28 November 2004 - 05:41 AM

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

Quote



#10 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 28 November 2004 - 10:15 AM

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:

#11 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 29 November 2004 - 03:05 AM

no one knows? :}

#12 User is offline   jdoe 

  • Advanced Member
  • PipPipPip
  • Group: Banned
  • Posts: 314
  • Joined: 02-May 04

Posted 30 November 2004 - 12:15 AM

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



:)

#13 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 30 November 2004 - 03:42 AM

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


#14 User is offline   sixpack 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 515
  • Joined: 29-May 04

Posted 08 December 2004 - 06:01 AM

DONE :w00t:

THANKS TO THIS FORUM
AND:
jdoe

btw this was build with the stuff i found on the internet and here

Attached File(s)



Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy