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

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)

Related

Open terminal and execute command with arguments in command line mac

I am currently using :
Runtime.getRuntime().exec("open -a Terminal" + directory + " " + argument);
My argument happens to also be a directory.
My problem is that it will open argument instead of considering it an argument of directory. The outcome will be the same as running the following:
Runtime.getRuntime().exec("open -a Terminal" + directory)
and
Runtime.getRuntime().exec("open -a Terminal" + argument)
Instead of typing directly in a terminal :
$/Users/xxxxx/RestOfPath /Users/xxxxx/argument
Is there a solution which would look like this without parenthesis?
Runtime.getRuntime().exec("open -a Terminal (" + directory + " " + argument+ ")");
Thank you!
You could try escaping the space
Runtime.getRuntime().exec("open -a Terminal" + directory + "\\ " + argument);

OSX 10.6.8 Xcode 4.2 Change Created and Modified Dates

I've been working on an Applescript to change the creation and modification dates of a particular file via (do shell script). The first script with a manually input date works fine, but the second script which uses the (current date command) with an appended time, throws an error code, even though the output from both scripts appears to be the same. Any help would be most appreciated.
Script #1
resetFile()
on resetFile()
do shell script "SetFile -d '02/18/2016 00:01:00' ~/Desktop/Test.txt"
do shell script "SetFile -m '02/18/2016 00:01:00' ~/Desktop/Test.txt"
end resetFile
Script #2
resetFile()
on resetFile()
set {year:y, month:m, day:d} to (current date)
set {year:y, month:m, day:d} to result
if (d * 1) < 10 then
if (m * 1) < 10 then
set dateTime to quoted form of ("0" & m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
else
set dateTime to quoted form of (m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
end if
else
if (m * 1) < 10 then
set dateTime to quoted form of ("0" & m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
else
set dateTime to quoted form of (m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
end if
end if
display dialog dateTime
do shell script "SetFile -d dateTime ~/Desktop/Test.txt"
do shell script "SetFile -m dateTime ~/Desktop/Test.txt"
end resetFile
This is a simpler version to create the date string. It uses a handler to add leading zeros if needed:
resetFile()
on resetFile()
set {year:y, month:m, day:d} to (current date)
set dateTime to quoted form of (pad(m as integer) & "/" & pad(d) & "/" & y & space & "00:01:00")
display dialog dateTime
do shell script "SetFile -d " & dateTime & " ~/Desktop/Test.txt"
do shell script "SetFile -m " & dateTime & " ~/Desktop/Test.txt"
end resetFile
on pad(v)
return text -2 thru -1 of ((v + 100) as text)
end pad

How to use a gnu command in c#

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");

rsync complex filter

I was an Unison fan but I need more complicated filters to back up my laptop.
After hours of trying I obtained for the most part what I want; this is my situation:
This is my bash script:
#!/bin/sh
rsync -aHAXzhv --dry-run \
--progress \
--stats \
--delete \
--prune-empty-dirs \
--filter="merge filter" \
/ /media/mypassport/laptop/
and this is my filter:
+ etc
+ etc/apache2
+ etc/apache2/sites-available
+ etc/apache2/sites-available/*
+ home
+ home/ale
+ home/ale/*
+ home/ale/.filezilla/*
+ home/ale/.emacs.d/*
+ home/ale/.evolution/*
+ home/ale/.mozilla/*
+ home/ale/.ssh/*
+ home/ale/.subversion/*
+ home/ale/.unison/*
+ root
+ root/*
+ root/.c/*
+ usr/
+ usr/local/
+ usr/local/*
+ var/
+ var/www
+ var/www/*
+ *.git/***
+ *.svn/***
+ *.cvs/***
- etc/*
- etc/apache2/*
- home/ale/.** # this exclude hidden folder in ale's home
- var/*
- root/.**
- lib64
- initrd.img.old
- initrd.img
- vmlinuz
- vmlinuz.old
- *~
- .*~
- *.o
- *.tmp
- .*
- */
I would like to be able to exclude ALL hidden files in home folderS and include just few of them in this way:
+ home/*/.bashrc
- home/*/.*
but it does not work.
Any help would be appreciated.
Best regards
Thank you

renaming files using variables in Ruby

How do you use variables to rename files in Ruby?
File.rename("text1.txt", "text2.txt")
The above example is fine when using irb, but I writing a script where both var1 and var2 are unknown to me.
for example:
script_dir = File.expand_path File.dirname(__FILE__)
Dir.chdir(script_dir)
Dir.glob('Cancer1-1.pencast').each do |pencast|
pencast_title = File.basename(File.basename(pencast), '.*')
i = 1
audio_title = File.basename(`unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`)
audio_path = `unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`
audio_extension = File.extname(File.basename(audio_path))
new_name = "#{pencast_title}-#{i}#{audio_extension}"
File.rename(audio_title, new_name)
does not work...
but if i use puts var1 I see the file name I want.
The error I get is:
prog_test.rb:12:in `rename': No such file or directory - audio-0.aac (Errno::ENOENT)
or Cancer1-1-1.aac
from prog_test.rb:12
from prog_test.rb:5:in `each'
from prog_test.rb:5
but the file audio-0.aac is there... I'm looking at it.
I am certain I have located the problem:
it seems to be adding a variable to another variable. This is a simplified example that produces the same output:
audio_title = "audio-0.aac"
fullPath = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + audio_title
newname = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + "audio1.aac"
puts fullPath
puts newname
File.rename(fullPath, newname)
OUTPUT :
/Users/name/Desktop/audio-0.aac
/Users/name/Desktop/audio1.aac
prog_test.rb:22:in `rename': No such file or directory - /Users/name/Desktop/audio-0.aac or /Users/name/Desktop/audio1.aac (Errno::ENOENT)
from prog_test.rb:22
You should be passing the full file path to File.rename, not just the basename
I am not sure what is going on in your example inside File.basename() , but imagine the following:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
basename = File.basename(fullPath) # File
newFileName = "File.bak"
File.rename(basename, newFileName)
# How can Ruby possibly know which directory to find the above file in, or where to put it? - It will just look in the current working directory
So instead, you need to pass the full path to File.rename, like so:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
directory = File.dirname(fullPath) # C:\Folder
newFileName = "File.bak"
File.rename(fullPath, directory + File::SEPARATOR + newFileName)

Resources