Results 1 to 15 of 15

Thread: Batch file

  1. #1
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Batch file

    I am needing to run an hourly batch file that will do the following:

    xcopy the file as a backup to this particular directory (I've already got this covered)

    Then rename that file to "yy-mm-dd--minute-FILENAME.xls" then move it to a different directory.

    Can you assist with that?
    Thanks!

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    kzkz,

    Here's a start:
    Code:
    @echo off
    
    Set FileName=YourFileNameHERE
    
    Set NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-%FILENAME%.txt
    
    echo. New Filename: %NewFN%
    
    Set NewFn=
    SET FileName=
    Produces a file name of: 2013-11-05-2145-YourFileNameHERE.txt

    You can make substitutions as necessary and find a way to get the Filename into the %FileName% variable. I'm a little brain dead right now.

    Of course you would use the Rename command: Rename OldFileName %FileName% and you would also have to mess with the drive and path info.

    One question though why copy it to one location then rename then move to another when your could change the name while copying it to the final location which would save a lot of steps? HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks
    BTW, this is for a spreadsheet that will be coming over to this folder as an xcopy once an hour. Doesn't have to go to that directory first then renamed necessasry. I just need the files which will all be the same file name, except for with the date prior to the file name.

    Can it output to the different directory within the same command?

    I tried the below and all it said was "New Filename: 2013-11-05-2116-"C:\Users\Admin\Desktop\test.xlsx".xlsx.
    Am I supposed to fill in some variables yet?

    @echo off

    Set FileName="C:\Users\Admin\Desktop\test.xlsx"

    Set NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-%FILENAME%.xlsx

    echo. New Filename: %NewFN%

    Set NewFn=
    SET FileName=
    pause

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    kzkz,

    Ok, I wasn't as brain dead as I thought and I couldn't let it go anyway.

    Code:
    @echo off
    
    echo. %NewFN%
    For %%f in (*.cmd) do ( 
    Set "NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-"
    Set "NewFN=%NewFN%%%f"
    Copy %%f G:\BEKDocs\Transfer\%NewFN%%%f
     )
    Ok this code assumes it is being executed from the SOURCE directory (where the files to be copied reside). If this not the case you'll have to append that to the beginning of the %%f directly after the Copy command like the one appended to the DESTINATION. Of course you'll have to change the destination directory!
    This does it all in one pass.
    You'll have to change the *.cmd to *.xls* that way you can get both old and new Excel files or if you just want the new then *.xlsx of course you may need to allow for ones with macros or not.

    Here's what my Destination directory after execution:
    TransferDir.JPG
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. #5
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm getting a 'teh filename, directory name, or volune label syuntax is incorrect."
    If I take out the directory name, it says the file can't be copied onto itself.
    Below is what I tried:

    @echo off
    echo. %NewFN%
    For %%f in (test.xlsx) do (
    Set "NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-"
    Set "NewFN=%NewFN%%%f"
    Copy %%f %NewFN%%%f
    )

  7. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    kzkz,

    If you don't have directory names then you need to navigate within the DOS window to the drive/directory where the file(s) reside.
    Secondly, you will get this error if you try to copy the files in quick succession as the filename w/date-time will already exist in the destination.
    If you are going to run this from a shortcut on your desktop you MUST have both SOURCE and DESTINATION drives/directories specified in the COPY command.
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  8. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What am I missing here? Even with the full directory and file name I get errors. Even with the directories left off, the .bat file and xlsx file is in the same desktop directory.

    @echo off

    echo. %NewFN%
    For %%f in (C:\Users\Admin\Desktop\test.xlsx) do (
    Set "NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-"
    Set "NewFN=%NewFN%%%f"
    Copy %%f C:\Users\Admin\Desktop\%NewFN%%%f
    )

  9. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    kzkz,

    How are you running the file? From a Command Prompt or from a Shortcut on the desktop?

    Try it this way.
    Code:
    @echo off
    
    For %%f in (C:\Users\Admin\Desktop\test.xlsx) do (
    Set "NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-"
    Set "NewFN=%NewFN%%%f"
    Copy C:\Users\Admin\Desktop\%%f  C:\Users\Admin\Desktop\%NewFN%%%f
    )
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  10. #9
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm running it from a bat file that is on my desktop. I get the same error when running your info above.

  11. #10
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,376
    Thanks
    7
    Thanked 205 Times in 195 Posts
    To test batch files like that I put "echo" in front of the commands so I can see what is going to happen. You can also comment out the initial "echo off".
    Code:
    ::@echo off
    
    For %%f in (C:\Users\Admin\Desktop\test.xlsx) do (
    Set "NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-"
    Set "NewFN=%NewFN%%%f"
    ECHO Copy C:\Users\Admin\Desktop\%%f  C:\Users\Admin\Desktop\%NewFN%%%f
    )
    To rename the file and then move it I suggest "move C:\Users\Admin\Desktop\%%f C:\Users\Admin\Desktop\NewDir\%NewFN%%%f

    cheers, Paul

    p.s. remove the "%%f" from the end of the copy/move command, it isn't required.

  12. #11
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    kzkz,

    I'm getting a little baffled here.

    Ok when this is run from a shortcut or not from the current directory the filename comes back with the full drive/path/filename/ext.
    So it needs to be split up. which I can do with: Set BaseName=%%~nxf
    However, when the program is run it fails to resubstitute the next name in the NewFN variable.
    Here is the current version of the code.
    Code:
    For %%f in ("G:\BEKDocs\Batch\*.cmd") do ( 
    Set BaseName=%%~nxf
    Set NewFN=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%-%Time:~0,2%%Time:~3,2%-%BaseName%
    Copy %%f "G:\BEKDocs\Transfer\%NewFN%"
    pause
     )
    Here is the output:
    C:\WINDOWS\system32>For %f in ("G:\BEKDocs\Batch\*.cmd") do (
    Set BaseName=%~nxf
    Set NewFN=2013-11-06-1058-TEDsCleanup.cmd <-- This is correct!
    Copy %f "G:\BEKDocs\Transfer\2013-11-06-1054-TEDsCleanup.cmd" <-- The source is missing while the dest is correct
    pause
    )

    C:\WINDOWS\system32>(
    Set BaseName=CopyAndRenameWithDateTime.cmd <-- This is correct!
    Set NewFN=2013-11-06-1058-TEDsCleanup.cmd <-- This hasn't changed from the first run through the loop!
    Copy G:\BEKDocs\Batch\CopyAndRenameWithDateTime.cmd "G:\BEKDocs\Transfer\2013-11-06-1054-TEDsCleanup.cmd"
    pause
    )
    1 file(s) copied.
    Press any key to continue . . .

    C:\WINDOWS\system32>(
    Set BaseName=RebuildIconCache.cmd <-- This is correct
    Set NewFN=2013-11-06-1058-TEDsCleanup.cmd <-- This is still stuck on the first iteration and continues so!
    Copy G:\BEKDocs\Batch\RebuildIconCache.cmd "G:\BEKDocs\Transfer\2013-11-06-1054-TEDsCleanup.cmd"
    pause
    )
    1 file(s) copied.
    Press any key to continue . . .

    C:\WINDOWS\system32>(
    Set BaseName=RebuildIcons.cmd
    Set NewFN=2013-11-06-1058-TEDsCleanup.cmd
    Copy G:\BEKDocs\Batch\RebuildIcons.cmd "G:\BEKDocs\Transfer\2013-11-06-1054-TEDsCleanup.cmd"
    pause
    )
    1 file(s) copied.
    Press any key to continue . . .

    C:\WINDOWS\system32>(
    Set BaseName=TEDsCleanup.cmd
    Set NewFN=2013-11-06-1058-TEDsCleanup.cmd
    Copy G:\BEKDocs\Batch\TEDsCleanup.cmd "G:\BEKDocs\Transfer\2013-11-06-1054-TEDsCleanup.cmd"
    pause
    )
    1 file(s) copied.
    Press any key to continue . . .
    FYI I tried clearing the variables e.g.{Set BaseName= } w/o any change.

    I haven't batched in quite a while so may someone else...like Batcher could chine in.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  13. #12
    3 Star Lounger SpywareDr's Avatar
    Join Date
    Dec 2009
    Location
    Miami, Florida, USA
    Posts
    321
    Thanks
    2
    Thanked 36 Times in 31 Posts
    Code:
    @echo off
    :SetupUsersEnvironmentVariables
     set Filename=filename.xls
     set SourceDir=C:\Users\Owner\Desktop
     Set TargetDir=C:\Backups
    :GetDate
     for /F "tokens=1-3 delims=/" %%A in ('echo %date%') do (
       set mm=%%A
       set dd=%%B
       set yy=%%C
     )
    :GetTime
     for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do (
       set hr=%%A
       set mn=%%B
       set sc=%%C
     )
     if %hr% LSS 10 set hr=0%hr%
    :Backup
     echo xcopy "%SourceDir%\%Filename%" "%TargetDir%\%yy%%mm%%dd%_%hr%%mn%%sc%_%Filename%"
    :CleanupEnvironmentVariables
     for %%x in (Filename SourceDir TargetDir mm dd yy hr mn sc) do set %%x=
    :End
    The four items you see in red need to be altered by you:

    1. Change the filename.xls to the name of the file you want backed up.

    2. Change the C:\Users\Owner\Desktop to the complete path of the drive:\folder(s) where your filename.xls is currently located.

    3. Change the C:\Backups location to the complete path of the drive:\folder(s) where you want the renamed backup to be copied into.

    4. Before you change this last one, the "echo ", run the batch file once and review the output. For example, if I were to run it as is here now, I would see:

      xcopy "C:\Users\Owner\Desktop\filename.xls" "C:\Backups\131106_123236_filename.xls"

      Anyway, if the output looks okay, remove the echo and save your changes.
    ____________http://www.microsoft.com/security____________
    \____________________ ____.-.____ ____________________/
    \_____________\ -._)!(_.- /_____________/
    \_______\. ~\ /~ ./_______/

  14. #13
    5 Star Lounger
    Join Date
    May 2001
    Location
    Allen, Texas, USA
    Posts
    653
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Excellent. Thanks!

  15. #14
    3 Star Lounger SpywareDr's Avatar
    Join Date
    Dec 2009
    Location
    Miami, Florida, USA
    Posts
    321
    Thanks
    2
    Thanked 36 Times in 31 Posts
    You're more than welcome.
    ____________http://www.microsoft.com/security____________
    \____________________ ____.-.____ ____________________/
    \_____________\ -._)!(_.- /_____________/
    \_______\. ~\ /~ ./_______/

  16. #15
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,813
    Thanks
    185
    Thanked 703 Times in 641 Posts
    Hey Y'all,

    I got so frustrated trying to get the CMD file to loop through all the files in the directory and timestamp the backups that I decided to do it in PowerShell since it's easier to read and test. So here are the results if you're interested.
    Code:
    <#+---------------------------------------------------------------------------+
      | PowerShell Pgm: CreateTimeStampedBackups.ps1                              |
      | Programmed By : The Computer Mentor aka Retired Geek                      |
      | Created       : 06 Nov 2013                                               |
      | Last Updated  : 06 Nov 2013                                               |
      | Current Ver.  : 2.0                                                       |
      +---------------------------------------------------------------------------+
    #>
    Param (
      [String]$SourcePath  = 'G:\BEKDocs\Batch' ,
      [String]$DestPath = 'G:\BEKDocs\Transfer' ,
      [String]$FType = 'cmd'
      )
    
      $TimeStamp = get-date -format yyyy-mm-dd-HHmm-
      $filecnt=0
    
      Foreach ($f in dir $SourcePath\*.$FType) {
    
            $SourceFile = "$SourcePath\" + $f.name
            $DestFile = $DestPath + '\' + $TimeStamp + $f.name
    
            copy-item $SourceFile $DestFile
    
            $filecnt++
      }
    
     $Message = "$filecnt File(s) were copied."
    
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [Windows.Forms.MessageBox]::Show($Message,"Completion Status", `
       [Windows.Forms.MessageBoxButtons]::OK , `
       [Windows.Forms.MessageBoxIcon]::Information)
    I've tested it by calling it from the Task Scheduler (since the OP wanted to do it every hour) using a 5 minute interval and it works just fine. You can delete the everything after and including the $Message if you don't want the status message.
    PSStatus.JPG
    Here's the basic info for the task:
    Command: %SystemRoot%\system32\WindowsPowerShell\v1.0\power shell.exe
    Arguments G:\BEKDocs\Scripts\CreateTimeStampedBackups.ps1 -sourcepath G:\BEKDocs\Batch -DestPath G:\BEKDocs\Transfer -Ftype cmd

    Note: the items in RED above will have to be changed according to your needs/system.

    Here's the destination directory in the time it took me to write this...remember 5 minute intervals.
    TimedBK.JPG
    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •