Remove leading characters and pass to new variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KipKasper
Posts: 3
Joined: 20 Jan 2011 14:06

Remove leading characters and pass to new variable

#1 Post by KipKasper » 20 Jan 2011 14:36

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

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Remove leading characters and pass to new variable

#2 Post by ChickenSoup » 21 Jan 2011 08:04

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Remove leading characters and pass to new variable

#3 Post by avery_larry » 21 Jan 2011 09:50

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.

KipKasper
Posts: 3
Joined: 20 Jan 2011 14:06

Re: Remove leading characters and pass to new variable

#4 Post by KipKasper » 21 Jan 2011 10:11

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

KipKasper
Posts: 3
Joined: 20 Jan 2011 14:06

Re: Remove leading characters and pass to new variable

#5 Post by KipKasper » 21 Jan 2011 10:13

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!

Post Reply