0

I have a batch file that will run csc using a file as input. I want to modify it to read references from a file, and add them to the line that is executed when the script runs.

I've tried a few different things but can't seem to get it work. The references are added with /r: and then each reference path has semi-colon as a separator.

Ideally, I'd like to just have a reference on a new line in the text file. The ref.txt file is in the same directory as the input file, and I'm not sure if it was looking in this directory or not. I also want to make it attempt to run without the ref.txt file, so I added the exists line to do this. I've never used batch scripting before, so maybe someone else knows how to do this better than me. I think that the first line needs to match the start line, which I tried to do in other attempts, but it wasn't working.

The script works in Notepad++, and was from this answer. I think now that the run command also needs to be modified.

This is the run command in Notepad++:

C:\bin\csc.bat "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" "$(FULL_CURRENT_PATH)"

This is the version from that answer:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /out:%1 %2

@echo off

if errorlevel 1 (
    pause
    exit
)

start %1 %1

This is an attempt to use references:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /out:%1 %2

@echo off

if errorlevel 1 (
    pause
    exit
)

if not exist ref.txt GOTO :write
set a = /r:
set refs = type ref.txt
start %1 %a% and %refs% and %1
exit
write
start %1 %1

The refs.txt file contains file paths like this:

C:\windows\some_path\some_file.dll;C:\windows\some_path\another_file.dll;

An example command from Microsoft is:

csc /t:exe /r:MyCodeLibrary.dll;NewLib.dll *.cs
double-beep
  • 5,031
  • 17
  • 33
  • 41
Matts
  • 1,301
  • 11
  • 30
  • 1
    I have no idea, what you try to explain, but `set a = /r:` sets a variable `a` with a value of `/r:`, what surely isn't what you want. Remove the spaces around `=` or even better use best practice `set "a=/r:"` (note the position of the quotes) – Stephan Jan 25 '19 at 08:26
  • Thanks, I'll try that. I was following example [here](https://www.tutorialspoint.com/batch_script/batch_script_create_string.html), and it didn't have quotes. I updated my question, with some extra details to try to make it clearer – Matts Jan 25 '19 at 09:01
  • What is the purpose of `and` in this line `start %1 and %a% and %refs% and %1`? – montonero Jan 25 '19 at 09:16
  • string concatenation. It probably wasn't the best way to do it. – Matts Jan 25 '19 at 09:22
  • The intention was to combine the last 3 variables into 1, and replace the last %1, but to do this the first "and" shouldn't be there, which was an error. – Matts Jan 25 '19 at 09:29
  • 1
    Same questions as others... Plus change `set refs = type ref.txt` to `set "refs=type ref.txt"`. Change `write` label to `:write`. You probably meant for `start %1 and %a% and %refs% and %1` to be `start %1 %a% & %refs% & %1`. You might have meant for `start %1 %1` to be `start %1 %2` ??? Be aware that as you have it written you will have a problem if %1 or %2 contain spaces. – RGuggisberg Jan 25 '19 at 09:29
  • Thanks @RGuggisberg I added some more info to the question. I think the run command is the first problem I need to fix, and then I can start trying to get the variables right. – Matts Jan 25 '19 at 10:24
  • 2
    additional to to points above: instead of `set refs = type ref.txt` use ` – Stephan Jan 25 '19 at 10:27
  • @Stephan it did while I was testing it, but I want to make it multiline, as there are a lot of references to add. – Matts Jan 25 '19 at 12:09

1 Answers1

1

IIUR you are trying to apply the refs to the compiled exe not to csc itself.

You need to adapt the path to the ref.txt file

:: Q:\Test\2019\01\25\SO_54360791.cmd
@echo off & Setlocal EnableDelayedExpansion
Set CSC="C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
Set Ref=".\ref.txt"

if exist %Ref% (
    <%Ref% Set /p "refs="
    set "refs=/r:!refs!"
) else set "refs="

%CSC% %refs% /out:%1 %2
if errorlevel 1 (
    pause
    exit
)

sample (echoed) output

> SO_54360791.cmd new.exe source.cs
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /r:C:\windows\some_path\some_file.dll;C:\windows\some_path\another_file.dll; /out:new.exe source.cs

I'm not sure if the trailing semicolon in your sample ref.txt will work.

EDIT: Variant with ref.txt file containing quoted pathes with trailing semiclon

:: Q:\Test\2019\01\25\SO_54360791.cmd
@echo off & Setlocal EnableDelayedExpansion
Set CSC="C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
Set Ref=".\ref.txt"
Set "refs="
if not exist %Ref% goto :cont
set "refs=/r:"
for /f "usebackq delims=" %%A in (%Ref%) Do set "refs=!refs!%%A"

:cont
echo %CSC% %refs% /out:%1 %2
if errorlevel 1 (
    pause
    exit
)
goto :Eof

sample (echoed) output

> SO_54360791.cmd new.exe source.cs
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /r:"C:\windows\some_path\some_file.dll";"C:\windows\some_path\another_file.dll"; /out:new.exe source.cs
  • Thanks, this is almost working for me. There wasn't a trailing semi colon as there were more than 2 refs, so I shouldn't have put that in. The filepath to ref.txt, I had to hard code, as it's not where the batch script resides, it's where the C# code is. The ref paths have spaces in them, so I had to wrap them in quotes. Is it possible to have each path on a new line, and have the script wrap the quotes and semi-colons? I had a go at doing it from this [answer](https://stackoverflow.com/questions/47335326/how-to-remove-new-line-character-from-batch-variable) – Matts Jan 25 '19 at 11:51
  • I think for csc to accept the refs it has to be a single line, but that could be assembled from several lines in the ref.txt file, I'm unsure if the whole refs have to be double quoted or the single ones. Edit the question to contain a sample of the new ref.txt. If you supply the path to the .cs with `%2` you can extract the path with `%~dp2` and so refernece the same folder. –  Jan 25 '19 at 12:28
  • Each ref is quoted with semi colon after the quoted path like this. `"C:\windows\some_path\some_file.dll";` except the last ref which has no semi-colon. Thanks `%~dp2` is working! – Matts Jan 25 '19 at 12:39
  • See changed answer, the scs command is only echoed, to execute remove the echo in front. –  Jan 25 '19 at 12:52