0

I am fairly new to writing batch scripts. I am trying to write a script file to achieve the following task.

I have a source safe command below to get the list of files changed between two dates

ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R  

The output of the above command is as follows

Building list for $/myproject.......................................................  
....................................................................................  
...................................  

***** AllPages.master  *****  
Version 67  
User: user1              Date: 1/12/12   Time: 1:08p  
Checked in $/myproject/websites/website1
Comment:  

***** AdminTSSetup.aspx.vb *****
Version 10
User: user2              Date: 1/12/12    Time: 1:09 p
Checked in $/myproject/websites/website1
Comment: 

With the output above, I want to read the file name (allpages.master, AdminTsSetup.aspx.vb) and version of each file from the output and run the following command

SS diff -DS <filename from output> -V<version from output - 1>~<version from output>

Basically, I was trying to compare the previous version with the current version for each file in the output.

Can some one please help?

Sridhar
  • 8,874
  • 4
  • 26
  • 37
  • Coudl you include sample output? Not everyone who might be able to help has seen SourceSafe output. – Joey Jan 13 '12 at 15:45

2 Answers2

3

This should give you what you asked for. But it seems like the diff command you specified is missing a reference to the project.

This solution should work even if the file name contains spaces, !, ;, or any special characters like & or ^.

@echo off
setlocal disableDelayedExpansion
for /f "delims=*" %%A in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R ^| findstr /b "***** Version"') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  if "!ln:~0,1!"==" " (
    for /f "eol=: delims=" %%F in ("!ln:~1!") do (
      endlocal
      set "file=%%~nxF"
    )
  ) else (
    for /f "tokens=2 delims= " %%V in ("!ln!") do (
      set /a "version1=%%V, version0=version1-1"
      ss.exe dif -DS "!file!" -V!version0!~!version1!
      endlocal
    )
  )
)

Here is a version that adds the project information from the "Checked in" line

@echo off
setlocal disableDelayedExpansion
for /f "delims=*" %%A in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R ^| findstr /b "***** Version Checked"') do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  if "!ln:~0,1!"==" " (
    for /f "eol=: delims=" %%F in ("!ln:~1!") do (
      endlocal
      set "file=%%~nxF"
    )
  ) else if "!ln:~0,1!"=="V" (
    for /f "tokens=2 delims= " %%V in ("!ln!") do (
      endlocal
      set /a "version1=%%V, version0=version1-1"
    )
  ) else (
    for /f "tokens=2* delims= " %%B in ("!ln!") do (
      endlocal
      set "proj=%%C"
      setlocal enableDelayedExpansion
      ss.exe dif -DS "!proj!/!file!" -V!version0!~!version1!
      endlocal
    )
  )
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • @Sridhar - Ummm, looks like what? Edit your post and add the sample output from your history command there. – dbenham Jan 13 '12 at 17:46
  • The output would look like "Building list for $/myproject............................ ***** filename ***** version 67 – Sridhar Jan 13 '12 at 17:50
  • it is really hard to enter the output here. Can I send it to you some other way? – Sridhar Jan 13 '12 at 17:51
  • @Sridhar - I meant you should edit your original question. You should be able to enter some output there. Enter it the same way you would enter code. – dbenham Jan 13 '12 at 17:55
  • Hi dbenham, before running the for loop, I need to run the following command "set ssdir=\\mysourcesafelocation" so that it sets the sourcesafe command. How would I do this? Also, I need to add a constant filepath to the filename. how would I do that? thanks – Sridhar Jan 13 '12 at 21:53
  • ok, I got it working kind of. The filename variable has a space in the front. How would I trim that? – Sridhar Jan 13 '12 at 22:06
  • @Sridhar - !VAR:~1! will trim the 1st character off the value in variable VAR – dbenham Jan 13 '12 at 22:12
  • Thank you dbenham. I am almost there. I need the value after "Checked in" in the output. That value + filename will give me the complete file path. – Sridhar Jan 13 '12 at 22:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6709/discussion-between-sridhar-and-dbenham) – Sridhar Jan 13 '12 at 22:37
0

Basically you need a for loop and a little state:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims= " %%i in ('ss.exe history $/myproject -Vd04/01/2012~01/01/2012 -R') do (
  if "%%i"=="*****" (
    rem a file name
    set "FileName=%%j"
  ) else (
    if "%%i"=="Version" (
      set Version=%%j
      set /a LastVersion=Version - 1
      ss diff -DS "!FileName!" -V!LastVersion!~!Version!
      set FileName=&set Version=&setLastVersion=
    )
  )
)

Should kinda work, I guess.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • it is giving the error "tokens=1,2 delims= " was unexpected at this time – Sridhar Jan 13 '12 at 21:32
  • This solution will not work if file name contains or !. Probably not a concern for most development environments. But you never know. – dbenham Jan 13 '12 at 21:38