Page 1 of 1

Remove leading characters and pass to new variable

Posted: 20 Jan 2011 14:36
by KipKasper
Hi All,

I have a log listing fully qualified file names that need to be copied.

ie.
\\some.server.domain.ca\c$\storage\folder\with\stuff\init.jpg
\\some.server.domain.ca\c$\storage\folder\with\stuff\more.jpg
\\some.server.domain.ca\c$\storage\folder\with\stuff\again.tif

the bit of code I'm fighting with is this

setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in (!dest2!output.log) do (
set filenames=%%a
set spath=!filenames:~73,0!
xcopy !filenames! !dest2!!spath! /f /h
))


it should, take the fully qualified names and load them into !filenames! to be the source for xcopy, and also remove 73 characters from the front of each line, so xcopy can create the same directory structure in the destination. (those 73 characters are \\some.server..... which i don't need. )

I can't tell if anything is being loaded into !spath! the echo output just shows !spath! rather than the data (if any) in it.

- Kip

Re: Remove leading characters and pass to new variable

Posted: 21 Jan 2011 08:04
by ChickenSoup
You can remove the "\\some.server..." with a better use of for and not be bound to a certain number of characters like below.

Code: Select all

for /f "tokens=1* delims=\" %%a in (output.log) do echo %%b

will give you:
c$\storage\folder\with\stuff\init.jpg
c$\storage\folder\with\stuff\more.jpg
c$\storage\folder\with\stuff\again.tif

--or--

Code: Select all

for /f "tokens=2* delims=\" %%a in (output.log) do echo %%b

will give you:
storage\folder\with\stuff\init.jpg
storage\folder\with\stuff\more.jpg
storage\folder\with\stuff\again.tif


You should be able to use the value of %%b to get your desired results in the xcopy and then drop the SET statements.

Re: Remove leading characters and pass to new variable

Posted: 21 Jan 2011 09:50
by avery_larry
untested:

This part:
!filenames:~73,0!
will return 0 characters skipping the first 72 characters. I believe you want:
!filenames:~73!

Which just skips the first 72 characters and returns everything else.

Re: Remove leading characters and pass to new variable

Posted: 21 Jan 2011 10:11
by KipKasper
avery_larry wrote:untested:

This part:
!filenames:~73,0!
will return 0 characters skipping the first 72 characters. I believe you want:
!filenames:~73!

Which just skips the first 72 characters and returns everything else.


Thanks avery_larry!

I had just looked that function up, I've never used it before, so I'm not surprised I was using it wrong =)

- Kip

Re: Remove leading characters and pass to new variable

Posted: 21 Jan 2011 10:13
by KipKasper
ChickenSoup wrote:You can remove the "\\some.server..." with a better use of for and not be bound to a certain number of characters like below.

Code: Select all

for /f "tokens=1* delims=\" %%a in (output.log) do echo %%b

will give you:
c$\storage\folder\with\stuff\init.jpg
c$\storage\folder\with\stuff\more.jpg
c$\storage\folder\with\stuff\again.tif

--or--

Code: Select all

for /f "tokens=2* delims=\" %%a in (output.log) do echo %%b

will give you:
storage\folder\with\stuff\init.jpg
storage\folder\with\stuff\more.jpg
storage\folder\with\stuff\again.tif


You should be able to use the value of %%b to get your desired results in the xcopy and then drop the SET statements.


Your right Chickensoup that would be a better way to go about it, and as avery_larry pointed out I was using :~72,-0 wrong anyway... thanks!