in reply to this script normaly deplace files in a directory !
the second backslash is being interpreted as an escape for the final single-quote character, which means that there really is no close-quote on this line, and this is why it won't compile. (You did remember to use double backslashes elsewhere, at lines 16 and 17, but forward slashes for file paths is still a better way to go.$targetDirFull = 'C:\target\';
This bit is strange:
This says that if you are able to do "chdir('C:/')", you do it a second time. Then if there is no file or directory named "multimedia" (this is the value assigned to $targetDir), you create a directory called "Target". (Huh?) You don't check whether the mkdir succeeds.unless( chdir('C:\\') ) { print "Error 1..\n" ;} else { chdir('C:\\'); } unless (-e $targetDir) { print "creating directory \"Target\"...\n"; mkdir('Target',0777); }
In this first use of readdir, you don't keep whatever file name was returned:
When you read the rest of the directory entries into @allThings and loop through them, you don't check to see which ones are plain files, and which are subdirectories. (Trying to open and the diamond read operator on directories won't work.) Then for each file you try to process, you do another opendir on the output directory, which is quite unnecessary -- you don't even need to do that once.unless(readdir(ORIG)) { print "Error 2"; ## not a very informative report! } else { ...
In addition to File::Spec, I suggest you look up File::Copy as well -- it will help simplify what you are doing. Also try using the "-f" or "-d" file checks (these are like the "-e" check that you are already using) as you go through the entries in the input directory, and only deal with things that are data files.
Other advice (mostly stylistic): declare and initialize variables where they are needed, rather than putting a bunch of "my" declarations at the top; use "if" more than "unless"; see if you can use spaces instead of tabs when indenting lines (at least when you post code here), so the indents will appear as intended.
|
|---|