Renaming files using hash table in perl -


I have created a Perl code that is shown below. What I'm trying to do here is to get input from the text file that contains a HTTP URL with a title. Thus the first regex is the title and the second regex receives the ID from within the url. All of these values ​​are entered in the hash table% myfilenames ().

Then there is the key as the URL ID in this hash table, and the title is in the form of the title. Even everything works fine, now I have a set of files on my computer, which has an ID, which we have removed from the URL.

What I want to do is that if the ID hash table, then the name of the files should be changed into the assigned value. Now the output is correct on the print statement in the last function, but I am unable to rename the file. I tried many things, but nothing worked. Someone can help please.

Example Content:

  URL: https://abc.com/789012 & lt; --- ID value (new title): ABC file on file = file-789012 & lt; ---- ID new file name = abc   

My code:

  Use the file: Slurp; Use the file: copy qw (move); Open (F, 'and Lieutenant; Hlop. Txt'); $ Key = ''; $ Value = ''; % Myfilenames = (); Forex Currency ( F & gt;) {if ($ _ = ~ / lecture /) {$ value = $ _; } If ($ _ = ~ / https /) {if ($ _ = ~ / \ d {6} /) {$ key = $ & amp; }} If (! ($ Value eq '' $ $ key eq '')) {#print "$ key \ t \ t $ value"; $ Myfilenames {$ key} = $ value; $ Key = ''; $ Value = ''; }} #while (my ($ k, $ v) = every% myfilenames) {print "$ k $ v \ n"; } My @files = read_dir 'C: \\ inputfolder'; (@files) {if ($ _ = ~ / \ d {6} /) {$ oldval = $ & amp;; } $ Newval = $ myfilenames {$ oldval}; Chomp ($ newVal); Print $ _, "\ t \ t $ nave" "\ N"; $ Key = ''; }    

You probably did not fill the path for file names. The following works for me (on the Linux box):

  #! Use / usr / bin / perl warnings; Strict use; Use the file: Slurp qw {read_dir}; My $ dir = 0; Mkdir $ dir; Open "$ dir / $ _" for your $ FH, '& gt;', 123456, 234567; My $ key = my $ value = q (); My% myfilenames = (); (& Lt; data & gt;) for {chomp; $ Value = $ _ if / lecture /; $ = $ 1 if / https / and / (\ d {6}) /; If ($ value ne q () and $ key ne q ()) {$ myfilenames {$ key} = $ value; $ Key = $ value = q (); }} My @files = read_dir ($ dir); (@files) {if (/ (\ d {6}) /) {my $ oldval = $ 1; My $ new = $ myfilenames {$ oldval}; Rename "$ Dir / $ oldval", "$ dir / $ newval"; }} __DATA__ Lecture A1 https: // 123456 # Comment Lecture A2 https: // 234567    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -