Home > Software engineering >  File Directory Redirection with appended text
File Directory Redirection with appended text

Time:12-10

I have a directory of SQL Backup files that I need to generate a file that I can use to restore these to the SQL database. I can not just create a batch file and use the basic Dir /b pathToFiles > fileList.txt CMD. I need to append text before and after the file name. Please see example below

Directory Listing of Files

File-20211207-90121.bak
File-20211207-100456.trn
File-20211207-101457.trn
File-20211207-102457.trn

Redirected to a file with this format

RESTORE DATABASE [dbName] FROM DISK = 'C:\filePath\File-20211207-90121.bak' WITH NORECOVERY, REPLACE
RESTORE LOG [dbName] FROM DISK = 'C:\filePath\File-20211207-100456.trn' WITH NORECOVERY
RESTORE LOG [dbName] FROM DISK = 'C:\filePath\File-20211207-101457.trn' WITH NORECOVERY
RESTORE LOG [dbName] FROM DISK = 'C:\filePath\File-20211207-102457.trn' WITH NORECOVERY
RESTORE DATABASE [dbName] WITH RECOVERY

Although my example above is including the bak file, I would be happy with a solution that just produces a list just for the trn (Transaction files).

Any help would be greatly appreciated.

Joey

CodePudding user response:

this is just an example that works as you would need it.

@echo off

for /f "usebackq" %%a in (`dir /b *.trn`) do (

    echo what you like write before ^"%%~dpfa^" and after
)
  • Related