Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl script to rename files with spaces in name, pushd/popd equivalent?
    primarykey
    data
    text
    <p>My Linux system mounts some Samba shares, and some files are deposited by Windows users. The names of these files sometimes contain spaces and other undesirable characters. Changing these characters to hyphens <code>-</code> seems like a reasonable solution. Nothing else needs to be changed to handle these cleaned file names.</p> <p>A couple of questions,</p> <ul> <li><p>What other characters besides spaces, parenthesis should be translated?</p></li> <li><p>What other file attributes (besides file type (file/dir) and permissions) should be checked?</p></li> <li><p>Does Perl offer a <code>pushd</code>/<code>popd</code> equivalent, or is <code>chdir</code> a reasonable solution to traversing the directory tree?</p></li> </ul> <p>This is my Perl program</p> <pre><code>#!/bin/env perl use strict; use warnings; use File::Copy; #rename files, map characters (not allowed) to allowed characters #map [\s\(\)] to "-" my $verbose = 2; my $pat = "[\\s\\(\\)]"; sub clean { my ($name) = @_; my $name2 = $name; $name2 =~ s/$pat/\-/g; #skip when unchanged, collision return $name if (($name eq $name2) || -e $name2); #skip collisions print "r: $name\n" if ($verbose &gt; 2); rename($name, $name2); $name2; } sub pDir { my ($obj) = @_; return if (!-d $obj); return if (!opendir(DIR, $obj)); print "p: $obj/\n" if ($verbose &gt; 2); chdir($obj); foreach my $dir (readdir DIR) { next if ($dir =~ /^\.\.?$/); #skip ./, ../ pDir(clean($dir)); } close(DIR); chdir(".."); } sub main { foreach my $argv (@ARGV) { print "$argv/\n" if ($verbose &gt; 3); $argv = clean($argv); if (-d $argv) { pDir($argv); } } } &amp;main(); </code></pre> <p><em>These posts are related, but don't really address my questions,</em></p> <ul> <li><em>Use quotes: <a href="https://stackoverflow.com/questions/4285989/in-perl-how-to-handle-filenames-with-spaces">How to handle filenames with spaces?</a> (using other scripts, prefer removing need for quotes)</em></li> <li><em>File::Find <a href="https://stackoverflow.com/questions/5241692/perl-script-to-recursively-list-all-filename-in-directroy">perl script to recursively list all filename in directory</a> (yes, but I have other reasons)</em></li> <li><em>URL escaping: <a href="https://stackoverflow.com/questions/18241508/modifying-a-perl-script-which-has-an-error-handling-spaces-in-files">Modifying a Perl script which has an error handling spaces in files</a> (not urls)</em></li> <li><em>Quotemeta: <a href="https://stackoverflow.com/questions/1267669/how-can-i-safely-pass-a-filename-with-spaces-to-an-external-command-in-perl">How can I safely pass a filename with spaces to an external command in Perl?</a> (not urls)</em></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload