Vidir: Difference between revisions

10,973 bytes added ,  3 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Raku}}: Bug fix, rename some variables minor tweaks)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 5 users not shown)
Line 42:
 
=={{header|Furor}}==
<syntaxhighlight lang="furor">
<lang Furor>
###sysinclude dir.uh
###define SEPARATOR 9
Line 115:
{ „neworiginalname” }
 
</syntaxhighlight>
</lang>
 
=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude args.uh
###sysinclude list.uh
###sysinclude str.uh
###sysinclude io.uh
###define SEPARATOR 9
#g argc 3 < { "." }{ 2 argv } sto mypath
@mypath 'd inv istrue { ."The given directory doesn't exist! Exited.\n" end }
6 '- pidstr sto pidstring
@mypath getdir { ."Cannot load the dir! Aborted.\n" end } sto mydir
mypath per // Ha nem per-jelre végződik, kiegészíti vele.
10 mem sto aktstr
10 mem sto aktfilename
10 mem sto neworiginalname
"dr" sto types
1 mem !maximize sto szep
@szep 0 SEPARATOR inv []
@mydir ~r @mydir ~d + sto elemszám
//@elemszám 300 [[mem]] sto tömb
@elemszám 1 [[mem]] sto tömb
zero index
types {~ @mydir {~?~} <-~ {{ #g
1 sto() aktstr~
@aktstr 0 {~?~} #k uppercase inv []
#s @szep sum aktstr
{{}} #g !(#s) !trim dup sum aktstr inv mem
@szep sum aktstr
@mydir {~?~} {{}} getfilename sum aktstr
@aktstr @index [[tömb]]=
#g ++() index
}}
~}
"/tmp/peridiredit_tempfile-" sto myfilename
#s
@pidstring sum myfilename
".txt" sum myfilename
@tömb @myfilename listtofile! // print the list into a temporary file
"EDITOR" env sto mycommand
" " sum mycommand
@myfilename sum mycommand
 
@mycommand shell // execute the command
 
@myfilename filetolist sto editedlist
//@editedlist [[print]]! end
 
tömb~ externalloop: {{ // Loop for the original list
#g {{}} @[[tömb]] ~ inv { {{<}} } // If empty lines occured...
zero foundflag
editedlist~ {{ // loop for the edited list
// searching for the originaltype and originalnumber:
#g
{{}} [[editedlist]][0] {{}}§externalloop [[tömb]][0] != { {{<}} }
SEPARATOR {{}} 1 [[editedlist]][_] sto uj
SEPARATOR {{}}§externalloop 1 [[tömb]][_] sto er
#s @er @uj != { @er inv mem @uj inv mem {{<}} }
 
SEPARATOR {{}} 2 [[editedlist]][_] -- sto uj // trim the NL char at the end of the string
SEPARATOR {{}}§externalloop 2 [[tömb]][_] sto er
#s @er @uj != {
aktfilename @mypath = @er sum aktfilename
neworiginalname @mypath = @uj sum neworiginalname
@aktfilename @neworiginalname rename
."Renamed : " @aktfilename printnl
." ==> " @neworiginalname printnl
}
@er inv mem @uj inv mem
one foundflag
}} // editedlist~
@foundflag inv { // Nincs meg a file vagy dir. Ekkor le kell törölni:
{{}} [[tömb]][0] 'D #g == { // directory
SEPARATOR {{}} 2 [[tömb]][_] sto er
aktfilename @mypath #s = @er sum aktfilename @er inv mem
@aktfilename rmdir
."Deleted : " @aktfilename sprintnl
{{<}}
}
{{}} [[tömb]][0] 'R #g == { // Regular file
SEPARATOR {{}} 2 [[tömb]][_] sto er
aktfilename @mypath #s = @er sum aktfilename @er inv mem
@aktfilename removefile
."Deleted : " @aktfilename sprintnl
{{<}}
}
}
}} // tömb~
@myfilename removefile
end
 
{ „er” }
{ „uj” }
{ „szep” }
{ „types” }
{ „index” }
{ „mydir” }
{ „tömb” }
{ „mypath” }
{ „aktstr” }
{ „elemszám” }
{ „pidstring” }
{ „myfilename” }
{ „mycommand” }
{ „editedlist” }
{ „foundflag” }
{ „aktfilename” }
{ „neworiginalname” }
 
</syntaxhighlight>
 
=={{header|Phix}}==
Should work on Windows and Linux. I have commented out all the actually destructive statements.
<syntaxhighlight lang="phix">-- demo/rosetta/vidir.exw
string directory = ".",
editor = iff(platform()=WINDOWS?"notepad":"gedit"),
workfile = "vidir.txt"
-- filter = ""
--bool recursive = false,
-- overwrite = false,
-- verbose = false -- ,... etc
 
procedure process_command_line()
sequence cl = command_line()[3..$]
if length(cl) then
-- I assume you can figure out how to deal with eg
-- {`-d`,`C:\Users\Pete\Documents`,`-e`,`notepad++`}
?{"command line arguments (ignored)",cl}
end if
end procedure
 
process_command_line()
sequence d = dir(directory)
if d=-1 then
crash("no such directory")
end if
d = d[3..$] -- (drop "." and "..")
integer fn = open(workfile,"w")
for i=1 to length(d) do
printf(fn,"%d: %s\n",{i,d[i][D_NAME]})
end for
close(fn)
{} = system_exec(editor&" "&workfile)
object lines = get_text(workfile,GT_LF_STRIPPED)
integer last = 0
if lines=-1 then
crash("error reading edited file")
end if
for i=1 to length(lines) do
sequence r = scanf(lines[i],"%d: %s")
if r={} then
crash("error parsing line")
end if
{{integer line, string name}} = r
for last=last+1 to line-1 do
printf(1,"delete_file(%s)\n",{d[last][D_NAME]})
if not file_exists(d[last][D_NAME]) then ?9/0 end if
-- if not delete_file(d[last][D_NAME]) then
-- crash("error deleting file")
-- end if
end for
string prev = d[line][D_NAME]
if prev!=name then
printf(1,"rename_file(%s,%s)\n",{prev,name})
if not file_exists(prev) then ?9/0 end if
if file_exists(name) then ?9/0 end if
-- if name[2]=':' and name[1]!=prev[1] then
-- if not move_file(prev,name) then
-- crash(error moving file")
-- end if
-- elsif not rename_file(prev,name) then
-- crash("error renaming file")
-- end if
end if
last = line
end for
?"done"
{} = wait_key()</syntaxhighlight>
 
=={{header|Raku}}==
Line 152 ⟶ 331:
Notice that all of the above operations will fail to apply if you lack sufficient permissions for the affected files or directories.
 
<syntaxhighlight lang="raku" perl6line>use Sort::Naturally;
use File::Temp;
 
Line 277 ⟶ 456:
}
}
}</langsyntaxhighlight>
 
=={{header|RPL}}==
This program, written in User RPL, can modify the name of one or more objects - variable or program, including itself - in the current directory, but cannot delete any of them: RPL "full-line" editor does not allow to identify which name(s) are removed from the list.
{{works with|HP|48G}}
≪ <span style="color:red">"VIDIR"
{ { "OBJ: " "?" 5 } }
{ }</span>
VARS <span style="color:red">1</span> →LIST DUP
INFORM VARS
→ newnames oldnames
≪ '''IF''' newnames SIZE oldnames SIZE == '''THEN'''
<span style="color:red">1</span> oldnames SIZE '''FOR''' j
oldnames j GET
DUP RCL
<span style="color:red">"'~tmp"</span> j + STR→ STO
PURGE
'''NEXT'''
<span style="color:red">1</span> oldnames SIZE '''FOR''' j
<span style="color:red">"'~tmp"</span> j + STR→
DUP RCL
newnames j GET STO
PURGE
'''NEXT'''
'''END'''
≫ ≫ '<span style="color:blue">VIDIR</span>' STO
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
Just a basic solution.
 
Although any directory can be used, only files (not sub-directories) can be processed and filtering is not supported.
 
As Wren-cli cannot currently run external processes such as text editors, we instead present the user with each file in turn and ask them to confirm whether it is to be left unchanged, the name is to be changed or the file is to be deleted. The maximum number of files to be processed has been limited to 999 because of this.
 
The amended file is then saved to disk and the changes processed, echoing name changes/deletions to the terminal if verbose is 'on'.
 
This has been written to work on Linux but should work on other platforms with minor changes.
<syntaxhighlight lang="wren">import "os" for Process
import "io" for Directory, File
import "./fmt" for Fmt
import "./ioutil" for FileUtil, Input
 
var args = Process.arguments
if (args.count > 2) {
System.print("Too many arguments - maximum is two.")
}
var verbose = false
var path = ""
if (args.count == 0) {
path = "./" // current directory
} else if (args.count == 1) {
if (args[0] == "--v" || args[0] == "--verbose") {
verbose = true
path = "./"
} else {
path = args[0]
}
} else if (args[0] == "--v" || args[0] == "--verbose") {
verbose = true
path = args[1]
} else {
System.print("First argument is invalid, must be --v(erbose)")
return
}
if (!Directory.exists(path)) {
System.print("Unable to find directory : %(path)")
return
}
 
// ignore sub-directories and other special files
if (!path.endsWith("/")) path = path + "/"
var fileNames = Directory.list(path).where { |f| File.exists(path + f) }.toList
if (fileNames.count == 0) {
System.print("There are no files in directory: %(path)")
return
} else if (fileNames.count > 999) {
System.print("There are too many files to process - maximum is 999.")
return
}
var origNames = [] // keep a list of the original file names and their index numbers
var ix = 1
File.create("vidir.txt") { |f|
for (fileName in fileNames) {
var ixs = Fmt.dz(3, ix) // 3 digits, zero filled
f.writeBytes(Fmt.swrite("$s $s\n", ixs, fileName))
origNames.add([ixs, fileName])
ix = ix + 1
}
}
 
// create a new file with amended details
File.create("vidir2.txt") { |f2|
var lines = FileUtil.readLines("vidir.txt")
for (line in lines) {
if (line == "") continue // get rid of any extraneous blank lines
System.print(line)
var action = Input.option(" (p)ass, (a)mend, (d)elete ? : ", "padPAD")
if (action == "p" || action == "P") {
f2.writeBytes(line + "\n")
continue
}
if (action == "d" || action == "D") {
continue
}
var name = Input.text(" Enter amended file name : ", 1)
f2.writeBytes(line[0..3] + name + "\n")
}
}
 
// change vidir2.txt to vidir.txt overwriting original file
FileUtil.move("vidir2.txt", "vidir.txt", true)
 
// process by first creating a map of the new names by index number
var newNames = {}
var lines = FileUtil.readLines("vidir.txt")
for (line in lines) {
if (line == "") continue
var split = line.split(" ")
newNames[split[0]] = split[1]
}
 
// now iterate through the origNames list and pass/amend/delete as appropriate
System.print()
if (verbose) System.print("The following changes are being made:")
for (origName in origNames) {
var ixs = origName[0]
var old = origName[1]
var new = newNames[ixs]
if (new == null) { // file to be deleted }
File.delete(path + old)
if (verbose) System.print(" Deleting '%(old)'")
} else if (new != old) { // file name to be changed
FileUtil.move(path + old, path + new, true)
if (verbose) System.print(" Changing '%(old)' to '%(new)'")
}
}
if (verbose) System.print("All changes have now been processed.")</syntaxhighlight>
 
{{out}}
For testing purposes I've created a sub-directory called 'test' off the current directory and placed four files in it: a.txt, b.txt, c.txt and d.txt
<pre>
$ wren vidir.wren --v test
001 a.txt
(p)ass, (a)mend, (d)elete ? : p
002 b.txt
(p)ass, (a)mend, (d)elete ? : a
Enter amended file name : bb.txt
003 c.txt
(p)ass, (a)mend, (d)elete ? : d
004 d.txt
(p)ass, (a)mend, (d)elete ? : a
Enter amended file name : dd.txt
 
The following changes are being made:
Changing 'b.txt' to 'bb.txt'
Deleting 'c.txt'
Changing 'd.txt' to 'dd.txt'
All changes have now been processed.
 
$ cat vidir.txt
001 a.txt
002 bb.txt
004 dd.txt
 
$ dir test
a.txt bb.txt dd.txt
</pre>
9,476

edits