Batch with files as input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Captcha142
Posts: 13
Joined: 18 Sep 2011 23:35

Batch with files as input

#1 Post by Captcha142 » 18 Sep 2011 23:47

Is there a way to write a batch file and then associate the file type with the batch file so that the batch file runs commands with the selected file name

I know how to associate file types, it's the batch file part I don't understand.
The goal of this is to run the following commands:

@echo off
javac (name of run file).java
java (name of run file)

Is this possible?

EDIT:
Alternatively, could I simply run the batch and pass a command to it.
eg. (where the batch file name is test.bat)

C:\System32>test | (name of file)

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Batch with files as input

#2 Post by Bob D » 19 Sep 2011 16:27

The START command can execute commands external to the current window. It can also name files which will be processed by a file extension association. You can execute them and wait for them to finish or simply launch them and forget them. Look at START /?

I used the following in a shortcut with the intent to lift the execution priority. I launch a cmd.exe, then issue the START command to start a batch file at high priority. You could use it within a batch file to do what you want. Is the java an example? If you can run it via a command line then you should be able to do it. See CMD /? and START /? for more detail.

C:\Windows\System32\cmd.exe /C start /realtime /b c:\bootlog\pcinfo.bat 45 0d

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Batch with files as input

#3 Post by nitt » 19 Sep 2011 17:00

Captcha142 wrote:Is there a way to write a batch file and then associate the file type with the batch file so that the batch file runs commands with the selected file name

I know how to associate file types, it's the batch file part I don't understand.
The goal of this is to run the following commands:

@echo off
javac (name of run file).java
java (name of run file)

Is this possible?

EDIT:
Alternatively, could I simply run the batch and pass a command to it.
eg. (where the batch file name is test.bat)

C:\System32>test | (name of file)


I'm confused by your request, so I think I'll break it down.

Is there a way to write a batch file


Yes, you use these weird things, that I have named myself, which I call "senders". You send the output of a command to a file.

Code: Select all

echo I love pie > test.txt


Will create a file named "test.txt" with the text "I love pie" inside.

associate the file type with the batch file


... Usually if you write a file, you should know the file type. I'm not 100% sure that there isn't a Batch command for this, but I don't think there is. So you can always just mix it with JScript. Here's an example:

Code: Select all

@echo off
set fname=bob.txt
echo.var file = new ActiveXObject("scripting.filesystemobject").getfile("%fname%");WScript.StdOut.Write(file.type);>~tmp.jse&cscript //nologo ~tmp.jse&ping 0 -n 1 > nul & del ~tmp.jse > nul


Now if you want to send the file type to a variable, use:


Code: Select all

@echo off
set fname=bob.txt
echo.var file = new ActiveXObject("scripting.filesystemobject").getfile("%fname%");WScript.StdOut.Write(file.type);>~tmp.jse
for /f "tokens=*" %%a in ('cscript //nologo ~tmp.jse') do (
set ftype=%%a
)
del ~tmp.jse
echo The file type is: %ftype%
pause


I like my method because instead of just giving you the extension, this will actually tell you what the file type is named. Such as, if I used "vp.com" instead of "bob.txt", it would say:

Image

the batch file runs commands with the selected file name


You always know the file name already in codes. Such as if you write it or read it, you need the name. If you want to make the prompt use "open with", it sends the name of the file it was opened with to "%1".


Bob D wrote:The START command can execute commands external to the current window. It can also name files which will be processed by a file extension association. You can execute them and wait for them to finish or simply launch them and forget them. Look at START /?

I used the following in a shortcut with the intent to lift the execution priority. I launch a cmd.exe, then issue the START command to start a batch file at high priority. You could use it within a batch file to do what you want. Is the java an example? If you can run it via a command line then you should be able to do it. See CMD /? and START /? for more detail.

C:\Windows\System32\cmd.exe /C start /realtime /b c:\bootlog\pcinfo.bat 45 0d


First of all, why the freaking pickle are you writing out the full path of a file that's already in your system32 folder?!?!?!?!?!
And second of all, I don't even have bootlog/pcinfo.bat on my PC! So I don't even see your point because any newbie can't test it!
Also, why "/realtime"? It usually works okay without that...

So it is unhelpful.

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Batch with files as input

#4 Post by Bob D » 19 Sep 2011 17:39

First of all, why the freaking pickle are you writing out the full path of a file that's already in your system32 folder?!?!?!?!?!
And second of all, I don't even have bootlog/pcinfo.bat on my PC! So I don't even see your point because any newbie can't test it!
Also, why "/realtime"? It usually works okay without that...


Hmmm, perhaps you have never created a shortcut and looked at your work. The system (Win 7 in my case) expands the file name of the executable to include the full path. Just try it making a shortcut with the command CMD.EXE then look at its properties.

Nobody asked you if you had the file pcinfo.bat on your system.

As for /realtime, as I said, I wanted to lift the priority.

The command was shown as an example of what can be done with the START command. I didn't state the reasons for the short cut on my system, nor do they matter, but you seem to have made some irrelevant assumptions.

I didn't give precise code to suit the original request because, just like you, I wasn't exactly sure what he wanted.

Captcha142
Posts: 13
Joined: 18 Sep 2011 23:35

Re: Batch with files as input

#5 Post by Captcha142 » 19 Sep 2011 17:41

Thank you lots nitt, but in order for all commands to work, I need to be able to remove the extension from the filename.

Is this possible?


the batch currently reads:

Code: Select all

@echo off
javac %1
REM this compiles .java file
java %1
REM for command to work I need to lose .java extension
pause

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch with files as input

#6 Post by aGerman » 19 Sep 2011 18:21

Yes, this is possible.

Code: Select all

@echo off
javac "%~1"
java "%~n1"
pause

You could replace javac and java by echo (or simly prepend an echo to these lines) to display what came in.

Regards
aGerman

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Batch with files as input

#7 Post by nitt » 19 Sep 2011 18:45

Bob D wrote:Hmmm, perhaps you have never created a shortcut and looked at your work. The system (Win 7 in my case) expands the file name of the executable to include the full path. Just try it making a shortcut with the command CMD.EXE then look at its properties.


All your commands are either built in to CMD/COMMAND or stored in your System32 and SysWOW64 folders.

Bob D wrote:Nobody asked you if you had the file pcinfo.bat on your system.


Okay? That still doesn't bring down what I said.

Bob D wrote:The command was shown as an example of what can be done with the START command. I didn't state the reasons for the short cut on my system, nor do they matter, but you seem to have made some irrelevant assumptions.


Ohoho, that's where you're wrong. Everything you post is relevant to the reader, because they'll most likely try to break down and understand the command, but when you give them crap they can't break down, then it's worthless.

Captcha142 wrote:Thank you lots nitt, but in order for all commands to work, I need to be able to remove the extension from the filename.


You can always do:

Code: Select all

@echo off
set file=bob.txt

for /f "tokens=1 delims=." %%a in ('echo %file%') do (
set file=%%a
)

echo %file%
pause


The only problem with that is, is if there is a couple dots in the filename, it may mess it up. I have a good for /f explanation here.

Or you could just listen to aGerman. He's probably more advanced than I am.

Captcha142
Posts: 13
Joined: 18 Sep 2011 23:35

Re: Batch with files as input

#8 Post by Captcha142 » 20 Sep 2011 17:00

one last question with for /f:
I read the thread, and is there a way to use a variable as a delimiter,

and can you use everything after the delimiter?

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Batch with files as input

#9 Post by nitt » 20 Sep 2011 18:23

Captcha142 wrote:one last question with for /f:
I read the thread, and is there a way to use a variable as a delimiter,

and can you use everything after the delimiter?


Batch isn't like normal programming. When you use a variable, it actually takes place of the code. So if I use:

Code: Select all

@echo off
set file=bob.txt
set cho=.
for /f "tokens=1 delims=%cho%" %%a in ('echo %file%') do (
set file=%%a
)

echo %file%
pause


It will actually replace all of the "%cho%"'s with "."'s.

Post Reply