Using a variable that's part of a filename to open that file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
I.Love.to.Use.DOS
Posts: 2
Joined: 25 Jun 2012 22:22

Using a variable that's part of a filename to open that file

#1 Post by I.Love.to.Use.DOS » 25 Jun 2012 23:23

I'm not too smart in batch files.
If you have an idea that I can 'grow' with that would be fine!



Here's the basics of it:

I'll want to use a variable when typing into Run field this way:

C PLASTIC

Where typing PLASTIC would open PLASTIC.RTF

typing

C PLASTIC will open PLASTIC.COMMENT.RTF

---------------

The BATCH files I have only open text files.
e.g. Typing PLASTIC opens 'PLASTIC.RTF'

We'll call those the old text files I've had for years. The new text files will be the same names
except they'll have
.COMMENT
added to the name.

Another way to say that:
Their names will be names that I already have on other text files and with .COMMENT inserted into the name.

e.g PLASTIC.RTF gets a 'brother' and his file name is PLASTIC.COMMENTS.RTF


I'll want to use a variable when typing into Run field this way:

C PLASTIC

Where typing PLASTIC would open PLASTIC.RTF

typing

C PLASTIC will open PLASTIC.COMMENT.RTF





(1)
; Batch File Name 'PLASTIC.bat'
@echo off

start "D:\Program Files\Cwordpad\CWordPad.exe" "c:\Users\Acer1\Documents\PLASTIC.rtf"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using a variable that's part of a filename to open that

#2 Post by foxidrive » 26 Jun 2012 09:58

I.Love.to.Use.DOS wrote:I'll want to use a variable when typing into Run field this way:

C PLASTIC

Where typing PLASTIC would open PLASTIC.RTF

typing

C PLASTIC will open PLASTIC.COMMENT.RTF


(1)
; Batch File Name 'PLASTIC.bat'
@echo off

start "D:\Program Files\Cwordpad\CWordPad.exe" "c:\Users\Acer1\Documents\PLASTIC.rtf"



Call this a.bat and then you can type this (including the a)

a plastic
and it will open plastic.rtf

If you type

a b plastic
then it will open plastic.comment.rtf
b can be any character.



and if you change plastic to another word, say steel

a steel
it will open steel.rtf

and if you type
a b steel
then it will open steel.comment.rtf



Code: Select all

@echo off
if  "%~2"=="" (start "D:\Program Files\Cwordpad\CWordPad.exe" "c:\Users\Acer1\Documents\%~1.rtf")
if not "%~2"=="" (start "D:\Program Files\Cwordpad\CWordPad.exe" "c:\Users\Acer1\Documents\%~2.comment.rtf"

I.Love.to.Use.DOS
Posts: 2
Joined: 25 Jun 2012 22:22

Re: Using a variable that's part of a filename to open that

#3 Post by I.Love.to.Use.DOS » 30 Jun 2012 12:38

I just saw your post this morning. I'll have a good look at it
and try it but the idea looks good...
I hope it works. I'll let you know.
Thank you! Really appreciate the note.

Post Reply