`print` stdin without file - batch-file

Suppose in a CMD (Windows 7) batch file I want to print the contents of a directory. I'd expect to do something like this:
dir | print
But this doesn't work, because print apparently requires a file. Is there an intelligent one-liner to do this? (preferably not involving file manipulation)

You can do the following
dir > f && print f
If you get the error Unable to initialize device PRN, then you will need to specify the printer using the /D: switch with the print command. You can also delete the file after running the command if you want.
dir > f && print f /D:"Printer Name" && del f
To do it without outputting to a file you can just redirect the command output to a shared printer.
dir > \\share\printer
To get a list of shared printers use PowerShell
get-WmiObject -class Win32_printer | ft name, systemName, shareName
I prefer outputting to a file then printing the file, so the name of the print job has some relevance to the actual content.

Related

Read text file content using batch file command

I have text file having content like
8.4.0.154
newline
I have written below command in a batch file to read the first line means 8.4.0.154
set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%
here it is just printing 8 prefixes with some special symbol as shown in below image
could anyone help me out here to figure it out that why am not able to read complete number in batch file and print it out.
Thanks in advance
I was creating this text file using PowerShell script using below command:
param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version > $textFile
The text file was not getting created with ANSI encoding hence, unable to read using a batch file.
Now, I changed the above code as below and it is working.
param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version | Out-File $textFile -Encoding Ascii
I am able to read the text file content using below command
set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%

csh set: no match error wildcard

trying to look up files in directories with wildcard *, and put the names into an array
the files have similar names (MATCHr1, MATCHr2 ... )
the problem arises when the file does not exist (which is a possibility)
set command returns a "no match" error and terminates the loop
if this happens how can i get it handle the error by jumping to the next iteration?
set SUBIDS = (10003 10005 10006)
foreach SUBID ($SUBIDS)
foreach SEQR ( MATCH ENC NBACK SIMON FACE )
ls -l *${SEQR}*.nii.gz
set Array = *${SEQR}*.nii.gz
echo $Array[*]
....rest of code works to use contents of Array to create text files
and works great when the *${SEQR}*.nii.gz returns a file name
but fails when the no matches are made with the wildcard
Any help would be well apreciated
thanks!
you can do you ls and check the $status (or $STATUS, depends on your system) flag:
ls -l *${SEQR}*.nii.gz >> /dev/null ; if !$status then ...

passing values of text file to array in shell scripting

My script fetches the names of directories in a path and stores in a text file.
#!/bin/bash
MYDIR="/bamboo/artifacts"
DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`
plan_list.txt:
**************
PLAN1
PLAN2
PLAN3
Now I am trying to pass each of these directory names to a URL to get output like this.
http://bamboo1.test.com:8080/browse/PLAN1
http://bamboo1.test.com:8080/browse/PLAN2
http://bamboo1.test.com:8080/browse/PLAN3
The script to do that doesn't seem to work
bambooServer="http://bamboo1.test.com:8080/browse/"
for DIR in $DIRS
do
echo `$bambooServer+$DIR`
done
Could someone please tell me what I am missing here? Instead of storing the ls command output to a plan_list.txt file i tried passing to array but that didn't work well too.
DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`
DIRS is just an empty variable since your command is not producing any output and just redirecting output to plan_list.txt.
You can rewrite your script like this:
#!/bin/bash
mydir="/bamboo/artifacts"
cd "$mydir"
bambooServer="http://bamboo1.test.com:8080/browse/"
for dir in */
do
echo "$bambooServer$dir"
done
*/ is the glob pattern to get all the directories in your current path.

I want to rename the command prompt screen from long path to short path or short name

I want to rename the command line path to a simple one so that I have more space to work on the window. Also it will keep the screen more neat
I want to rename the command prompt screen from
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64
to
C:\NETFXPATH
I learned in my old days where you issue a command line ren xyz and the screen suddently becomes c:\xyz or xyz so that its easy to work on the screen.
How do I make this happen?
Unfortunately, that's not possible using the standard PROMPT command-line. It's not one of the options accepted by PROMPT:
J:\>prompt /?
Changes the cmd.exe command prompt.
PROMPT [text]
text Specifies a new command prompt.
Prompt can be made up of normal characters and the following special codes:
$A & (Ampersand)
$B | (pipe)
$C ( (Left parenthesis)
$D Current date
$E Escape code (ASCII code 27)
$F ) (Right parenthesis)
$G > (greater-than sign)
$H Backspace (erases previous character)
$L < (less-than sign)
$N Current drive
$P Current drive and path
$Q = (equal sign)
$S (space)
$T Current time
$V Windows version number
$_ Carriage return and linefeed
$$ $ (dollar sign)
If Command Extensions are enabled the PROMPT command supports
the following additional formatting characters:
$+ zero or more plus sign (+) characters depending upon the
depth of the PUSHD directory stack, one character for each
level pushed.
$M Displays the remote name associated with the current drive
letter or the empty string if current drive is not a network
drive.
You could potentially work around it by creating junctions with MKLINK to shorten the path to the folder, or by using SUBST to map folders to a drive letter.
No way to get something like your example.
But you can get a short prompt (without any path) with
prompt $g
which will only show ">"
or if you only need more space to write your commands, you can add a Linefeed to prompt:
prompt $p$_ $g
which will show the path (as the "usual" prompt), but gets input from the next line.

How do i make multiple copies of a single file with different filenames?

I want to create a windows batch file (Win7) to achieve the following:
Copy source.doc to destination with destinationFilename.doc taken from a list in a text file (nameList.txt)
I have batch file that will make directories from nameList.txt but I can't figure out how to modify the batch file to make it copy source.doc in the required manner.
Using xargs you can process a file nameList.txt which contains a newline separated list of target filenames like this:
cat nameList.txt | xargs -I "F" cp source.doc F
where -I "F" defines F as a placeholder to be used in command invocation of cp.

Resources