Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

converting an alias to use a Pipe

Apr
21
0
alias trim_filename=del e:\C\fred_x.bat > nul & touch/c e:\C\fred_x.bat >nul & dir/s1/a-d/b e:\%1\.* >> e:\C\fred_x.bat & for in (@e:\C\fred_x.bat) do if NOT ISDIR %x echo %@name[%x].%@ext[%x]

I've got an alias as above I want to change it to use a Pipe but can't get it to work Help please
dmsherwood53
 
I did not try this, but this may work for you?
Code:
alias trim_filename=(del e:\C\fred_x.bat > nul & touch/c e:\C\fred_x.bat >nul & dir/s1/a-d/b e:\%1\.* >> e:\C\fred_x.bat & for in (@e:\C\fred_x.bat) do if NOT ISDIR %x  echo %@name[%x].%@ext[%x])

Usage:
Code:
trim_filename myfile.txt |! view

...or...
Code:
trim_filename myfile.txt | view

Make sure to read up on the following;
Ref: https://jpsoft.com/help/grouping.htm
Ref: https://jpsoft.com/help/pipes.htm

Joe
 
Last edited:
alias trim_filename=del e:\C\fred_x.bat > nul & touch/c e:\C\fred_x.bat >nul & dir/s1/a-d/b e:\%1\.* >> e:\C\fred_x.bat & for in (@e:\C\fred_x.bat) do if NOT ISDIR %x echo %@name[%x].%@ext[%x]

my Idea to convert the above using a PIPE is the below
alias trim_file=(iff EXIST fred_x.bat then `del fred_x.bat > nul & touch/c fr
ed > nul`) & dir/s1/a-d/b e:\1\.* |echo %@name[CON].%@ext[CON]

I get NO CLOSING QHOTE and then
.CON.
CON.

Please tell me what's wrong
 
From the help file;
When you use the alias command at the command prompt or in a batch file, you must use back quotes ` around the alias definition if it contains multiple commands, or parameters (discussed below), or environment variables, or variable functions, or redirection, or piping. If you do not use back quotes, parameters, variables and functions are evaluated, and redirection or piping performed during the alias definition, and only the first command becomes part of the alias, the remaining ones are performed immediately. The back quotes prevent this premature expansion. You may use back quotes around other definitions, but they are not required. You do not need back quotes when your aliases are loaded from an ALIAS /R file; see below for details. The examples above and below include back quotes only when they are required.

So...
Code:
alias trim_file=`((iff EXIST fred_x.bat then del fred_x.bat > nul & touch/c fred > nul`) & dir/s1/a-d/b e:\1\.* |echo %@name[CON].%@ext[CON])`

Note also that I have enclosed the entire code with ().

Always make sure that parentheses are balanced.

What exactly are you wanting to do here?
Code:
dir /s1/a-d/b r:\.* | echo %@name[CON].%@ext[CON])

On my system, when I run the following;
Code:
R:\>dir /s1/a-d/b r:\.*
R:\temp\.ses

Do you want this to print out only the file name and extension, but no path?

Joe
 
Does this return the results that you want;
Code:
dir /s1/a-d/b e:\1\.* > tmp9: & do x=0 to %@lines[tmp9:] (echo %@name[%@line[tmp9:,%x]].%@ext[%@line[tmp9:,%x]])

Once the individual parts are working,
we can make it an alias.

Joe
 
Does this return the results that you want;
Code:
dir /s1/a-d/b e:\1\.* > tmp9: & do x=0 to %@lines[tmp9:] (echo %@name[%@line[tmp9:,%x]].%@ext[%@line[tmp9:,%x]])

Once the individual parts are working,
we can make it an alias.

Joe
yeah the avove works but I want to use a pipe
Anybody tell me why the below doesn't work

dir/s0/a-d/b e:\FILE_Trim\.*| echo %@name[CON]
 
Anybody tell me why the below doesn't work

dir/s0/a-d/b e:\FILE_Trim\.*| echo %@name[CON]

I'm not sure what you're trying to do, but %@NAME[CON] is always going to be CON.

Code:
C:\>echo %@name[con]
con

C:\>echo %@name[con.txt]
con

C:\>echo %@name[z:\foo\bar\fum\con]
con

C:\>
 
The alias:
Code:
R:\>alias demo=`(dir /s1/a-d/b r:\* > tmp9: & do x=0 to %@lines[tmp9:] (echo %@name[%@line[tmp9:,%x]].%@ext[%@line[tmp9:,%x]]))`

Executing the alias;
Code:
R:\>demo
DESCRIPT.ION
odbc.exe
screen.dat
TRNSCTNS.DBF
ws.dsn
zoom150.btm
zoom150.lnk
.ses
COUT3ca0.JPS
datamystic_logger_critical_textpipeengine64.log
prep_crosoft Office_root_Office16_sdxs_FA000000083_index_win32_bundle_V8_perf.cache
prep_foundation_win32_bundle_V8_perf.cache
prep_ui_win32_bundle_V8_perf.cache
{FDE1F2AC-2B9E-4D6C-B49D-17044CB1DF79} - OProcSessId.dat
~DF0152242452534165.TMP

Piping the alias to sort;
Code:
R:\>demo | sort
.ses
{FDE1F2AC-2B9E-4D6C-B49D-17044CB1DF79} - OProcSessId.dat
~DF0152242452534165.TMP
COUT3ca0.JPS
datamystic_logger_critical_textpipeengine64.log
DESCRIPT.ION
odbc.exe
prep_crosoft Office_root_Office16_sdxs_FA000000083_index_win32_bundle_V8_perf.cache
prep_foundation_win32_bundle_V8_perf.cache
prep_ui_win32_bundle_V8_perf.cache
screen.dat
TRNSCTNS.DBF
ws.dsn
zoom150.btm
zoom150.lnk

Piping the alias to type;
Code:
R:\>demo | type
DESCRIPT.ION
odbc.exe
screen.dat
TRNSCTNS.DBF
ws.dsn
zoom150.btm
zoom150.lnk
.ses
COUT3ca0.JPS
datamystic_logger_critical_textpipeengine64.log
prep_crosoft Office_root_Office16_sdxs_FA000000083_index_win32_bundle_V8_perf.cache
prep_foundation_win32_bundle_V8_perf.cache
prep_ui_win32_bundle_V8_perf.cache
{FDE1F2AC-2B9E-4D6C-B49D-17044CB1DF79} - OProcSessId.dat
~DF0152242452534165.TMP

Joe
 
I'm not sure what you're trying to do, but %@NAME[CON] is always going to be CON.

Code:
C:\>echo %@name[con]
con

C:\>echo %@name[con.txt]
con

C:\>echo %@name[z:\foo\bar\fum\con]
con

C:\>
OK I SAID it doesn't workthe code belore the PIPEW characyrer puts into the pipe
dir /s1/a-d/b
a series of full paths of all the files in a given directory(s)
I want to filter this thru use of @name & @exn these require a term in [ ] wereas type & sort don't
The pirpose is to return the file mame & extention only
Am I missing the point of piping ?
 
As I posted, you are getting the name and extension with each file.
Code:
R:\>#demo
screen.dat
zoom150.btm
zoom150.lnk
.ses
CIN658.JPS
COUT658.JPS
~DFF12B67E400DA825E.TMP

R:\>#demo | type
screen.dat
zoom150.btm
zoom150.lnk
.ses
CIN658.JPS
COUT658.JPS
~DFF12B67E400DA825E.TMP

Why do you need to use @NAME and @EXT, when you are already getting @NAME and @EXT by default?

Joe
 
Why do you need to use @NAME and @EXT, when you are already getting @NAME and @EXT by default?

For that matter, @FILENAME is probably a better bet than combining @NAME and @EXT. Not all files will have an extension.
 
Am I missing the point of piping ?
Maybe. [I could never figure out what this thread had to do with piping.]

When you say command1 | command2

the standard_output (what you would normally see) of command1 is sent to command2 as standard_input (instead of going to the screen). Command2 must be able to read and process its standard_input. Here are simple examples.

tr translates characters; wc counts lines, words and characters. Below the 5 characters include the carriage return and linefeed supplied by the ECHO command.

Code:
v:\> echo foo | wc
      1       1       5

v:\> echo foo | tr o i
fii
 
I want to filter this thru use of @name & @exn these require a term in [ ] wereas type & sort don't
The pirpose is to return the file mame & extention only
Am I missing the point of piping ?

I don't wish to offend, but I suspect that you are. A pipe sends the output of one command to the input of another command. TYPE and SORT are commands. @NAME and @EXT are functions, not commands. They mostly exist to modify the arguments you pass to a command.

If you want to process lines of output from a command, one way is to use DO /P. For example:

Code:
do f in /p dir /a-d /f ( echo %f -- %@filename[%f] )

There are other ways — the horrific for /f "usebackq" syntax, which makes me want to remove my own eyes with an ice cream fork — but DO /P is probably the easiest.

My DO example above is a single line, but in a batch file the multi-line syntax is probably more useful:

Code:
do f in /p dir /a-d /f
    echo %f - %@filename[%f]
enddo

You can put as many lines as you like between the DO and ENDDO.
 
I don't wish to offend, but I suspect that you are. A pipe sends the output of one command to the input of another command. TYPE and SORT are commands. @NAME and @EXT are functions, not commands. They mostly exist to modify the arguments you pass to a command.

If you want to process lines of output from a command, one way is to use DO /P. For example:

Code:
do f in /p dir /a-d /f ( echo %f -- %@filename[%f] )

There are other ways — the horrific for /f "usebackq" syntax, which makes me want to remove my own eyes with an ice cream fork — but DO /P is probably the easiest.

My DO example above is a single line, but in a batch file the multi-line syntax is probably more useful:

Code:
do f in /p dir /a-d /f
    echo %f - %@filename[%f]
enddo

You can put as many lines as you like between the DO and ENDDO.
Thanks you needn't think you'll cause me to take offence i'm a clumpsy beginner at
this TCC coding & know it
I wanted to know the powers & limitating of piping for its own sake
not because of the specific case .I gues i'll have to go away 7 read the hep files
ie
Ref: https://jpsoft.com/help/grouping.htm
Ref: https://jpsoft.com/help/pipes.htm
 
Hey @dmsherwood53, would this work for you?

It uses CON:, @name[] and @ext[].

Code:
R:\>#demo | for %file in (@CON:) echo %@name[%file].%@ext[%file]
screen.dat
zoom150.btm
zoom150.lnk
.ses
COUT4528.JPS
~DFF12B67E400DA825E.TMP
 
Back
Top