Replace file in different directories

  • Thread starter Thread starter Mark Coutinho
  • Start date Start date
M

Mark Coutinho

Hello,

I'm writing a batch in which I have to replace a file (actually it's a
link, let's call it test.lnk) which can be situated in different
directories on my D:-drive.

The code must sound something like this: if test.lnk is somewhere than
replace it with test.lnk (which is situated on the server).
So if there's only one test.lnk this replace action is only valid for
one place.
If there are two test.lnk's this replace action is valid for two
places.

Can anyone help me on the right code for this?

Thanks in advance!

Mark
 
This will replace all instances on the D drive of test.lnk with test.lnk
from the server:

@echo off & setlocal enableextensions
set dircmd=
set srcfile=\\server\share\test.lnk
for /f "tokens=*" %%c in ('dir d:\test.lnk /b /s /a-d') do (
copy "%srcfile%" "%%c" /Y
)
 
Hello,

I'm writing a batch in which I have to replace a file (actually it's a
link, let's call it test.lnk) which can be situated in different
directories on my D:-drive.

The code must sound something like this: if test.lnk is somewhere than
replace it with test.lnk (which is situated on the server).
So if there's only one test.lnk this replace action is only valid for
one place.
If there are two test.lnk's this replace action is valid for two
places.

Can anyone help me on the right code for this?

Thanks in advance!

Mark

Use the REPLACE command!

replace \\server\share\test.lnk C:\ /s /u

Garry
 
Paul, you're great!
Thank you.

Mark

Paul R. Sadowski said:
This will replace all instances on the D drive of test.lnk with test.lnk
from the server:

@echo off & setlocal enableextensions
set dircmd=
set srcfile=\\server\share\test.lnk
for /f "tokens=*" %%c in ('dir d:\test.lnk /b /s /a-d') do (
copy "%srcfile%" "%%c" /Y
)
 
Although a test with test.lnk did work I can't get it done with the
actual file I have to replace everywhere. It's called I&O mail voor
Windows.lnk
This is how I put it:

@echo off & setlocal enableextensions
set dircmd=
set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"
for /f "tokens=*" %%c in ('dir "d:\I&O mail voor Windows.lnk" /b /s
/a-d') do (
copy "%srcfile%" "%%c" /Y
)

It has to do with the &-sign and with the spaces, but how would the
right notation be for me to get it done?

Thanks again!
 
Mark Coutinho said:
set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"

Remove the quotation maks around the set command above. They are added by
the copy command below.
copy "%srcfile%" "%%c" /Y

That should make it work.
 
Mark Coutinho said:
Although a test with test.lnk did work I can't get it done with the
actual file I have to replace everywhere. It's called I&O mail voor
Windows.lnk
This is how I put it:

OK, sorry. Ignore my other reply. This should work. Leave the srcfile double
quoted but I removed the quotations around the %srcfile% variable in the
copy command.

It should work fine now.

@echo off & setlocal enableextensions
set dircmd=
set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"
for /f "tokens=*" %%c in ('dir "d:\I&O mail voor Windows.lnk" /b /s /a-d')
do (
copy %srcfile% "%%c" /Y
)
 
Hey, Paul,

it's driving me mad, but your suggestion doesn't work either. In fact:
it's become even a bit worse: if I use a file called 'test.lnk' it
doesn't work either, while that one did work in the version you
suggested in the first place.

I just copied and pasted your last suggestion like this:

@echo off & setlocal enableextensions
set dircmd=
set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"
for /f "tokens=*" %%c in ('dir "d:\I&O mail voor Windows.lnk" /b /s
/a-d')
do (
copy %srcfile% "%%c" /Y
)

Do you see where things go wrong? Has it something to do with leading
spaces? Or on which line the do ( command and the two lines after
should be?
I keep hoping for your solution - that saves me a walk to 90 p.c.'s
here in the office...

Thanks again.
 
Mark Coutinho wrote:
Hi Mark.
@echo off & setlocal enableextensions
set dircmd=
set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"
for /f "tokens=*" %%c in ('dir "d:\I&O mail voor Windows.lnk" /b /s
/a-d')
do (
copy %srcfile% "%%c" /Y
)
Seems there are line wrappings by the news reader. This might work:
(You may advance to a new line just after an opening parenthes or put
a tilde as the last char of a line to denote the line continues.)

@echo off & setlocal enableextensions
set dircmd=
set "file=I&O mail voor Windows.lnk"
set "src=\\kantoor\netlogon\%file%"
for /f "tokens=*" %%c in ('dir "d:\%file%" /b /s /a-d') do (
copy "%src%" "%%c" /Y
)

NOT TESTED

HTH
 
1@echo off & setlocal enableextensions
2set dircmd=
3set srcfile="\\kantoor\netlogon\I&O mail voor Windows.lnk"
4for /f "tokens=*" %%c in ('dir "d:\I&O mail voor Windows.lnk" /b /s /a-d')
do (
4 copy %srcfile% "%%c" /Y
6)

for through do ( must be on a single line.
Do you see where things go wrong? Has it something to do with leading
spaces? Or on which line the do ( command and the two lines after
should be?
I keep hoping for your solution - that saves me a walk to 90 p.c.'s
here in the office...

There should be 6 lines. Copy and remove thenumber at the beginning of each
line.
 
Paul, got it!

Humbly and with lots of apologies I have to admit that somewhere in
the trial-versions I renamed the file to I&Omailvoorwindows.lnk and
forgot about that.
So no wonder your batch didn't work.
Sorry for the trouble but again...
THANKS!
 
Back
Top