Run a self-defined command on many files in a directory tree on the Windows, Mac OS X and Linux command line with a free open source tool.
sfk run "your command $file [$relfile] [...]" [-yes] [-nohead] [-quiet] [...]
run a self-defined command on every file- or directory name.
within your command string, you may specify:
$file - insert full filename, including path.
$quotfile or $qfile - just as $file, but with quotes "" around.
$relfile or $qrelfile - insert relative filename, without path.
$base or $qbase - the relative base filename, without extension.
$ext or $qext - filename extension. foo.bar.txt has extension .txt.
$path or $qpath - the path (directory) without filename.
$relpath or $qrelpath - sub path relative to start folder.
$ufile or $upath - force unix style slashes "/" on output.
$qufile or $qupath - unix slashes and quotes combined.
$since or $qsince - with option -sincediff: the reference file name.
$text or $qtext - one record of input text, similar to $file.
$targ or $qtarg - with -tomake: target filename.
always prefer 'q' forms over non-quoted forms: as soon as there is a filename
containing blanks, e.g. X:\the src files\test one.txt, you will need quotations,
or you have to manually insert \" or \q escaped quotes (see 3rd example below).
you may also use $quotrelfile, $quotsince, $quottext for greater clarity.
if you supply only $path expressions, only directories will be processed.
on single word chain commands like "+run vi", " $qfile" is added automatically.
further pattern support:
-spat activates slash patterns like \t \q \xnn etc.
-upat unix style syntax using # instead of $
-yes really execute. default is just to simulate what would be done.
you may also type run. (with a dot) as quick confirmation.
-nohead does not display the [simulating:] info text.
-noinfo unless you use $text, sfk checks the input filenames
1. if they contain blanks, but no quotes are given within command.
2. if they seem to use the wrong path separator character.
in both cases, a reminder is printed. if you know that your command
needs no changes, add -noinfo or use $text instead of $file.
-quiet does not echo the commands before execution.
-relnames strips the root directory names from filenames.
-i[files] process a text or filename list from stdin.
-idirs process a directory name list from stdin.
on stdin, '#' remark lines and empty lines are skipped. note:
"sfk.exe <list.txt" supports only 4 KB for list.txt under windows.
"type list.txt | sfk.exe" supports unlimited stream length.
-nofile[names] with chaining, does not create ":file " name records.
-printcmd print the full command which is executed to console.
-stoprc=n stop processing if a command returns return code >= n.
command string format
with option -spat, slashpatterns like \t \q \xnn are supported.
due to syntax limitations of the command shell, it may help
- to use \q instead of \" (avoids quote miscounting at shell)
- to use \x26 instead of & (if ampersand is behaving unexpected)
quoted variable expansion
when using sfk variables which contain filenames, like in
run "copy #(src) #(dst)"
then spaces in filenames require enquoting. when using -spat
and \q it may cause conflicts if the filename itself contains
known slash patterns, like \t in file 'mydir\thebar.txt'.
to avoid this you can use (with sfk run only):
run "copy #(qsrc) #(qdst)"
which will surround variable contents by double quotes.
quoted multi line parameters are supported in scripts
using parm trim. type "sfk script" for details.
temporary or permanent output files
if run output is post-processed by command chaining, e.g. run ... +filter,
sfk creates temporary files to collect the output. by default, these files
are deleted when run finishes. say "sfk help options" for more on this.
specify -to targetdir\$file to write command output into a permanent
target fileset. required directories are created automatically.
-to accepts the same mask as run itself, e.g. -to "mydir\$path\$base.tmp"
by default, standard output AND standard error stream are written to file.
add 2>nul to your command to strip the error stream.
return code by variable
sfk variable run.lastrc contains the return code of the external
program called. if multiple files were processed then it contains
only the rc of the last file.
command chaining notes
sfk run "...$path..." +nextcmd: will pass directories, not filenames.
sfk run ... -to tmp\$file +nextcmd: will pass output filenames, not input.
sfk run ... +run: will pass unchanged input filename list.
see alsosfk perline run sfk command(s) per text input line.
sfk runloop run commands using a loop counter.
web referencehttp://stahlworks.com/sfk-runexamplessfk run "attrib -R $qfile" -quiet testfiles\FooBank\BarDriver
removes readonly attribute on all files within BarDriver
sfk run "<img src=$quottext>" -dir . -file .jpg -nohead >index.html
create html-style image list of all jpegs (using just simulation).
note that option -nohead removes the [simulating:] info text lines.
type dirlist.txt | sfk run -idirs "xcopy \"x:\$path\" \"z:\$path\" /I /D"
update-copy all directories from dirlist.txt from x: to z:
sfk run "diff oldsrc\$file newsrc\$file" -relnames -sincediff oldsrc newsrc
compare directories, run "diff" on all files with different content.
sfk run "diff $qsince $qfile" -sincediff oldsrc newsrc
same as above, only shorter and safer (including quotes around filenames).
sfk run "zip update.zip $qfile" -since 20070131 . .java .jsp
collect .java and .jsp files added/changed since 31-Jan-2007 into a zip file.
sfk list testfiles .txt +run vi
open all .txt files in vi. $qfile is added automatically.
sfk sel . .avi +run "ffmpeg -i $file -f image -t .02 thumbs\$base-%d.jpg"
extract first image from all .avi movies, videos using ffmpeg.
sfk sel -since 30m . .cpp .hpp +run -printcmd "rm $path/$base.o"
delete all object files of source codes changed in the last 30 minutes
sfk echo -lines 100 101 102 +run "showstatus.bat $text"
run showstatus.bat three times with the given numbers, e.g. local ip's.
sfk sel soundlib .wav -tomake "outdir\$base.mp3"+run "ffmpeg -i $qfile $qtarg"
for all .wav files within soundlib that have no, or an older, .mp3 file
within outdir, run command ffmpeg to convert from .wav to .mp3.
sfk -exectime run. "copy in.dat out.dat"
measure the time it takes to run a copy command.
sfk -var run "myprog.exe" -yes +tell "myprog rc: #(run.lastrc)"
run external program myprog.exe and tell it's return code.
Don't try to execute a full run statement in ONE GO. Almost certainly, something
will go wrong (wrong files selected, syntax error in the command itself), and you
end up with many wrong output files. Instead, use THREE STEPS:
1. find the correct file set, by some trial and error:sfk run "echo $quotfile" mydir
This will simply show all filenames from "mydir". no command is executed
on those files, so nothing bad is happening. almost certainly, you notice
that too many files are included. Maybe you have to add "-nosub" to exclude
subfolders, or add more details about your file selection, like:
sfk run "echo $quotfile" mydir .jpg .jpeg
which reduces the file set to just .jpg and .jpeg files within "mydir".
2. Replace "echo" by the actual command, still running in simulation mode.sfk run "copy $quotfile \"d:\pic\small_$base.jpg\"" mydir .jpg .jpeg
This simulates a copy of all images from mydir to d:\pic, prefixing their name
by "small_", and ensuring that all target file extensions are only ".jpg".
3. When you're satisfied with the simulation output, add "-yes".
Another example: list the methods of all .class files in a directory tree.
This time, we take a different approach, starting with "sfk list".
To list all .class files in the directory tree "pack", we say:
sfk list pack .class
This may result in an output like this:
Our goal is to turn these lines into commands of the form:
So how do we achieve this? First, we have to change the format of the
output lines, through adding a "filter" command:
sfk list pack .class +filter -rep /\/./ -rep /.class//
This replaces all slashes "\" by a dot ".", and strips off the ".class".
Now the resulting output is:
Finally, we pipe this into "run":
sfk list pack .class +filt -rep /\/./ -rep /.class// +run "javap $file"
The resulting output - a simulated preview - is now:
Finally, run the command again, this time adding "-yes":
sfk list pack .class +filt -rep /\/./ -rep /.class// +run "javap $file" -yes
Which will result in the interface listings of all classes.