How to use a gnu command in c# - cmd

I have a problem using a porting of the Gnu "find" in my c# app. Let me explain better:
in my winform application i have a button. When i press this button i need to launch this command (in cmd.exe this works well):
gfind training_set\positives\*.jpg -exec identify -format "%i 1 0 0 %w %h \n" {} ; > training_set\positives.dat
I used this:
string find = "gfind.exe ";
string command = textBox4.Text + "\\positives\\*.jpg -exec identify -format \" %i 1 0 0 %w %h \n\" {} /; > " + textBox4.Text + "\\positives.dat";
System.Diagnostics.Process.Start("CMD.exe", "/C "+find + command);
but the shell opens and tell me that i haven't passed any argument to the -exec
Any ideas?
EDIT:
this work:
string path = textBox4.Text + #"\positives\";
System.Diagnostics.Process.Start("CMD.exe", "/C cd " + textBox4.Text + " && gfind positives\*.png -exec identify -format ; > " + textBox4.Text + "\positives.dat");
BUT
-format requires this argument: '%i 1 0 0 %w %h \n' {}
in this way don't work...
EDIT2:
OK almost done..
System.Diagnostics.Process.Start("CMD.exe", "/C cd " + textBox4.Text + " && gfind positives\\*.png -exec identify -format \"%i 1 0 0 %w %h\" {} ; > " + textBox4.Text + "\\positives.dat");
Ok this work, but print every entry in the same row:
positives\3LiIIH8.png 1 0 0 28 20positives\5NoLpfy.png 1 0 0 28 20positives\7XNpLX0.png 1 0 0 28 20
I need one entry per row like:
positives\3LiIIH8.png 1 0 0 28 20
positives\5NoLpfy.png 1 0 0 28 20
positives\7XNpLX0.png 1 0 0 28 20
\n break the sintax

Solved... quotes and escapes characters make me crazy
System.Diagnostics.Process.Start("CMD.exe", "/C cd " + textBox4.Text + " && gfind positives\\*.png -exec identify -format \"%i 1 0 0 %w %h\\n\" {} ; > " + textBox4.Text + "\\positives.dat");

Related

jq in a Jenkins pipeline not saving output to variable

So in my Jenkins pipeline I run a couple of curl commands across different stages. I store the ouput of Stage1 into a file and for every item in that list I run another curl command and use the output of that to extract some values using jq.
However from the second stage I can't seem to store the jq extracted values into variables to echo them later. What am I doing wrong?
{Stage1}
.
.
.
{Stage2}
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh (script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh (script: "jq -r '.component.name' <<< '${stageTwoList}' ")
pfKey = sh (script: "jq -r '.component.key' <<< '${stageTwoList}' ")
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
returns in the end for Stage2
[Pipeline] sh
+ jq -r .component.name
digital-hot-wallet-gateway
[Pipeline] sh
+ jq -r .component.key
dhwg
[Pipeline] echo
Component Names and Keys
| null | null |
Any help in the right direction appreciated!
You passed true as the argument for the returnStdout argument to the shell step method for stageTwoList, but then forgot to use the same argument for the JSON parsed returns to the next two variable assignments:
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh(script: "jq -r '.component.name' <<< '${stageTwoList}' ", returnStdout: true)
pfKey = sh(script: "jq -r '.component.key' <<< '${stageTwoList}' ", returnStdout: true)
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
Note you can also make this much easier on yourself by doing the JSON parsing natively in Groovy and with Jenkins Pipeline step methods:
String stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
Map stageTwoListData = readJSON(text: stageTwoList)
pfName = stageTwoListData['component']['name']
pfKey = stageTwoListData['component']['key']

How can I use inline variables in a .bat file?

I need to convert the following to be compatible with a batch script.
cmd.exe cd " + homepath + "\\" + a
mvn archetype:generate -DarchetypeCatalog=file://"+ homepath + "/.m2/repository
1
c
b
c
uuid.toString()
Y
cd " + homepath +"\\"+ a +"\\" + b
vn clean install
"cd " + homepath +"\\" + a
a
cd " + homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target
jar -xvf " + zipDirectory
cmd cd " + homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target\\" + "\\META-INF\\maven\\" + c + "\\" + b + "-plugin
copy pom.xml " + pluginDirectory
cd " + pluginDirectory
rename pom.xml " + b + "-plugin-1.0.0.pom
color 0a
For a line like the first one:
cmd.exe cd " + homepath + "\\" + a
Would the line look like this?
SET homepath = C:\Users\Joe\
SET a = plugins
cmd.exe cd echo %homepath% echo %a%
in batch, there is no string concatenation symbol like on (other) programming languages. You just use the variable instead of the string:
set homepath=%userprofile%
set a=plugins
echo homepath is %homepath% and a is %a%.
cd %homepath%\%a%
(Note: do not use spaces around = with the set command - they would be part of the variable name respectively the value)

Read Multiple lines in log file and set as 1 line in CSV file

We have multiple lines for each revision in a SVN log file. Sometimes there is only one revision per ID, and sometimes there maybe more( I have seen 30).
Each revison is set by a delimter '--------------------------"
We want the re-order the output into a CSV with 1 line for each revision.
See link to attached Input log and output log
Here are links to output & input: https://www.dropbox.com/s/vagfdr4foowsegf/output.csv
https://www.dropbox.com/s/59l7njeehsrg5p3/svn.log
The output.csv has 5 lines. One for each revision or QUES ID
Is there a way to do it with a FOR loop?
[ FOR /F "delims=~" %%a in (svn.log) echo %%a
set col3 = %%a
echo col3 col2 col1 ]
This handles the delimiter lines too and it seems to do what you need from your sample files.
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
#echo off
type "svn.log"|findstr /r /v /c:"^----------"|repl "," " "|repl "^ *([AMD] .*)\r\n" " $1" xm |repl "(^Changed paths:)\r\n" "$1" xm |repl "^[ /t]*$" "" xm |repl "\r\n\r\n" "\r\n" xm |repl "(.*)\r\n(.*)\r\n(.*)\r\n(.*)\r\n" "$4,$3,$1,$2\r\n" xm > "output.csv"
Expanded version also using VBS:
#echo off
set "fileout=%temp%\svntemp.tmp"
type "svn.log"|findstr /r /v /c:"^----------"|repl "," ";"|repl "^ *([AMD] .*)\r\n" " $1" xm |repl "(^Changed paths:)\r\n" "$1" xm |repl "\r\n" "," xm |repl ",,," "," |repl ",," "," |repl ",," "," |repl "," "\r\n" x >"%fileout%"
(
echo Const ForReading = 1, ForWriting = 2
echo infile = "%fileout%"
echo Set fso = CreateObject("Scripting.FileSystemObject"^)
echo Set f1 = fso.OpenTextFile(infile, ForReading^)
echo Do While not f1.AtEndOfStream
echo f = f1.readline
echo Wscript.echo f
echo if lcase(left(f,14^)^) = "changed paths:" then flag = 1 : line = "" else flag = 0
echo do while flag = 1
echo f = f1.readline
echo if not lcase(left(f,9^)^) = "ques id: " then line=line + f + "; " else Wscript.echo line : Wscript.echo f : exit do
echo loop
echo loop
echo f1.close
)>"%fileout%.vbs"
cscript /nologo "%fileout%.vbs" >"%fileout%.2"
type "%fileout%.2"|repl "(.*)\r\n(.*)\r\n(.*)\r\n(.*)\r\n" "$4,$3,$1,$2\r\n" xm > "output.csv"
del "%fileout%" "%fileout%.2" "%fileout%.vbs"

FFMPEG: Cut out last 10 seconds of wave/mp3

I'm using ffmpeg to convert my files from wave to mp3. But for a new service I need to cut out the last 10 seconds of some of the songs (for piracy issues), no matter how long they are. I've only found information about doing this when the length of the track has been known, but for this I need to do it automatically.
Does anyone know which command to use? If I can fade-out 5 seconds before that would be optimal!
python is a powerfull tool for almost everything (tested in linux)
#!/bin/python
from sys import argv
from os import system
from subprocess import Popen, PIPE
ffm = 'ffmpeg -i' # input file
aud = ' -acodec mp3' #add your quality preferences
dur = ' 2>&1 | grep "Duration" | cut -d " " -f 4'
def cutter(inp,t=0):
out = inp[:-5] + '_cut' + inp[-5:]
cut = ' -t %s' % ( duration(inp)-t )
cmd = ffm + inp + aud + cut + out
print cmd; system(cmd)
def fader(inp,t=0):
out = inp[:-5] + '_fade' + inp[-5:]
fad = ' -af "afade=t=out:st=%s:d=%s"' % ( duration(inp)-t, t )
cmd = ffm + inp + fad + out
print cmd; system(cmd)
def duration(inp):
proc = Popen(ffm + inp + dur, shell=True, stdout=PIPE, stderr=PIPE)
out,err = proc.communicate()
h,m,s = [float(x) for x in out[:-2].split(':')]
return (h*60 + m)*60 + s
if __name__ == '__main__':
fname=' "'+argv[1]+'"'
cutter(fname,10)
fader (fname, 5)
# $ python cut_end.py "audio.mp3"
To fade-out the command is
ffmpeg -i audio.mp3 -af "afade=t=out:st=65:d=5" test.mp3
t: type (in|out)
st: start time
d: duration
To automate
for i in *wav;do python cut_end.py "$i";done
You can concatenate (cutter->fader) to do what you want.
Regards.

Is it possible to use AutoHotKey to run PSLoggedOn and edit the output in the stream and return the result?

I am using AutoHotKey to build a simple GUI tool that uses the Sysinternals tool PSLoggedOn. First if I use
run psloggedon.exe -l -x \\computername I don't get any output at all.
So I tried run %ComSpec% /C psloggedon.exe -l -x 1> %Temp%\psloggedon.txt and that gives me the output in domain\username format for each user logged in.
Like my first example I would rather just run psloggedon and get the output instead of first opening a command prompt; is that possible somehow?
Either way I want to take the output and avoid writing it to a file, but instead edit the output from the output stream to remove the "domain\" part and then just return the username. How can this be done without any third-party software?
This answer to another question on Serverfault shows a way to do it by reading the StdoutRead.
I too was looking at a way to avoid using a file but have decided it's actually the simplest option.
Code copied from user60738
#include <Constants.au3>
Dim $line, $line2, $file, $icount, $reg, $reg2, $reg3
$file = FileOpen("pclist.txt", 0)
While 1
$line = FileReadLine($file)
If #error Then ExitLoop
$reg3 = ""
$reg2 = ""
$reg = ""
If Ping($line, 200) Then
$reg = #ComSpec & ' /C "' & #ScriptDir & "\pstools\psloggedon.exe -l -x \\" & $line & '"'
$reg = Run($reg, "", #SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
While 1
$reg2 = StdoutRead($reg)
If #error Then ExitLoop
If $reg2 Then
$reg3 &= $reg2
EndIf
WEnd
If StringInStr($reg3, "Error") Then
$reg = "Error"
ElseIf StringInStr($reg3,"No one is logged") Then
$reg = "No one is logged on locally."
Else
$reg = StringTrimLeft($reg3, StringInStr($reg3, Chr(13), "", 3))
$reg = StringTrimLeft($reg, StringInStr($reg, "\"))
$reg = StringTrimRight($reg, StringLen($reg) - StringInStr($reg, Chr(13)))
$reg = StringStripWS($reg, 8)
EndIf
$icount += 1
$line2 &= $icount & #TAB & $line & #TAB & $reg & #CRLF
EndIf
TrayTip("Psloggedon", $icount & " " & $line & " User: " & $reg, 10)
WEnd
FileClose($file)
ConsoleWrite($line2)
FileDelete("C:\output.txt")
FileWrite("C:\output.txt", $line2)
ShellExecute("C:\output.txt")

Resources