Copy all files and renaming them

Hi
My SQL script needs to copy all backup files in a folder to another one while renaming them at the same time by adding the time stamp like this: Originalfilename[Timestamp].bak
Basically the script looks like this:
declare
@cmdstring varchar(1000)
declare
@filenamestr varchar(100)
set
@filenamestr =
CURRENT_TIMESTAMP
set
@cmdstring =
'copy \\Webserver\BackupStorage\SQLbackup\*.*
\\Webserver\BackupStorage\SQLbackupArchive\'
exec
master..xp_cmdshell
@cmdstring – this part is not complete
What code needs to be added to rename each file during the copy process?
Patrick

SET NOCOUNT ON;
declare @sourcepath varchar(1000) = 'C:\temp\',
  @destinationpath varchar(1000) = 'C:\MyFiles\'
 CREATE TABLE #FileList
     FileID INT IDENTITY(1, 1)
    ,Line VARCHAR(512)
 CREATE TABLE #temp
     isFileThere BIT
    ,isDirectory BIT
    ,parentDirExists BIT
    CREATE TABLE #output
  result varchar(500)
 DECLARE @Command VARCHAR(1024)
 ,  @RowCount INT
 ,  @counter INT
 ,  @FileName VARCHAR(1024)
 ,  @FileExists BIT
 SET @Command = 'dir '+@sourcepath+'  /A-D  /B'
 INSERT #FileList
        EXEC master.dbo.xp_cmdshell @Command
 DELETE FROM #FileList
 WHERE  Line IS NULL
 SELECT @RowCount = COUNT(*)
 FROM   [#FileList]
 SET @counter = 1
 WHILE ( @counter <= @RowCount )
    BEGIN
        SELECT  @FileName = [Line]
        FROM    [#FileList]
        WHERE   [FileID] = @counter
        SET @Command = 'copy /-Y
'+@sourcepath+'*.txt '+@destinationpath
  insert into #output
     EXEC master.dbo.xp_cmdshell @Command
     SET @Command = 'REN
'+@destinationpath+''+ @FileName +' '+ LEFT(@FileName,CHARINDEX('.',@FileName)-1)+'_rename.'+SUBSTRING(@FileName,charindex('.',@Filename)+1,len(@FileName))
     print @command
        SET @counter = @counter + 1
        IF LEN(@Command) > 0
            EXEC master.dbo.xp_cmdshell @Command
    END
 DROP TABLE #output
 DROP TABLE #FileList
 DROP TABLE [#temp]

Similar Messages

  • I have a pdf file which contains text if i copy all text and paste them into notpad and save file as .txt Is it change the ascii value of text ?

    I have a pdf file which contains text if i copy all text and paste them into notpad and save file as .txt Is it change the ascii value of text ?

    How to know character's encoding is ASCII?

  • I accidentally deleted all of my Logic Pro X files and put them in my trash but now that I copied them back to my documents they all play in the wrong places in my projects

    I accidentally deleted all of my Logic Pro X files and put them in my trash but now that I copied them back to my documents they all play in the wrong places in my projects

    Buddhaguru wrote:
    I know that Mac's don't have a roll back like PC's
    They do. It's called Time Machine.
    You need to reinstall all of your Apps. That should replace all of the support files you nuked.

  • Selecting all files from JFilechooser and renaming them??

    dear all
    i am working on a application that selects the jpeg,jpg,jpe files form the jfilechooser with an additonal botton rename which calls the rename method and the selected file is renamed.
    i have been succssful in mselecting one file and renaming it.but the problem is with...
    1) Selection of multiple files and sending their names and parent directories getParent(); is not working with taht....should i use arrays to store that .....but how when i have enabled multiple selection already.
    2)SEcond problem is to refersh the contents of JFileChooser after i have renamed it.iwant that it shopuld be refreshed automatically just after renaming has been done.
    help is always appreciated..
    regards
    s m sharma (trainee s/w/ engg)

    hi everybody
    i have done it myself.......it was not too tough that u didnot responed...anyway thanks..
    regards
    s m sharmna

  • Renaming MXF files and pulling them out of their file structure

    My company's workflow necessitates that we pull mxf files out of the file structure and rename them. I know this is a big no-no, but with CalibratedMXF it is no problem.
    My proposed workflow was to use Prelude to locate the mxf files from where they are backed up on my drive, ingest them, copy them to a new location, and rename them.
    In attempting this process, however, I am realizing that Prelude will not pull mxf files out of their file structure. It copies the entire mxf folder structure to the new location, and renames the media only in Prelude (the clip name, not the filename).
    The only way to get Prelude to rename and discard with folder structure upon copy to new location is to check the 'Transcode' box and transcode the media.
    I want to work natively in mxf, I do not wish to transcode or rewrap.
    My workaround was to transcode the files to the exact same setting: MXF Op1a with preset 'Match Source (rewrap)'
    However this process simply forces Prelude to crash.
    Any suggestions on how I can batch copy and rename mxf files, instead of going through each level of folders for each clip and dragging the file out and manually renaming one by one?
    Prelude CC
    OS X 10.9.5
    3.5 GHz Intel Core i7
    files are C300 MXF Op1a MPEG-2

    Hi Kaye,
    Sorry if I missed this in your description of the problem, but what device is generating the MXF files you are attempting to rewrap? Also, when Prelude crashes, which it should not do obviously, does the crash reporter launch? If so, can you copy or attach the report into a reply?
    Thanks!
    Michael

  • My iWeb image names are incompatible with FTP servers and must be renamed but I can't find the images in iWeb. I can upload to a desktop file and change them there but then I would need to download them to a new site in iWeb to publish them.

    my iWeb image names are incompatible with FTP servers and must be renamed but I can't find the images in iWeb. I can upload to a desktop file and change them there but then I would need to download them to a new site in iWeb to publish them. 
    What's the answer?
    Thanks 

    Many thanks to  responded to my question and sorry not to get back sooner.
    Yes, I discovered by this unfortunate experience that the names of photos are a serious issue if the site is to ever go to an FTP server.  Apple should discuss this carefully in their "manual".
    One more question:  I uploaded the website to a folder on my desktop. When I open it, unlike the photos in iWeb, there are folders for each page which have all the photos in them. I went through each folder and changed the names to be FTP compatible. I'm using iWeb SEO Tool which allows one to upload from this local folder to an FTP server but having trouble.
    Is this because there is an imbedment in iWeb that uses the old photo names to link to the actual photos and it can't find them because of the name changes?
    Thanks again for your help ...
    Phil McP

  • When I try to same an image under the command 'save as' it will only let me save as file types 'firefox document' or 'all files' and when I try to look at them later they dont work. How can I save an image as the same file type it is on the web site?!

    When I try to save an image under the command 'save as' it will only let me save as file types 'firefox document' or 'all files' and when I try to look at them later they dont work. How can I save an image as the same file type it is on the web site?!
    == This happened ==
    Every time Firefox opened
    == I updated to one of the firefox versions (Not sure which one it was)

    Thanks Alex, but sadly I already tried that. Neither .docx or .xlsx files show up in the content list. They both show as a Chrome HTML document so changing how Firefox addresses those doesn't help since it thinks its the same type of file. I don't think I can manually add files into the "Content Type" left side nav.

  • Using Terminal to copy all files (hidden and not hidden) and folders from one place to another

    How -using Terminal- can I copy all files (hidden and not hidden) and folders (and their contents) from one place to another

    ditto
    DITTO(1)                  BSD General Commands Manual                 DITTO(1)
    NAME
         ditto -- copy directory hierarchies, create and extract archives
    SYNOPSIS
         ditto [-v] [-V] [-X] [<options>] src ... dst_directory
         ditto [-v] [-V] [<options>] src_file dst_file
         ditto -c [-z | -j | -k] [-v] [-V] [-X] [<options>] src dst_archive
         ditto -x [-z | -j | -k] [-v] [-V] [<options>] src_archive ...
               dst_directory
         ditto -h | --help
    DESCRIPTION
         In its first form, ditto copies one or more source files or directories
         to a destination directory.  If the destination directory does not exist
         it will be created before the first source is copied.  If the destination
         directory already exists then the source directories are merged with the
         previous contents of the destination.

  • Can't copy all files from a CD-R

    I have a number of CD-Rs of family photos.  I made the disks on a Dell PC with a horizontally mounted CD-RW Drive in 2005.  I recently tried to load them onto my iMAC, but I could only copy about half of the files before the drive hung-up.  The drive would often make noise and then slow to a near inaudible crawl and then say the file(s) could not be copied.
    I was able to copy the missing files to a memory stick at a local library on an HP PC with a horizontally mounted drive, so the disks are OK.  Interestingly, I had trouble with the disks at a library with iMAC-like HP PCs, i.e., all-in-one monitor/pc/optical-drive with a vertically mounted drive.
    Other drive-related problems: (1) friends have trouble playing Audio CD-Rs I've made for them, (2) I can't copy a DVD-R with MackTheRipper (PowerPC), the DVD-R plays fine.
    Is this a drive issue, a drive orientation issue, or something else?
    Thanks for your help.

    Hi all.  I am trying to copy all files from a flash: on a CME to a
    usbflash drive.  I am able to do so, but only one at a time.  Is there
    a away to copy all files at one time so I dont have to name them
    individually when copying all of them over?  Thanks.
    Check out the below link hope to help !!
    https://cisco.com/en/US/products/hw/routers/ps233/products_tech_note09186a00800a6744.shtml
    Ganesh.H

  • Upon creating a new catalog, all files are renamed with a postfix: "-2"?

    My catalog was corrupted, and i decided to create a new catalog from cratch.
    To my big surprise alle falenames was renamed from IMG_xxxx.cr2 to IMG_xxxx-2.cr2. Why?
    This is i problem sice i still god the old catalog, and try to recover some of the old editorial settings, but since alle files are renamed the are no longer visible in the old catalog. just about 15.000 files are renamed!

    Lightroom only adds suffixes when it finds files of the same name in a location it's moving or copying too.
    What option did you use on Import? Copy/Add/Move?

  • Can I delete all files and users (including admin account) without re-installing SL?

    I have a 1st generation Macbook Air, running Snow Leopard 10.6.8.  Just bought myself a new MBA, want to sell the old one (privately, not soliciting here).
    The original install disks were Leopard.  I bought a family pack SL and upgraded the MBA at that point.  Earlier this year I sold my old Mac Mini (when I upgraded) and included the SL install disks with that sale (stupid of me).
    Now I want to sell the old MBA.  Ideally, I'd have a clean install on SL on it, minus all previous files and accounts.  I've done that before when selling old computers, reformatted / erased the HD, then reinstalled the OS.  But I don't have the SL disk anymore, and I don't really want to pay for it just to prep this notebook for sale.
    AFAICT, my options are:
    wipe the HD and reinstall Leopard (the only OSX disks I have).  Whoever buys it from me would need to upgrade to SL themselves, and from there to Lion if they want.
    somehow try to delete all files and users without touching the current install of SL.  I'm not terribly worried about security of what little information is on the MBA (nothing important to me), so I don't mind trashing files and then emptying trash instead of wiping the HD block by block. 
    Any ideas for how to do #2?  I can trash all files/folders under /user, uninstall all applications and delete their folders from Library, uninstall all browsers except Safari and wipe the history from that...
    Is there other stuff I should be deleting?  plist files, maybe?  I'm leery of messing around too much and disrupting the install of the underlying OS.
    Can I also delete the admin account (or rename it "admin" with a generic default password?)?
    Any other suggestions?  Thanks.

    Trashing files you have created is relatively easy. Getting rid of an account is a bit harder. Trashing the .plist files, etc., that may contain personal info is a lot harder, and there is always the risk that you will miss something.
    Best, and easiest, and customary, is to do option #1 - wipe the drive and re-install the original OS from the original disk set. The receiver of the machine can then decide which OS they want to upgrade it to, and purchse accordingly.
    The original disk set for that machine should be included with it when it is sold.

  • Reading multiple text files and writing them to one text file

    Hi,
    I'm trying to read a number of text files and write them to a single master file. My program reads all the files but only writes the last one to the master file.
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    // Input/Output Classes
    import java.util.Scanner;
    import java.io.PrintWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    import java.awt.GridLayout;
    public class Actor implements ActionListener
    public static String DataRoot = "I:\\JAVA\\UBP\\DAT
    public static JFrame SW = new JFrame();
    public static JTextField txtSPath;
    public static JTextArea txtTable;
    public void actionPerformed( ActionEvent e )
    String Command = e.getActionCommand();
    if (Command.equals("Process") )
    SetupWin();
    ProcessAll();
    public void SetupWin()
    SW.setTitle("void");
    SW.setSize(300,400);
    SW.setLayout(new GridLayout(6,1));
    txtSPath = new JTextField(40);    SW.add(txtSPath);
    txtTable = new JTextArea(10, 40); SW.add(txtTable);
    SW.setVisible(true);
    public void ProcessAll()
    Process("Dunstable","Finance");
    Process("Dunstable","Production");
    Process("Dunstable","Sales");
    public void Process(String Town, String Dept)
    String SPath = DataRoot + Town + "
    " + Dept + ".txt";
    String MPath = DataRoot + "masterFile" + ".txt";
    txtSPath.setText(SPath);
    String message = "Trying  " + SPath;
    System.out.println(message);
    String SlaveTable=message;
    try// if following fails an exception is thrown
    Scanner Slave = new Scanner(new FileInputStream(SPath)); // reads slave file
    PrintWriter outputStream = null;
    outputStream = new PrintWriter(new FileOutputStream(MPath));
    while ( Slave.hasNextLine() ) // reads text line by line
    //Read and output next record
    String PartRecord  = Slave.nextLine();
    String FullRecord = Town + " " + PartRecord;
    System.out.println(FullRecord);
    outputStream.println(FullRecord);
    Slave.close();
    outputStream.close();
    txtTable.setText(SlaveTable);
    //An Exception Error would be THROWN by above & CAUGHT below
    catch(FileNotFoundException e)
    message = "Could Not Find " + SPath;
    System.out.println(message);     //console
    txtSPath.setText(message);     //window
    catch(IOException e)
    System.out.println("Slave I/O Problem " + SPath);
    }Edited by: Ardour on Mar 4, 2008 1:53 PM

    I haven't looked closely, but my spidey sense tingles at this:
    new FileOutputStream(MPath)This will clobber (erase) the previous contents of the file. Open in append mode:
    new FileOutputStream(MPath, true)Of course, if the file exists before you run this code, you will end up append to the original contents. If that is not wanted, consider using File's delete method first.

  • Using a container to load several swf files and play them

    I need some help. I want to use several swf files and have them be called upon in a container file and play them in sequence. It's a presentation that needs to play thru but still have the ability to stop, click on items, open a popup and then continue on in the presentation. I am building all the individual "chapters" and their "sub-chapters" as swf files, with the hope that I can load them in order. I am relatively new to AS3. Help?

    If you will be loading swf files into a container then you will use the Loader class to accomplish that, so give that a looking over in the help documents and see what you can do.  If you have a problem getting it to work, post your code and describe what you have done.

  • IN Premiere Pro CC how can I group copy all files in time line to another part of the time line

    How can I group copy all files from one part of the timeline to another part

    From the Premiere Pro manual (I believe track targeting matters, too.  That is, the targeted tracks only are copied): 
    Copy command enhanced to operate on a range in a sequence
    The existing Copy command (Edit > Copy, Ctrl+C (Windows), Command+C (Mac OS)) is enhanced when used in the Timeline panel. If there are no clips or keyframes selected, and an In Point or Out Point is set, then the range of time between the In point and Out point is copied to the clipboard.

  • Copy All Files From Folder1 To Folder2

    I need a powershell script that will copy all files (not shortcuts, omit those) from folder1 to folder2.  The files are various types ranging from file folders (will want the contents of the folders copied as well) to .xlsx, .docx, .tiff, .jpg etc
    (and more).

    Hi,
    You can check the repository if you're looking for prewritten scripts:
    https://gallery.technet.microsoft.com/scriptcenter
    If you don't find what you need and you can't write your own, you can request a script here:
    https://gallery.technet.microsoft.com/scriptcenter/site/requests
    If you want to try to tackle this problem on your own, I'd suggest looking into Copy-Item or just using robocopy.
    Don't retire TechNet! -
    (Don't give up yet - 13,085+ strong and growing)

Maybe you are looking for

  • How do i add EQ to multiple music tracks

    in an effort to make my touch's battery life last longer i read the tips and 1 was to turn off eq on the touch and add eq to the tracks from itunes but i can only do 1 at a time which is very slow. how do i add eq to multiple tracks?? im talking like

  • How to add "%" symobl in Bex query?

    Hello All, In a Bex query, there is one structure with 3 elements as Rows. I have to show the resultant values in a specified format... in the first row, the values are to be suffixed with character "K" and the remaining 2 rows values are suffixed wi

  • Cannot view html images from only one sender

    I have the option set to view remote HTML images. I have junk filtering turned off. I have reset my junk mail filters just in case that was the issue. There is email from one sender that is a problem. Instead of images I get the blue squares with que

  • Acrobat Pro 11 Local Help Files

    This works for Reader. Where can I find the one for Pro? Or, where can I find the syntax so I can create my own? http://helpx.adobe.com/acrobat/kb/install-help-files-offline-reader.html

  • Tweak on photo import in bridge

    Hi, I have been using some other tools to import photos from camera mostly canon soft. but since my last camera acquisition I have been shooting more and more in RAW, so the conversion tool to DNG in bridge is great for me. However there is just one