You take photos on your camera, they are named something similar to:
During your photoshoot, your memory card becomes full so you swap cards. After your photoshoot your new card has photos with these names:
You want to rename the second batch of files to be renumbered, starting from 0422 ending at 0562.
I'm not sure what other photographers do, but since I also wear an SQL hat, I simply created this proc:
IF OBJECT_ID('dbo.cameraFileRenumber') Is Not Null
DROP PROC dbo.cameraFileRenumber
CREATE PROC dbo.cameraFileRenumber
@Debug bit = 1
/* DATE AUTHOR REMARKS
1/1/17 PPaiva Initial creation.
Renumbers camera files with a numeric offset.
You do a photoshoot with a memory card. Photos are numbered as such:
DSC_0001.nef to DSC_0421.nef (421 files)
Your memory card fills up. You change memory cards, the new photos are now numbered:
DSC_0001.nef to DSC_0141.nef (141 files)
You have a problem - you can't put all the files into the same folder because they
start counting from the same base number. Therefore you have overlapping file names.
Solution: Rename the second set of files to start counting at 0422.
Desired end result: DSC_0001.nef to DSC_0562.nef (562 files)
'DSC_', 4, 421
cameraFileRenumber 'K:\Images\Nikon\2017\2017-12-17_Elizabeth\batch2', 'DSC_', 422
SET NOCOUNT ON
DECLARE @s varchar(1000)
SET @s = 'dir /b "' + @Path + '"'
CREATE TABLE #t (
ID int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
SET @Path = dbo.udfAddBackslashIfNec(@Path)
-- Get filenames into table
INSERT INTO #t (Filename)
Exec xp_cmdshell @s
WHERE Filename Is Null
IF @Debug = 1
CHARINDEX('.', Filename) Pos,
Left(Filename, CHARINDEX('.', Filename) - 1) FilenameNoExt,
Substring(Filename, CHARINDEX('.', Filename) + 1, 100 ) ExtOnly
SET FilenameNoExt = Left(Filename, CHARINDEX('.', Filename) - 1),
Extension = Substring(Filename, CHARINDEX('.', Filename) + 1, 100 )
SET sNumWithoutBase = Replace(FilenameNoExt, @Base, '')
SET NumWithoutBase = convert(int, sNumWithoutBase)
SET NewNumWithoutBase = NumWithoutBase + @OffsetAdd
SET NewFilename = @Base + dbo.udfPadLeft(NewNumWithoutBase, @NumDigits, '0') + '.' + Extension
SET RenameCommand = 'rename "' + @Path + Filename + '" "' + NewFilename + '"'
IF @Debug = 1
ORDER BY ID
DECLARE @MaxID int,
SELECT @MaxID = Max(ID),
@ID = 0
WHILE @ID < @MaxID
-- Get next ID
SELECT @ID = Min(ID)
WHERE ID > @ID
-- Get other attribs
SELECT @Cmd = RenameCommand
WHERE ID = @ID
IF @Debug = 0
Exec xp_cmdshell @cmd