find, rename and copy original from cd

December 25, 2009 at 03:50:18
Specs: all windows
hello, i am trying to create a batch script that will search the hard drives for a certain file, rename it with the .bak extension and then copy a original from cd to the locations. here is a example of where i am at.

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/s/b/a-d c:\xxx.dll') do (
if errorlevel 1 goto :eof
echo ren %%a xxx.dll.bak
ren %%a xxx.dll.bak
)


See More: find, rename and copy original from cd

Report •


#1
December 25, 2009 at 04:07:59
Obviously, you need to parse the pathname somewhere, since %%a would contain C:\windows\system\xxx.dll
while the name on the CD may be something like:

X:\some\path\xxx.dll

In a "FOR /F" statement you can easily strip the filename off:

%~nI - expands %I to a file name only

this would be xxx.dll

And from there on ... just some adding toghether of variables:

set cddrive=X
set cdbasepath=\some\path\

Something like that ?


PS, for the display of the command, you can also do:
@echo on
ren %%a xxx.dll.bak
@echo off

This woud need to prevent having to type a similar line, and particularly when you have more commands (here only 1 rename, but suppose you have more) it's easier this way.


Report •

#2
December 25, 2009 at 04:25:40
ok, on my computer my cd drive is d:
the file is located at d:/original/xxx.dll
the xxx.dll is scattered all over my hard drive (for testing)
would i change the script to look like below?

@echo off
setLocal EnableDelayedExpansion

set cddrive=d
set cdbasepath=\original\xxx.dll

for /f "tokens=* delims= " %%a in ('dir/s/b/a-d c:\xxx.dll') do (
if errorlevel 1 goto :eof

@echo on
ren %%a xxx.dll.bak
copy cdbasepath %%a
@echo off
)

is that correct? please forgive me as i am learning as i go. i know alittle about batch but not alot.

thanks for the help


Report •

#3
December 25, 2009 at 05:12:20
This works, but It'll replace ALL occurances of the DLL with that name, without any exception. Not sure you want that. I've put an ECHO before each of those commands, because what you are doing is quite dangerous. Note it will ask to overwrite files here and there, once you remove the ECHO. I take no responsibility for any DLL's being messed up here ;)

@echo off

set originalfile=whatever.dll
set cddrive=D
set cdbasepath=\original\

for /f "tokens=* delims= " %%a in ('dir /S /B /A-D c:\%originalfile%') do (
if errorlevel 1 goto :eof

@echo on
echo rename %%a %%a.bak
echo copy %cddrive%%cdbasepath%%originalfile% %%a
@echo off
)


Report •

Related Solutions

#4
December 25, 2009 at 05:53:36
ok, now my next question. how can i output the success/failure to a log file?

Sorry for the noob question. i've been up for 24 hours straight getting a friends computer ready for christmas and wrapping a ton of presents and working on this stuff.


Report •

#5
December 25, 2009 at 06:16:22
There's many ways to do this, and it depends a bit on what you exactly want, but try this:


@echo off

set originalfile=whatever.dll
set cddrive=D
set cdbasepath=\original\
set logfile=C:\main.log

for /f "tokens=* delims= " %%a in ('dir /S /B /A-D c:\%originalfile%') do (
if errorlevel 1 goto :eof

date /t >> %logfile%
time /t >> %logfile%
echo Handling file %%a>> %logfile%
@echo on
echo rename %%a %%a.bak
echo copy %cddrive%%cdbasepath%%originalfile% %%a
@echo off
)


Report •

#6
December 25, 2009 at 06:16:29
also wanted to let you know that when i remove the echo from the beginning of the code, it doesn't work.

Report •

#7
December 26, 2009 at 08:15:08
Explain "it doesn't work"


Report •

#8
December 26, 2009 at 08:32:45
it will go through the actions of listing the files, but doesn't back them up nor copies the original over.

Report •

#9
December 26, 2009 at 12:42:50
is this what you tried? (changing this:)

time /t >> %logfile%
echo Handling file %%a>> %logfile%
@echo on
echo rename %%a %%a.bak
echo copy %cddrive%%cdbasepath%%originalfile% %%a
@echo off
------------------
to this?

time /t >> %logfile%
echo Handling file %%a>> %logfile%
@echo on
rename %%a %%a.bak
copy %cddrive%%cdbasepath%%originalfile% %%a
@echo off


Report •

#10
December 26, 2009 at 13:02:00
this is what my script looks like and is exactly what i use.

@echo off

set originalfile=xxx.dll
set cddrive=d
set cdbasepath=\original\
set logfile=C:\CS4log.txt

for /f "tokens=* delims= " %%a in ('dir /S /B /A-D c:\%originalfile%') do (
if errorlevel 1 goto :eof

date /t >> %logfile%
time /t >> %logfile%
echo Handling file %%a>> %logfile%
@echo on
rename %%a %%a.bak
copy %cddrive%%cdbasepath%%originalfile% %%a
@echo off
)


when i run the script, it will show the files and where they are located and looks like it is working, but doesn't create the backup of the file and doesn't copy the original file to the folder.


Report •

#11
December 26, 2009 at 13:49:43
although i doubt these are source of failure, i would recommend these alterations first:
take the space out of this: "tokens=* delims= "
make it: "tokens=* delims="
put quotes around %%a in every line after the "for" statement
change "rename" to "move /y"
and remove the extension from %%a before appending ".bak" (see next lines)

another way to do additional checking, change this:

rename %%a %%a.bak
copy %cddrive%%cdbasepath%%originalfile% %%a

to create a temp batch file, like this:
echo move /y "%%a" "%%~dpna.bak" >> temp.bat
echo copy %cddrive%%cdbasepath%%originalfile% "%%a" >> temp.bat

------------- modified script (bottom part only)
del temp.bat 2>nul
for /f "tokens=* delims=" %%a in ('dir /S /B /A-D c:\%originalfile%') do (
if errorlevel 1 goto :eof

date /t >> %logfile%
time /t >> %logfile%
echo Handling file "%%a">> %logfile%
@echo on
echo move /y "%%a" "%%~dpna.bak" >> temp.bat
echo copy "%cddrive%%cdbasepath%%originalfile%" "%%a" >> temp.bat
@echo off
)
more temp.bat
echo does this look ok? ctrl-c if not:
pause >nul
call temp.bat
del temp.bat
:://* end of batchscript
like is said, this prob'ly won't fix the issue, but it fixes some potential problems and you can see what the output looks like (ie: the contents of "temp.bat") prior to executing.


Report •

#12
December 26, 2009 at 14:02:30
that does seem to work better, although it did not copy the original file back over. the only file that i have is the .bak file. i am not sure if it is the original or the copy. as both the test and the original are the same (just a copy of the original) for testing. let me try it with 2 different ones to test it.

nope it doesn't copy the original file over.


Report •

#13
December 26, 2009 at 14:20:02
i think i got it. it was an error on my part. i forgot to add the : after the f for the drive letter....lol.

Report •

#14
December 26, 2009 at 14:25:00
nice "catch"! its the little goblins that getcha most times.
i didn't see it...

Report •

#15
December 27, 2009 at 04:51:28
Hmmyeah, I may have done tests only with no-whitespace stuff. Still, you have an ECHO ON section, just showing the commands in question ... when you say either the MOVE or the COPY does not work ... how hard is it, to look at both the command, and the error message and either:

- figure out yourself it is not working
- showing US that part ... you got it on the screen, what more do you want ?


Report •


Ask Question