I was wondering: is it possible to write the content of a variable (in my case, the last search) to a file with a command?
I tried the following:
:echo #/ >> /tmp/foo.txt
:#/w /tmp/foo.txt
But that didn't work. Any idea on what is the correct way to do this?
An alternative solution to romain’s proposal is to use the redir command which redirects messages to a file. As described in :help redir,
The messages which are the output of commands are written to that file,
until redirection ends.
To append the contents of the search register, run the following sequence of commands:
redir >> /tmp/foo.txt
echo #/
redir END
This sequence could be turned into a function and/or used as a key mapping.
Related
I am trying to write a little project thing to get my javascript skills up. The goal is for, when using curln, the .cmd file will write %~dp0\#temp with the curl %1 (the curled html result of the first parameter)
expected with curln "https://www.google.com": (long html)
actual: "<< was unexpected at this time."
I have tried looking things up, this was a mesh of what I found online. I have tried using < and << as, a beginner, I don't understand the difference.
Code:
%~dp0\#temp << cmd curl %1
I would like the output of curl to be written to the current directory's (%~dp0) file named #temp. %1 is a preset *.cmd parameter variable that is shown in this example:
example "this is %1"
> and >> are output. The first creates a new file, the second appends to an existing file. < is input. It accepts input (reads) into a program from another source, which is typically a file or the output of another program. It doesn't work with writing to files.
You're most likely looking for curl %1 >>"%~dp0#temp" instead. You don't need the cmd in front of it, as you're already in a command window when the batch file executes.
For a simple transfer I use repeatedly using curl the minimal call is
curl -o "%~dpnx0" remote.url
That ensures the fileName and eXtension are saved in the current DrivePath
There is no error checking or security communication so is only useful for a single .html or .js or .pdf etc.
Thus a complex call can run to dozens of lines.
Hello stackoverflow users!
I'm not really new to batch. I just never used pipes | in batch and even after I read reference on ss64.com I don't understand what's the pipe used for.
At first I thought it is OR operator or something (obviously I know now it's not).
I only know that it's located between two lines (commands) like &, but I still don't get what it does exactly, and how it is used practically in code.
Thanks for answering!
Pipe [|]: Redirect standard output of commandA to standard input of commandB
http://www.robvanderwoude.com/redirection.php
example :
echo KKZiomek | find "KKZ"
will redirect the echo KKZiomek in the input of the FIND and be used as second parameter of it.
Like well commented by #aschipfl the space is piped too.
so better use :
echo KKZiomek| find "KKZ"
The pipe is used to send the output of one command to the input of another command.
For example, del /p will ask for confirmation when you delete files. However, you can pipe echo y to it to send a y to the del command and del will act as if the user had pressed y.
I have a batch file with the following code within:
ECHO .> C:\file.txt
I read about ECHO and understand what it is used for, but what I do not know is what are the characters used for after the word echo (.>) and what is the use of the path of file after that.
It's used to truncate, or create if necessary, the file. echo . outputs a single line which is redirected to the file, effectively truncating it.
To obtain an absolutely empty file I often use cd . > filename. I don't know where I picked that one up but it's been around for a long time on UNIX systems.
> redirects the output of the command before.
echo .>c:\file.txtprints a dot to the file c:\file.txt, overwriting its contents (so it will contain a dot only afterwards)
I think, you got it wrong. Usually echo.>file.txt is used to create a empty file (or delete the contents, if the file exists). (Note the missing space)
I have a demo.txt file. I need to delete content in that file using a batch file. Please tell me the command to delete content for demo.txt file.
break>demo.txt
Try this.it will set an empty file on the place of demo.txt. As break is internal command that does nothing it should be pretty fast.Also the break command can produce output only with /? argument so this makes this method pretty robust.
Command Prompt:
break>c:\'file directory'\demo.txt
PowerShell:
Clear-Content c:\'file directory'\demo.txt
type nul > demo.txt
works and also works in JPSoft's TakeCommand TCC.EXE command shell (where "break" will output "BREAK is ON" rather than nothing as it does in Microsoft CMD.EXE)
The general idea is to find a command that outputs Nothing and redirect that to the file using >
This seems most intuitive to me:
copy /y nul demo.txt
NOTE: Unfortunately, like the other methods provided here, this fails if demo.txt is currently in use by another process. In such a case it is sometimes possible to open the file in a text editor and delete all the contents, even though the file is in use. I don't know of a way to do this from the command line.
If the file is used by another application command prompt redirection may fail (as it requires more file access then necessary). In that case you can use powershell:
PS> Set-Content file.txt $null
Note: do not expect that it will allow access to exclusively opened files.
I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.