Fine and Replace

November 2, 2010 at 18:06:50
Specs: windows xp or 7
Ok here is what I need my batch file to do.

I need it to search the C drive or even better all drive connected to the computer for a file, call it file.a. When it finds file.a it makes a copy of its shelf in the same directory named file.a.old. Then replaces file.a with file.b which has the same name.

Any help would be great.
Thanks


See More: Fine and Replace

Report •


#1
November 2, 2010 at 20:59:46
Here is the code I had made a while back but it's long and doesnt fully do what I want. First off it doesnt make a backup of the original file and second just searches the Program Files. Im not sure if I really need the cd C:\ part but that is where the new file live and gets copied from so I figured that was a go place to be to have this work.

cd C:\
FOR /F "tokens=*" %%D IN ('DIR "C:\Program Files\file.a" /S /B') DO copy /Y /V /Z "file.a" "%%D"
FOR /F "tokens=*" %%D IN ('DIR "D:\Program Files\file.a" /S /B') DO copy /Y /V /Z "file.a" "%%D"
Repeat all the way to
FOR /F "tokens=*" %%D IN ('DIR "Z:\Program Files\file.a" /S /B') DO copy /Y /V /Z "file.a" "%%D"
exit

So if some one could improve on this code or could write a much simpler code that does everything I need that would be great.

Thanks Again


Report •

#2
November 3, 2010 at 04:27:12
@echo off & setLocal EnableDELAYedeXpansion

for %%i in (c d e f) do (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\filea.old
move /y file.b %%a
)
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
November 3, 2010 at 12:04:27
That is great, a for each loop =) I knew there was a way to do it but I dont know batch coding all that will. But here is a curve ball now they want to run this on batch file on 32 and 64 bit systems, the problem being the files have the same names and it would be really bad if someone copied the 32 bit file onto the 64 bit system. So the original 32 bit and 64 bit files are different sizes. The 32bit original file is 2.69MB or 2,826,240 bytes the 64 bit files size Im unsure of as of now so like just say it 3.0MB 3,000,000 bytes, could we put some kind of if statement that checks the files size and use a goto to then copy the file with the new name and replace it?

So here is the code I have so far

@echo off
echo.
echo This batch script replaces all the instructions.dll file
echo.
:menu
echo 1.Replace all instruction.dll file on 32 bit systems
echo 2.Replace all instruction.dll file on 64 bit systems
echo 3.Undo all system changes made by this batch script
echo 4.clean up and exit
set /p choice=enter your choice 1,2,3 or 4:
if %choice%==1 goto replace 32 bit
if %choice%==2 goto replace 64 bit
if %choice%==3 goto undo
if %choice%==4 goto exit

:replace 32 bit
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\file.a.old
move /y file.b %%a
)
)
goto menu

:replace 64 bit
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\file.a.old
move /y file.b %%a
)
)
goto menu

:undo
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
del /f /q file.a %%a
move /y %%a %%~DPa\file.a.old
)
)
goto menu

:exit
exit

Can you help me add the code that would check for the size? Also if you wouldnt mind check this line in the undo part "del /f /q file.a %%a" and make sure it is correct and fix this line in the undo part cause Im sure it is worng =)
move /y %%a %%~DPa\file.a.old


Report •

Related Solutions

#4
November 3, 2010 at 12:37:55
You got me mystified.


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
November 3, 2010 at 12:46:52
How so?

Report •

#6
November 3, 2010 at 13:33:45
ok let me fully explain what this will do this batch file will be compiled with a few source files and folder into an exe. The exe will unpack a folder onto the C: drive call the folder updater. In side the folder updater are 2 more folder the first folder is 32 and the second is 64. In side those folder is the file that need to replace the file on the target computer. As of now I know the code is not complete. Here is what needs to be added

1.The goto replace 32 bit code which is this
":replace 32 bit
cd C:\updater\32\
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\file.a.old
move /y file.b %%a
)
)
goto menu"
Needs an if statement to check if the original file.a is 2.69MB or 2,826,240 bytes and if that if statement comes back true (it is 2.69MB) then do the
"move /y %%a %%~DPa\file.a.old
move /y file.b %%a"
part if it comes back false (it is not 2.69MB) then just leave the file as is

2. The goto replace 64 bit code which is this
":replace 64 bit
cd C:\updater\64\
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a 2^>nul') do (
move /y %%a %%~DPa\file.a.old
move /y file.b %%a
)
)
goto menu"
Needs an if statement to check if the original file.a is 3MB or 3,000,000 bytes and if that if statement comes back true (it is 3 MB) then do the
"move /y %%a %%~DPa\file.a.old
move /y file.b %%a"
part if it comes back false (it is not 3MB) then just leave the file as is

3. The undo code which is
":undo
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\file.a.old 2^>nul') do (
move /y %%a %%~DPa\file.a
)
)
goto menu"
Need to just search for the file.a.old and rename it to file.a over the old file.

Do you think this can be done?


Report •

#7
November 3, 2010 at 22:03:31
Ok first off thats so much for the help so far this will help me out so so much. I wanted to know if you or anyone for that matter wouldn't mind looking over the rest of my code checking for syntax errors or anything else that does not look right. I see 2 problems as it stands first would be the new files which are going to replace the file.a aka chkhedr.dll are located at C:\updater\32\ and C:\updater\64. These to paths need to be excluded in the loop and if statement cause as of right now the batch script finds them first and renames them to chkhedr.dll.old and then there is no chkhedr.dll in the directories to be copied to the other directories on the computer. Second would be I need help writing the code to undo the changes made. This bit of code would just rename the chkhedr.dll.old back to chkhedr.dll over writing the chkhedr.dll file in the directory putting everything back to the way it was before the batch script ran.

So here is my code.

@echo off
echo.
echo This batch script updates all the chkhedr.dll files on this computer
echo.
:menu
echo 1.Replace all chkhedr.dll file on 32 bit systems
echo 2.Replace all chkhedr.dll file on 64 bit systems
echo 3.Undo all system changes made by this batch script
echo 4.Exit
set /p choice=enter your choice 1 2 3 or 4:
if %choice%==1 goto replace 32 bit
if %choice%==2 goto replace 64 bit
if %choice%==3 goto undo
if %choice%==4 goto exit

:replace 32 bit
cd C:\updater\32\
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\chkhedr.dll 2^>nul') do (
if "%%~za" == "2826240" (
move /y %%a %%~DPa\chkhedr.dll.old
move /y chkhedr.dll %%a
)
)
)
goto menu

:replace 64 bit
cd C:\updater\64\
for %%i 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 (
for /f "tokens=* delims= " %%a in ('dir/b/s/a-d %%i:\chkhedr.dll 2^>nul') do (
if "%%~za" == "3253670" (
move /y %%a %%~DPa\chkhedr.dll.old
move /y chkhedr.dll %%a
)
)
)
goto menu

:undo
??????????????????????????????????????????????????????????
goto menu

:exit
exit

Any Help would be great. And Thank Again Mechanix2Go.


Report •


Ask Question