Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Inspired while writing my other question (which i now deleted) (sometimes talking to the duck does help) I think I found a solution to my problem but I don't know how to do it.

I have the following command in a batch script

FOR /R %1 %%G IN (*) DO rdiff.exe signature "%%G" | 7za.exe a -mx0 -si"%%G" signatures.7z

What I would like to do is remove the text of %1 from %%G in the 7zip -si argument but leave it in the rdiff argument.

How would I go about doing that?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

String manipulation is very awkward in the command prompt. Removing the contents of %1 from the beginning of %G would involve counting the characters of %1 (loop through the characters and increase a counter by one) and then use the substring syntax (%var:~start,length%).

Using forfiles should be much simpler, since it has a built-in command variable to access the relative path of the file (@relpath).

This should work:

forfiles /S /P %1 /C "cmd /V:on /C if @isdir==FALSE set R=@relpath&set R=!R:~3,-1!&%~dp0rdiff signature @path | %~dp07za a -mx0 -si\"!R!\" %~dp0signatures.7z"

Details:

  • /S makes forfiles recursive.

  • /P %1 specifies the path.

  • /C "cmd /V:on /C ... executes ... in a subshell with delayed variable expansion (!var! syntax).

  • if @isdir==FALSE checks for the presence of a file (rather than a directory).

  • set R=@relpath stores the relative path in a variable called R.

  • set R=!R:~3,-1! removes the string at the beginning "\." and the trailing double-quote.

  • %~dp0 forfiles changes directories to execute the command, specifying %~dp0 gives you the absolute path the batch file was run from.

share|improve this answer
    
That looks good, but its no longer seeing rdiff.exe in the same folder as the batch file. if I do E:\rdiff>makeSig.bat E:\example it gives me the error message 'rdiff.exe' is not recognized as an internal or external command, operable program or batch file. –  Scott Chamberlain Mar 15 '13 at 19:15
    
Forfiles seems to change directories to execute the command. That means the executables, the signatures file and the archive will require full paths. –  Dennis Mar 15 '13 at 19:18
    
By the way, all the variable juggling doesn't seem to be necessary. Just using -si\@relpath should work as well. –  Dennis Mar 15 '13 at 19:20
1  
replacing the file-names of the programs to %~dp0rdiff.exe and %~dp07za solved it. I also needed to add to the name of the zip to make %~dp0signatures.7z –  Scott Chamberlain Mar 15 '13 at 19:22
    
Also if I just use -si\@relpath I get blank directory and a directory named . in the zip file. but the string replacement works great. –  Scott Chamberlain Mar 15 '13 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.