I am trying to display a full textfile in a batch file, but when I do it like this, it opens notepad.
#echo off
call YourText.txt
pause >nul
exit
I also tried
#echo off
more < YourText.txt
pause >nul
exit
Both with double quotes, and without. However, it doesn't work. Then, when I did it without #echo off it works, but it will display each line as:
C:\Documents and Settings\Gebruiker\Mijn documenten\others\Bureaublad YourTextline 1
C:\Documents and Settings\Gebruiker\Mijn documenten\others\Bureaublad YourTextline 2
I already tried the set /f command without any luck.
Does anybody know how to import a full text file through a batch file?
You're looking for type:
type YourFile.txt
If you want it to pause between screens full of text, combine it with more using the pipe | operator:
type YourFile.txt | more
or using more directly getting input from the text file or via redirection (first example courtesy of #MatthewStrawbridge in his comment):
more YourFile.txt
or
more < YourFile.txt
Related
I have a program BIL.exe that prompts for user four inputs (marked with pink below).
I would like to run this program from command line by specifying the inputs as arguments. Something like this:
.\BIL.exe "HOAL" 78 80 1
In a batch script I can do it as shown below. However this doesn't work in command line.
#echo off
set var1="HOAL"
set var2=78
set var3=80
set var4=1
(echo %var1% && echo %var2% && echo %var3% && echo %var4%)|BIL.exe
Does the solution differ between CMD and powershell?
So given your confirmation that stdin from file worked means that the solution should be simple:
batch_name.cmd
#echo off
(echo %1&echo %2&echo %3&echo %4)|bil.exe
it sends the output in list format. so you should be able to run the batch as:
batch_name.cmd "HOAL" 78 80 1
keep in mind, I am not stripping outside quotations, as it seems that those are used by the executable. If not, then change to:
#echo off
(echo %~1&echo %~2&echo %~3&echo %~4)|bil.exe
As you never need to set anything in relation to the variable name, we simply do not use variables as they come as input meta variables from your input.
Below you can find the content of the two example text files I will use, example1.txt and obf_example1.txt. The latter one contains the string of example1.txt at the end of the file but has some obfuscated strings before.
example1.txt:
adasdkasdaksdasdkjlasdjasndjasd.
obf_example1.txt:
ŠxpÃÒ²Ø-Gêÿ ój"f>ïí H€À(ø4$/+#6Ni9Pvü¶ |CF CÀ¾ý~ª-°à9ÉOÿ V[o¦.E…-Š ƒ9Ú\žê*D´ß()^“£¹ìÅjXÑÍ¥â(¨µ×d'«P|I*èSººº&)Ø|̉ òÔ®¥Ô$LÁ:9ŠLá{¶nZÒبNÙÀØŒ‹0õ´Sék›áÇÉîÆbËF§BЄƒöZKaÒR ²°ÅšDn?+¶()IªP›$ÇEv©¡k€[ßè¨×q-Ëk!µTóPA²—: A ?ÉEEEGÐJúÌ©ÒWµHB¡aäXû|ÓË BPÁwr„Ûi¥åܺÈQ÷ORàSb,Šv¢D ,Žb’(2 öb¢wtKzíĦ#ï¯u©²Ù aîR隬ëÌTbà÷¥3ÄtSGì´R$)X Šù
'¹¨D³ÞeOK3!{·‹¦cäиNÅô:Na1žAÇ1ø8 &Fuôë %¸T¯_òMå†C"ý¤F ™º„Iµºí4Ü¡ˆc!ì•+3 ‰‹M K#JÁ«8¢bsL†!Ù“àšn·öMå•Œ&ýèvÀ}¨?¦hùÊò(É#Žf~5‰‘qØçþƒ‰Å²ÓÖÊJU•âNWÁ«L¼Y”$G¢ßè&§ÖÉØŒS‘WàË„°SØW Ш´_è%‚Å¢ø.ãÃð”#X^þ*1þ‚q85¡lÒ‚Ò>‘¸ÿ £ôQôz#ø¤ÎõÚªï|Xö%;åÍËûGú+îUƒö³‰›p U±Ò ðtÜGÜÿ ð,åXÿ k8È I”ÿ “½¿Ð`¨u5=SÓqyFÈ É8ôã¨ð£è6’H#lÄI10‚Ö§ÑdµÖ?t¡]D†9Zj,¥EɺÜEq¤#,ìn—¢º‚´€bc·ú¨Lû£ÿ Ó×ÿÙ||adasdkasdaksdasdkjlasdjasndjasd.
When I ran the following powershell command for example.txt in a batch file, it works and I get the output of example.txt:
#echo off
for /f "delims=" %%a in ('powershell Get-Content .\example.txt') do set _output=%%a
echo %_output%
adasdkasdaksdasdkjlasdjasndjasd
Good so far.
However, when I ran the above powershell command for obf_example1.txt, it does not work and I get the following error message:
'¹¨D³ÃzeOK3!{·â?¹Â¦cäÃ?Â?¸Â?NÃ.ô:Na1žAÃ╬1ø8
The command "FuôëÂ" is either misspelled or could not be found.
The command "ýèvÃ?}¨?¦hùÃSÂ?ò" is either misspelled or could not be found.
Why? Never mind I thought: As I am only interested in the last n characters both in example1.txt and obf_example1.txt accordingly, my idea was to extract the last n characters and check if I can see the output of obf_example1.txt then. To check if my idea works, I run the following command for example1.txt to get the last 4 characters as an example:
#echo off
for /f "delims=" %%a in ('powershell $a=Get-Content .\example.txt; $a.substring^(0,$a.length-4^)') do set _output=%%a
echo.%_output%
It doesn't show me anything though. %_output% seems to be empty. How to fix that? And will the fixed version work for obf_example1.txt as well so that I get an output there instead of the above error message?
Apparently, the piece of text you are after is behind the ||.
With PowerShell you can easily get that by using
((Get-Content 'D:\Test\obf_example1.txt' -Raw) -split '\|\|')[-1]
Returns
adasdkasdaksdasdkjlasdjasndjasd
Isn't htis what you want?
You could try reading the last 4 bytes, if you really are taking text characters from what is clearly not a text file. (My guess is that it is text hidden inside a binary file, probably a graphic file).
#For /F Delims^=^ EOL^= %%G In (
'%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoP^
"$f=[IO.File]::OpenRead('C:\Users\Ferit\Desktop\obf_example1.txt');"^
"$f.Seek(-4,[System.IO.SeekOrigin]::End)|Out-Null;$buffer=new-object Byte[] 4;"^
"$f.Read($buffer,0,4)|Out-Null;$f.Close();"^
"[System.Text.Encoding]::UTF8.GetString($buffer)"')Do #Set "_output=%%G"
#Set _output 2>NUL&&Pause
Don't forget to modify the text file path, (on line 3), and the three instances of 4 if you want more or less bytes. The last line is included just to show you the output, (you would obviously replace that with your own code).
The following works for me to get the "clear-text" part after || (from your example):
for /f "delims=" %%a in (.\obf_example1.txt) do set "_output=%%a"
set _output
echo testing last 10: %_output:~-10%
set "_output=%_output:*||=%"
set _output
echo %_output%
(it might not work with different encodings of the text file)
(Consider Powershell - cmd has a limit on line length and can easily be overwhelmed)
I tried to start a exe (jtag console) from a batch file and feed the data from a file to it. This worked well with:
nios2-terminal.exe < test.txt
But there are null chars (0x00) in this file. The chars after the null chars are not passed to the exe, just more zeros, probably because of null terminated strings.
So I splitted the file in 4 files where just the last char is a null and feeded them one after another. The problem is I have to kill the processes, because they wont do it thereself. I need to use the start command.
But I don't know how to feed the file not to the start command but to the exe. This does probably the first:
start /d %path% nios2-terminal.exe < test0.txt
ping 1.1.1.1 -n 1 -w 1 > nul REM// 1ms delay, tried more, too
taskkill /f /im nios2-terminal.exe
start /d %path% nios2-terminal.exe < test1.txt ...
As nios2-terminal.exe puts all chars from stdIn to the jtag of my FPGA, I thougth to use start /b /d %path% nios2-terminal.exe and then read all the files with type testx.txt.
When I do this manualy, I have to press Return first and then the first char I type is put on jtag. In a script I don get this working.
Is ther any other posibility like sending input to the running instance of nios2-terminal.exe or some alternative for the JTAG terminal nios2-terminal.exe, which doesn't mind NULL char inputs.
I hope I didn't missed any threat that allready covers this issue, but I found none.
Thanks a lot
edit:
The title says stdin and stdout redirect because I also want to redirect the output of the program to a file. But I think once I got how to redirect the input with the start command, I can handle this, too.
try this:
nios2-terminal.exe > test.txt
and if you want to append use this cmd:
nios2-terminal.exe >> test.txt
I am trying to redirect a PS output to a file and process it further.
For this I am using the Printer Port Redirection RedMon which is sending the output to CMD.exe
C:\Windows\system32\cmd.exe
As arguments I expected that something like the following should work, but it does not. "%1" contains the user input for filename.
/c >"%1"
or
/c 1>"%1"
or
/c |"%1"
or
/c > "%1" 2>&1
What almost works if I send the output to a batch file which writes it then to file.
/c WriteOutput.bat "%1"
However, the batch file is somehow altering the file (skipping empty lines, and ignoring exclamation marks and so on...)
If possible I want to avoid a batch file. Is there a way to get it "directly" to a file?
Select "Print to FILE" in the printer options is not an option for me. I want the same end result but via cmd.exe being able to process it further.
Any ideas?
Edit:
Well, that's the batch file I used. It neglects empty lines and space at the beginning.
#echo off
setlocal
set FileName=%1
echo(>%FileName%.ps
for /F "tokens=*" %%A in ('more') do (
echo %%A>>%FileName%.ps
)
Well, so far I still haven't found a direct way to write STDIN via RedMon via CMD.exe to a file. As #aschipfl wrote, all the versions with for /F will skip lines and ignore certain characters.
However, with the following batch script (via RedMon) I end up with a "correct looking" file on disk.
C:\Windows\system32\cmd.exe /c WritePS.bat "%1"
"%1" contains the user input for filename without extension.
The Batch-File WritePS.bat looks as simple as this:
#echo off & setlocal
set FileName=%1.ps
more > "%FileName%"
However,
the resulting Postscript file is different from a file which I "Print to FILE" via the Postscript-Printer setup. I am pretty sure that all the printer settings which I can set are the same in both cases.
If anybody has an idea why there might be a difference, please let me know.
I need to write a .bat or .cmd script that will find all instances of file type .log in the directory it is run from, and for each of those search it for "searchstring", counting how many times it appears. Then I need to rename the file (original name: "[name].log") to "name.log". This is to enable me to get a very quick visual count of the number of errors in a file (which is part of what the log contains).
I've already got the for loop that locates all *.log files, but how do I count instances of a particular string?
try this:
for /f "tokens=2delims=:" %a in ('find /c "string" *.log') do #set /a count+=%a
echo %count%
Code is for shell prompt. For shell file replace %a with %%a.