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
9
  • 1
    I have no idea, what you try to explain, but set a = /r: sets a variable a<space> with a value of <space>/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) Commented Jan 25, 2019 at 8:26
  • Thanks, I'll try that. I was following example here, and it didn't have quotes. I updated my question, with some extra details to try to make it clearer Commented Jan 25, 2019 at 9:01
  • What is the purpose of and in this line start %1 and %a% and %refs% and %1? Commented Jan 25, 2019 at 9:16
  • 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. Commented Jan 25, 2019 at 9:29
  • 2
    additional to to points above: instead of set refs = type ref.txt use <ref.txt set /p refs= (as I understand, the file has just one line) Commented Jan 25, 2019 at 10:27

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
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!
See changed answer, the scs command is only echoed, to execute remove the echo in front.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.