In perforce I print the list of shelved/changes to the file with the following batch script :
for %%A IN (%ShelvedCHL%) DO (
echo Change List: %%A
p4 -p %PPort% -c %PClient% unshelve -s %%A && if NOT %ERRORLEVEL%==0 exit -1
)>> list.txt
Here is my list.txt
Change List: 24536
//GSA/TOC.h#1 - unshelved, opened for edit
... /GSA/TOC.h - also opened by test@dev
... /GSA/TOC.h - also opened by test@dev
//odata/oenums.h#6 - unshelved, opened for edit
... //odata/oenums.h#6 - also opened by test@dev
I want to have the following output, basically remove sentence after - with dash as well : or even any perforce command to have less information and just list of files :
//GSA/TOC.h#1
... /GSA/TOC.h
... /GSA/TOC.h
//odata/oenums.h#6
... //odata/oenums.h#6
I would appreciate any help, thanks in advance!
At first, let us take a look at your code:
p4
, but I assume it is setting theErrorLevel
. So since this value is updated in the same block of code as you want to read it, you need to use delayed expansion, so placesetlocal EnableDelayedExpansion
on top of your script and use!ErrorLevel!
instead of%ErrorLevel%
. Another way is to replaceif not !ErrorLevel!==0
byif
not ErrorLevel 1
, meaning ifErrorLevel
is not greater than and not equal to1
, or expressed in a simpler way, ifErrorLevel
is less than1
, but this works only if the program does not set a negative value.ErrorLevel
issue, theif
query would never eb executed because of the conditional command concatenation operator%%
, because this lets the following command only execute in case the preceding one succeeded, meaning that its exit code1 equals zero. Therefore to execute theif
statement, use the unconditional operator&
. Anyway, there is also another conditional operator||
, which lets the following command only execute in case the exit code is a non-zero value; this one could replace yourif
condition completely.exit
command does not only quit the batch file, it also terminates the command prompt (cmd
) instance which the batch script ran in. To quit the batch file only useexit /B
instead.ErrorLevel
to-1
byexit -1
. You can do this, of course, but usually negative values are avoided; hence let me suggest a positive value like1
(byexit /B 1
).list.txt
for every single iteration of thefor
loop. This reduces overall performance. Furthermore, iflist.txt
already exists, the data becomes appended; if you do not want that you need to placedel "list.txt" 2> nul
before thefor
loop to initially delete the file. Anyway, to write the entire file at once, put another pair of parentheses around thefor
loop. You can then chose whether to append to an already existing file using the redirection operator>>
, or to overwrite it using operator>
(without any need to delete it first).All this results in the following improved script:
Depending on what
%ShelvedCHL%
contains (it seems to be24536
in your sample data, so not a file path/name/mask), thefor
loop might even be superfluous, although I cannot know at this point...Anyway, all of the above does still not yet account for removal of the partial string beginning with SPACE +
-
+ SPACE, so let us implement this now:To keep it simple, we could just modify the file
list.txt
after the above code, using this code (see all the explanatoryrem
remarks; the mentioned string manipulation is called sub-string substitution):The resulting data is contained in the file
list_NEW.txt
. To have it in the original file, append the following line to the code:1... Usually the exit code and
ErrorLevel
are the same, but there are in fact some rare cases where they may differ.