Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: How to Rename files in multiple directories with a Perl script using an array of hashes.

by ObiPanda (Acolyte)
on Jun 11, 2023 at 17:40 UTC ( [id://11152780]=note: print w/replies, xml ) Need Help??


in reply to Re: How to Rename files in multiple directories with a Perl script using an array of hashes.
in thread How to Rename files in multiple directories with a Perl script using an array of hashes.

I followed your suggestion of dumping the path array and discovered a slight issue with my code. One problem occurred when the directories (and perhaps files) had a space in the naming. I added a single parenthesis on each side and this seemed to fix the problem. The reason I didn't do this before was because I thought single quotes may render my variable a literal. The modification is  my @path = glob("'$Subscriptions_Path/$_->{Lib_Sub_Path}/*'");.

glob might be my friend after all, I just wasn't treating it right. haha. The path array seems to be picking up the files in each of the directories now. So I guess it's the rename that isn't happy with what I'm passing it.

Could rename have an issue with the file length? Here's why I ask.

Going to your second suggestion, I slightly modified the working test code to account for spaces in the directory name. I'll provide the code and then the output, which appears to be perfectly correct here. (Bear in mind, this script is being run from my terminal in the VM but the script and the files and folders are all located on the NTFS file system which is shared with the VM.)

#!/usr/bin/env perl use strict; use warnings; use autodie; use File::Find; use File::Rename; use Data::Dumper; # VARIABLES # Subscriptions Library & Archive Path variables for clarity my $Subscriptions_Path = "/mnt/hgfs"; # Subscription DATA sets my @Subscription = ( { Sub_Name => "T 1", Lib_Sub_Path => "VM-Share", Keep_Duration => 0, }, { Sub_Name => "T 2", Lib_Sub_Path => "VM-Share/xtr", Keep_Duration => 0, }, { Sub_Name => "T 3", Lib_Sub_Path => "VM-Share/xtr 2", Keep_Duration => 0, } ); for (@Subscription) { print "Beginning Subscription Service for $_->{Sub_Name} \n"; print "The absolute library path is: $Subscriptions_Path/$_->{Lib +_Sub_Path}\n"; my @path = glob("'$Subscriptions_Path/$_->{Lib_Sub_Path}/*'"); print Dumper(\@path); print "\n"; if (@path) { File::Rename::rename(\@path,{ _code => sub { s/_/ / }, verbose => 1, no_action => 1, } ); }; print "Completing Subscription Service for $_->{Sub_Name} \n\n"; }
Beginning Subscription Service for T 1 The absolute library path is: /mnt/hgfs/VM-Share $VAR1 = [ '/mnt/hgfs/VM-Share/2023-05-23.json', '/mnt/hgfs/VM-Share/Hello my Pony.txt', '/mnt/hgfs/VM-Share/New folder', '/mnt/hgfs/VM-Share/New Text Document.txt', '/mnt/hgfs/VM-Share/pia-linux-3.3.1-06924.run', '/mnt/hgfs/VM-Share/sub_test.pl', '/mnt/hgfs/VM-Share/Subscriptions', '/mnt/hgfs/VM-Share/subscriptions.pl', '/mnt/hgfs/VM-Share/test_rename.pl', '/mnt/hgfs/VM-Share/Torrents', '/mnt/hgfs/VM-Share/UK stopping covid primary vaccinations [ +bpzd74_gZJk].webm', '/mnt/hgfs/VM-Share/xtr', '/mnt/hgfs/VM-Share/xtr 2' ]; rename(/mnt/hgfs/VM-Share/sub_test.pl, /mnt/hgfs/VM-Share/sub test.pl) rename(/mnt/hgfs/VM-Share/test_rename.pl, /mnt/hgfs/VM-Share/test rena +me.pl) rename(/mnt/hgfs/VM-Share/UK stopping covid primary vaccinations [bpzd +74_gZJk].webm, /mnt/hgfs/VM-Share/UK stopping covid primary vaccinati +ons [bpzd74 gZJk].webm) Completing Subscription Service for T 1 Beginning Subscription Service for T 2 The absolute library path is: /mnt/hgfs/VM-Share/xtr $VAR1 = [ '/mnt/hgfs/VM-Share/xtr/test_rename.pl' ]; rename(/mnt/hgfs/VM-Share/xtr/test_rename.pl, /mnt/hgfs/VM-Share/xtr/t +est rename.pl) Completing Subscription Service for T 2 Beginning Subscription Service for T 3 The absolute library path is: /mnt/hgfs/VM-Share/xtr 2 $VAR1 = [ '/mnt/hgfs/VM-Share/xtr 2/Dogs are our_enemies.txt' ]; rename(/mnt/hgfs/VM-Share/xtr 2/Dogs are our_enemies.txt, /mnt/hgfs/VM +-Share/xtr 2/Dogs are our enemies.txt) Completing Subscription Service for T 3
  • Comment on Re^2: How to Rename files in multiple directories with a Perl script using an array of hashes.
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: How to Rename files in multiple directories with a Perl script using an array of hashes.
by choroba (Cardinal) on Jun 11, 2023 at 21:41 UTC
    Using single quotes might solve some problems with spaces: but if the path contained a single quote, it would break again. As written in the documentation of glob, use bsd_glob from File::Glob if you don't want whitespace to be considered a separator.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11152780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2025-06-20 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.