Archive for 'file management'

dos batch copy directory contents with wild card

Sunday, 28 September 2014

If you want to use xcopy in a batchfile to mirror the contents of some other path with a wildcard expression, instead of dumping all the files into your current working directory, this may be what you need:

for /D %%I in (%1) do xcopy “%%I” “%%~nI” /S /I

If you think of the fully qualified pathname as a slash-delimited stack, the ~n operator pops the last element from it and uses it as the target parameter for xcopy. If you put this in a batchfile called, say, copys, you can use like so:

copys d:\some\network\path\backup*

and your current working directory will get the backup* directories and their contents.

renner - hacky little file renaming utility

Monday, 12 November 2007

Even in these days of graphical development environments, I often find myself needing to change the extensions of hundreds of files, or remove “_converted” from the end of a file name, or zero-pad the beginning of a file name so “001″ “2″ and “03″ will sort correctly.

Renner is a little perl script I hacked together to do all those tasks. It’s not pretty, but it’s gotten the job done for me for years. Mostly I’m putting it on the Internets so it’s easy for me to find it when I need it.

How to use it:

Summary Help

perl location [matchcriterion] action [utility]
S = a string
n = a number (of characters)
match criteria:
-mask S|-unmask S
-app S| -pre S| -unapp n| -unpre n
actions:filename substitutions:
-mask S1 -sub S2
-dig n1 n2
-help | -silent | -test | -v


Add .jpg to all non jpg files
perl /pathto/ . -unmask jpg$ -app .jpg

Match Criteria

$ - anchors a match to END of string
^ - anchors a match to START of string

-mask string
default: .
act only on filenames that match the string
example: perl . -mask .txt -app .bak
append .bak to any file in currect directory matching .txt
foo.txt -> foo.txt.bak
foo.txt.bak -> foo.txt.bak.txt
example: . -mask .txt$ -app .bak
append .bak to any file with extension .txt
foo.txt -> foo.txt.bak
foo.txt.bak -> foo.txt.bak
-unmask string
act only on filenames that do NOT match the string
example: . -unmask ^_ -app .txt
append .txt to any file in current directory that does NOT
begin with underscore
_foo -> foo
foo -> foo.txt
NOTE: -mask and -umask may not be used in the same operation.

Rename directories in addition to plain files.
NOTE: -dir and -r (recursion) may not be used in the same operation.

Add or Remove Strings from Filename

-app string
-pre string
append or prepend string to all matching filenames
example: . -app .bak
append .bak to all files in current directory
foo -> foo.bak
example: . -mask .gif$ -pre _
prepend all .gif files in current directory with underscore
foo.gif -> _foo.gif
foo.txt -> foo.txt

-unapp integer
-unpre integer
delete integer characters from start or end of filename.
example: . -mask .txt$ -unapp 4
delete extension from all txt files in current directory
foo.txt -> foo
example: . -unmask . -unapp 1
delete leading character from all files in current directory with
no extension
afoo -> foo

Perform Substitutions on Filename

-mask searchstring - sub replacestring
replace searchstring in filename with replacestring. searchstring
is required in this form.
example: -mask display -sub d
display1.jpg -> d1.jpg

-dig integer1 integer2
zeropad strings of integer1 consecutive digits to integer2 digits
example: -mask .txt$ -dig 2 3
23.txt -> 023.txt
123.txt -> 123.txt
To include a space in a mask, sub, or unmask, use $space


recursively traverse directories.
NOTE: -r and -dir (rename directories) may not be used in the same operation.


print this message

suppress messages

describe action but don’t actually rename files

print detailed messages