1

While I started learning C programming language, I hated the tedious process of compiling c files using the command line using gcc, I know that there are better other programs that can solve this issue, but I thought it would be pretty challenging to use batch files to do this process since it as well works in the command prompt, the first problem I found myself unable to solve is to open the c file using the batch file in sort of an input, like when you open text files using a text editor other than notepad, it would be awesome to do that for c files.

I know that in this situation we can make the batch open the command window and wait for the input of the path of the c file, since we can drag the c file from the file explorer window the command window and it's gonna write the path of the file in the command line, but I had rather use the first method since it's easier and it can help in other situations where the click and drag method might not help, especially when wanting to open files with something other than a batch file, for example, a python file.

  • Possible duplicate of [How can I pass arguments to a batch file?](https://stackoverflow.com/questions/26551/how-can-i-pass-arguments-to-a-batch-file) – SLaks Jul 09 '19 at 18:47
  • You want to change the file association for `.C` files to be a call to a script? – Ben Personick Jul 09 '19 at 18:59
  • I find that inferred duplicate a complete mess when I think all he is asking is that in a batch file, your passed commands are %1 %2 ... %8 %9. Also, just like C, argv[0] represents your app while in batch it is %0. – Señor CMasMas Jul 09 '19 at 19:05
  • @BenPersonick yes – Yassine Safraoui Jul 09 '19 at 22:20
  • @YassineSafraoui, Good, then you'll be happy to find, I've posted an Answer that Allows you to associate the Files with a CMD Script, and an n Example CMD Script I used on my System to Generate some outputs of the results once it is associated – Ben Personick Jul 09 '19 at 22:40

1 Answers1

1

I believe you want to Associate .C Files with a batch script.

EG: So that if you double-click the .c file, or call the .c file on the command line, it will be passed as an argument to the CMD Script which you will have GCC run in.

This is possible, both through GUI and through CMD.

Here is an example of how you would accomplish this:

Optional: Backup old association for .c files to be able to restore it later if you want:

REM Backup old association for .c files to be able to restore it later if you want:

Assoc .c >>"%temp%\BKP_Assocs.txt"

Create Custom FileType that points to your CMD Script. -- This links the Filetype to the CMD Script, you will need to provide the full path with quotes for the cmd script:

FType c.cmd="C:\Path\To\YourScript.cmd"

Associate the .c file extension with the custom file type created in FType, allowing it to open in the CMD Script:

Assoc .c=c.cmd

Once you have run the above, you may Double-click on any .c file say "C:\Somefile.c" and it will have it's address passed to the .CMD Script you specified eg C:\Path\To\YourScript.cmd

You can also type "C:\Somefile.c" on the CLI and get the file to open in the C:\Path\To\YourScript.cmd script as a path passed as an argument.

Below is an example script to allow me to show you it works in the Example output following this script, it will spit out the info provided when you double-click a .c file of your choice for testing:

@ECHO OFF
ECHO(%~n0:
ECHO(%~n0: =====================
ECHO(%~n0: Begin Script "%~f0"
ECHO(%~n0: =====================
ECHO(%~n0:
ECHO(%~n0: The Full File Path That opened this script is:
ECHO(%~n0:   %1
ECHO(%~n0:
ECHO(%~n0: The Full File Path That opened this script, without Quotes, is:
ECHO(%~n0:   %~1
ECHO(%~n0:
ECHO(%~n0: ---------------------------------------------------------
ECHO(%~n0: File Name: "%~n1"
ECHO(%~n0: File Path: "%~dp1"
ECHO(%~n0: File Size: "%~z1"
ECHO(%~n0: File Ext:  "%~x1"
ECHO(%~n0: File Date: "%~t1"
PAUSE

When I double-click on an Example .c File The result is:

C_Parser:
C_Parser: =====================
C_Parser: Begin Script "C:\Admin\C_Parser.cmd"
C_Parser: =====================
C_Parser:
C_Parser: The Full File Path That opened this script is:
C_Parser:   "C:\Admin\testfile.c"
C_Parser:
C_Parser: The Full File Path That opened this script, without Quotes, is:
C_Parser:   C:\Admin\testfile.c
C_Parser:
C_Parser: ---------------------------------------------------------
C_Parser: File Name: "testfile"
C_Parser: File Path: "C:\Admin\"
C_Parser: File Size: "35"
C_Parser: File Ext:  ".c"
C_Parser: File Date: "07/09/2019 03:20 PM"
Press any key to continue . . .
Ben Personick
  • 3,074
  • 1
  • 22
  • 29