EzDevInfo.com

batch-rename interview questions

Top batch-rename frequently asked interview questions

How to add a prefix to all files and folders in a folder? (windows)

The following command only changes the name of the files but not the folders.

for %a in (*) do ren "%a" "00_%a"


Source: (StackOverflow)

Is there a way to batch rename files to lowercase?

I need a way to rename all files in folders and subfolders to lowercase.

I'd like to know if there is a way to do that using only windows (XP or 7)


Source: (StackOverflow)

Advertisements

rename file with zmv

I just discovered zmv this morning.

To test it i have created a few files.

test1.txt test2.txt test3.txt

I'm trying to rename them with zmv I'd like to get this output:

test-1.txt test-2.txt test-3.txt

when I try to use wildcards I get an error message:

zmv '(*)([0-9]{1,})(.txt)' '$1-$2$3'
zmv: error: number of wildcards in each pattern must match

Am i doing something wrong (answer has to be yes :) )


Source: (StackOverflow)

Windows XP: how do I add 00 in front of every file via command prompt?

Windows XP: how do I add 00 in front of every file via command prompt?

I tried REN * 00*.gif but it doesn't work.


Source: (StackOverflow)

Windows 7 command to add a leading zero to a batch of filenames

Having trouble getting the correct command syntax. We need to add a leading zero to a batch of files in a directory on a regular basis. I cannot download and install a software item to do so. The operating system is Windows-7. The length of the filenames varies. i.e. 000165-CityName1.pdf to 0000165-CityName1.pdf 000166-CityNameLonger2.pdf to 0000166-CityNameLonger2.pdf etc. Looking for a rename command that would work, can someone please suggest one, it would be appreciated. I have tried a half dozen without success.


Source: (StackOverflow)

recursive batch rename files

I have audio library that has artworks in every folder and the names are: Cover.PNG, Cover.png, Cover.JPEG, Cover.jpeg, Cover.JPG, Cover.jpg…

How to recursively find all those files and rename them all to cover.png, cover.jpg?

Looking for command line way to do it.


Source: (StackOverflow)

How to find a file and move it into the directory it was found in

I am trying to rename files in sub directories using xargs by running the following command:

find . -name oldfile.txt -print0 | xargs -n 1 -0 -I % mv % newfile.txt

The problem is that the file gets moved into the directory I run the command in instead of the directory that oldfile.txt is found in. I cant seem the solution for this, any ideas?

I'm thinking that perhaps I can get the directory from the found file's path and then append it to the front of the second paramater of mv but how would that work?


Source: (StackOverflow)

Linux GUI Regular Expression File Renamer (with substitution)

I have folder with several hundred files named like this:

010203.txt

I want to rearrange the file names so that last two digits are moved to the front like this:

030102.txt

I want to avoid writing a script.

Instead, I'm looking for a "linux gui regular expression file renamer" that can recursively evaluate all files in a folder (and its sub-folders).

I want the ability to specify a regular expression for matching a file:

(\d\d)(\d\d)(\d\d)(.*)

And I want the ability to perform regular expression substitution to rename the file:

$3$1$2$4

Any suggestion?


Source: (StackOverflow)

Add folder name to beginning of filename

I have a directory structure as below:

Folder
  > SubFolder1
    > FileName1.abc
    > Filename2.abc
    > .............

  > SubFolder2
    > FileName11.abc
    > Filename12.abc
    > ..............

  > ..........

etc. I want to rename the files inside the subfolders as:

SubFolder1_Filename1.abc
SubFolder1_Filename2.abc
SubFolder2_Filename11.abc
SubFolder2_Filename12.abc

i.e. add the folder name at the beginning of the file name with the delimiter "_". The directory structure should remain unchanged. Note: Beginning of file name is same. e.g. in above case File*.

I made below Script


for /r "PATH" %%G in (.) do (
  pushd %%G
  for %%* in (.) do  set MyDir=%%~n* 
  FOR %%v IN (File*.*) DO REN %%v  "%MyDir%_%%v" 
  popd
  ) 

Problem with the above script is that it is taking only one Subfolder name and placing it to the beginning of file name irrespective of the folder.


Source: (StackOverflow)

How do I remove non-ascii characters from filenames?

I have several files with names containing various Unicode characters. I'd like to rename them to only contain the "printable" ASCII characters (32-126).

E.g,

Läsmig.txt         //Before
L_smig.txt         //After
Mike’s Project.zip 
Mike_s Project.zip 

Or for bonus points, transcribe to the closest character

Läsmig.txt
Lasmig.txt
Mike’s Project.zip
Mike's Project.zip

Ideally looking for an answer that doesn't require 3rd party tools. (Edit: Scripts encouraged; I'm just trying to avoid niche shareware apps that need to be installed to work)


Power shell snippet that finds the files I'm interested in renaming:

gci -recurse | where {$_.Name -match "[^\u0020-\u007E]"}

Unanswered similar python question - http://stackoverflow.com/questions/17870055/how-to-rename-a-file-with-non-ascii-character-encoding-to-ascii


Source: (StackOverflow)

Linux: Howto get same output filenames of batch conversion with different extension?

I am using Timidity to convert a lot of midi files to mp3. I was wondering if there is a standard way of specifying a output pattern so that it converts all the *.mid files to /path/*.wav

I ask this in general because it would also be the case in a mv command. Or is this tied to a specific implementation in the app?

So, how to move a *.mid files to *.wav ?


Source: (StackOverflow)

Increment file number using zmv

I have a set of files that are named in the vein of "some_file-nameN", where N is a number starting at 1, incrementing, and ending somewhere between 10 and 30. I now need to insert a file at the start. How can I increment the number of the file using ZMV and ZSH?


Source: (StackOverflow)

How can I bulk move and rename images/videos in a folder

All my mobile device's photos and videos are backed up to the cloud and synchronized to my laptop using Dropbox's "Camera Upload" feature. All the photos and videos get added to the "camera uploads" folder in the Dropbox folder.

I am wanting to move all of these to my Photos and Videos folders within my user folder. I am using Windows 8 but this question could also be for Windows 7.

Ideally I want photos moved to the My Pictures folder. The photo should be in a folder named the date and that folder should be in a folder named as the year. For example, if a photo was taken on October 4, 2013 it should be in My Pictures/2013/2013-10-04/

The photo itself should be renamed to the date and time the photo was taken and optionally followed by the original filename. For example if a photo was taken at 14:05:07 on October 4, 2013 and was called IMG003.jpg it would be in My Pictures/2013/2013-10-04/2013-10-04 14.05.07 IMG003.jpg

Videos would work the same but would be put in the My Videos folder. For example, a video taken at 17:03:01 on October 1, 2013 would be moved to My Videos/2013/2013-10-01/2013-10-01 17.03.01 VIDEO003.mpg

Is there an app that I could use to automate this process or could it be done using a batch file?


Source: (StackOverflow)

Bulk rename files to name of parent folder

I have a large (aprox. 150,000) tiff files which all have the same filename. They are unique because of the directory structure they are held in.

I would like to bulk rename the tiff files so they become unique, based on the directory structre that they are held within.

Does anyone have any method of acheiving this?

I am using Windows Server 2012 so a solution using a cmd script, batch file or windows GUI tool would be perfect.

Ideally, this is what I would like to acheieve, but if I have to have more or all of the directory structure in the final filename thsi would still be very, very helpful.

C:\A_001\B_0001\ABC\0001.tif -> ABC.tif

C:\A_001\B_0001\JKL:\0001.tif -> JKL.tif

C:\A_001\B_0001\XYZ\0001.tif -> XYZ.tif

C:\A_001\B_0002\123\0001.tif -> 123.tif

C:\A_001\B_0002\456\0001.tif -> 456.tif

C:\A_001\B_0002\789\0001.tif -> 789.tif


Source: (StackOverflow)

How do I signify repetition in a ZMV regex?

I'm trying to manually pull the digests off of Rails assets (don't ask). I was directed to ZMV for easy regex-based find/replace. But the normal {32} syntax for specifying an amount of repetition does not work:

$ zmv -n '(**/)(*)' '$1${2//-[A-Za-z0-9]\{32\}/}'

I've tried some other formats. This, for example, works, but is too greedy (it will turn image-3.png into image.png, for example):

$ zmv -n '(**/)(*)' '$1${2//-[A-Za-z0-9]##\./.}'

That double-hash syntax only showed up after a lot of Googling (I'd have expected +). But I cannot for the life of me find how to make {32} work. I tried #32#? Which appeared to work, but that's because it was reading it as (in my eyes) ?32? and that means it met anything that had a three in the digest or last character.

How do I signify character repetition in zmv?

EDIT:

Apparently it would help some to view filenames I'm trying to match? To be clear: my question is "how do I signify character repetition in zmv" not "how do I match these filenames" (a question I know the answer to in standard RegEx format). If it helps, here is my intended before and after:

directory/asset-jej4jtifne9bjkkeuwr09rewrewlur23.css
another-directory/style-748reiodlpqwerntaerwerwerexfzsdf.js.gz
directory/subdirectory/this-is-a-thing-qwertyuiopasdfghjklzxcvbnm123456.js
third-directory/should-not-match-3.css

Should become:

directory/asset.css
another-directory/style.js.gz
directory/subdirectory/this-is-a-thing.js
third-directory/should-not-match-3.css

SECOND EDIT:

Because I needed to do this yesterday, I did it the long way and (as expected) it worked. I'd still like to know how to avoid it in the future. Here's the command I ended up using (I repeated my character matcher 32 times explicitly):

$ zmv '(***/)(*)' '$1${2//-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]/}'

THIRD EDIT:

For the record, I'm using zsh on OS X. I'd imagine zmv is the same across platforms, but I couldn't say for sure.


Source: (StackOverflow)