Get the App
SLTechnology News&Howtos  ›  Database  › 

How to rename files in batch in SQLServer database

Shulou Source: shulou.com Published: 2022-05-31 19:12:38 09月30日 Update

In order to solve the problem of how to rename files in batch in SQLServer database, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

How to rename files in batch by SQLServer database

1. Create a new custom function: intercept a string starting with the last specific character

Createfunctionfn_GetLastCharIndex (@ Charsvarchar (1000), @ Charvarchar (10) returnsvarchar (1000) begindeclare@iintset@i=CharIndex (@ Char,@Chars) while (@ I > 0) beginset@Chars=Substring (@ Chars,@i+1,len (@ Chars)) set@i=CharIndex (@ Char,@Chars) endreturn@Charsend

two。 Get a list of file names

Ifexists (select*fromtempdb..sysobjectswhereid=object_id ('tempdb..#temp')) droptable#tempcreatetable#temp (filenamesvarchar (1000)) Insertinto#temp (filenames) Execmaster..xp_cmdshell'dirF:\ slam dunk-full color version of the National Competition'

How to rename files in batch by SQLServer database

3. Rename

Declare@FileNamevarchar (1000), @ NewFileNamevarchar (1000), @ svarchar (1000) declarecur_1cursorforselectdbo.fn_GetLastCharIndex (filenames,'') FileNamefrom#tempwhereRight (filenames,4) in ('.jpg') andfilenameslike'%slamdunk%'orderby1opencur_1fetchcur_1into@FileNamewhile (@ @ fetch_status=0) beginset@NewFileName=Replace (@ FileName,dbo.fn_GetLastCharIndex (@ FileName,'-'), Right ('00'+dbo.fn_GetLastCharIndex (@ FileName,'-')) 7) set@s='renF:\ slam dunk-National Competition full color version\'+ @ FileName+''+@NewFileNameExecmaster..xp_cmdshell@s,no_outputfetchcur_1into@FileNameenddeallocatecur_1

This is the answer to the question about how to rename files in batch in the SQLServer database. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: File data database question national full color character more dunk slam dunk master master contest help solution easy easy content function string boy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple MariaDB Shulou Technology Docker