substitution modifiers in batch file (for loop)

P

Parvinder

All,

I have a file which contains complete path of the files on a seperate
line. What I want to do is execute a command for each of those files
with the path seperator as front slash ("/") instead of backslash
("\"")

Example

inputfile.txt
------------------------
c:\temp\t1\foo.txt
c:\temp\dir1\bar.txt

Following code is my code in the batch file
-----------------------------------------------------------------------
for /F "delims=:" %%X in (%~f1) do echo "CommandFile/%%~pX/%%~nxX" >>
c:\elog.txt

and this is the output
CommandFile/temp\t1\foo.txt
CommandFile/temp\dir1\foo.txt

whereas what i want is

CommandFile/temp/t1/foo.txt
CommandFile/temp/dir1/foo.txt

i.e. change the path seperator from back slash to front slash

Even if i change the input file from backslash to front slash. The
substitution modifiers (~p and ~nx) change it to a back slash

Is there any other substitution modifiers which will the complete path
with a front slash? Can i write my own substitution modifiers?

Thanks for your help
~Parvinder
 
C

Clay Calvert

Example

inputfile.txt
------------------------
c:\temp\t1\foo.txt
c:\temp\dir1\bar.txt

Following code is my code in the batch file
-----------------------------------------------------------------------
for /F "delims=:" %%X in (%~f1) do echo "CommandFile/%%~pX/%%~nxX" >>
c:\elog.txt

and this is the output
CommandFile/temp\t1\foo.txt
CommandFile/temp\dir1\foo.txt

whereas what i want is

CommandFile/temp/t1/foo.txt
CommandFile/temp/dir1/foo.txt

i.e. change the path seperator from back slash to front slash

Even if i change the input file from backslash to front slash. The
substitution modifiers (~p and ~nx) change it to a back slash

Is there any other substitution modifiers which will the complete path
with a front slash? Can i write my own substitution modifiers?

If always two subdirectories then this should work.,

for /F "tokens=2-4 delims=:\" %%a in (%~sf1) do echo
"CommandFile/%%a/%%b/%%c"
Clay Calvert
(e-mail address removed)
Replace "Z" with "L"
 
F

foxidrive

If always two subdirectories then this should work.,

for /F "tokens=2-4 delims=:\" %%a in (%~sf1) do echo "CommandFile/%%a/%%b/%%c"

Here's another (untested) method:

@echo off
for /F "delims=" %%a in(%~sf1) do call :next "%%a"
goto :EOF
:next
set var=%~1
set var=%var:\=/%
echo/%var%
 
P

Parvinder

Thanks for your reply

The number of sub directories is not known. It can be even more than 2
so that means the number of tokens is not known.

~Parvinder
 
T

Todd Vargo

Parvinder said:
All,

I have a file which contains complete path of the files on a seperate
line. What I want to do is execute a command for each of those files
with the path seperator as front slash ("/") instead of backslash
("\"")

Example

inputfile.txt
------------------------
c:\temp\t1\foo.txt
c:\temp\dir1\bar.txt

Following code is my code in the batch file
-----------------------------------------------------------------------
for /F "delims=:" %%X in (%~f1) do echo "CommandFile/%%~pX/%%~nxX" >>
c:\elog.txt

and this is the output
CommandFile/temp\t1\foo.txt
CommandFile/temp\dir1\foo.txt

whereas what i want is

CommandFile/temp/t1/foo.txt
CommandFile/temp/dir1/foo.txt

i.e. change the path seperator from back slash to front slash

If your input file contains multiple path depths, you may find this approach
useful. All lines begin with 2 spaces.


for /F "tokens=2 delims=:" %%X in (%~f1) do call :reslash "%%X"
goto :eof

:reslash
set FN=%~1
echo "CommandFile%FN:\=/%" >> c:\elog.txt
 
P

Parvinder

Awesome Guys
Thanks a bunch Todd and Foxi
This worked !
Someone sometime back had said that batch is really powerful today i
belive it

Basically i never found any documentation in the %variable% syntax for
the replacement character like the following
%FN:\=/%"
Can someone point me for more documentation on this ?

Here is the complete script which i wanted
****************************************************************
@echo off
for /F "tokens=* delims=:" %%X in (%~f1) do call :reslash "%%~pX%%~nxX"
goto :eof

:reslash
set FN=%~1
echo curl -iT "%FN%"
"http://X.X.X.X/fcfs_data/users/myuser/%FN:\=/%"
****************************************************************
 
B

billious

Parvinder said:
Awesome Guys
Thanks a bunch Todd and Foxi
This worked !
Someone sometime back had said that batch is really powerful today i
belive it

Basically i never found any documentation in the %variable% syntax for
the replacement character like the following
%FN:\=/%"
Can someone point me for more documentation on this ?

From the prompt:

SET /?

or generally,

commandname /?

for documentation, but the environment substitution syntax occurs directly
after the /P option documentation for the SET command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top