Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

batch copy and rename files

Posted on 2014-11-12
3
Medium Priority
?
508 Views
Last Modified: 2014-11-13
Hi,

I need to copy all files in a directory to another directory and rename them by appending the current date. I guess xcopy or robocopy would work, but don't know how to set the destination and file name, so all files are copied and renamed to include the date.

Thanks!
0
Comment
Question by:rookie_b
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 668 total points
ID: 40437132
Try the script below; it uses WMI to get the date, which avoids problems with the OS and user dependent time format settings. The script is currently in test mode and will only display the copy commands it would normally run. Remove the uppercase ECHO in line 15 to run it for real.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp
set TargetFolder=D:\Temp
set FileMask=*.txt
echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
	set /a Line += 1
	if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
	if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%
for %%a in ("%SourceFolder%\%FileMask%") do (
	ECHO copy "%%a" "%TargetFolder%\%%~na_%TimeStamp%%%~xa)
)

Open in new window

0
 
LVL 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 668 total points
ID: 40438712
Powershell method, remove the whatif if it satisfies you, change the $source and $destination to your defaults.
[CmdletBinding()]
Param(
    [string]$Source= "C:\temp",
    [string]$Destination= "d:\temp",
    [switch]$Force = $false
    )
$date= Get-Date -Format "yyyy-MM-dd"
While ((Test-Path -Path $Source) -ne $True)
 {
    Write-Output $Source " does not exist"
    $Source = Read-Host "Input Source Path"
}
While ((Test-Path -Path $Destination) -ne $True) {
    Write-Output $Destination " does not exist"
    $Destination = Read-Host "Input Destination Path"
} 
$files = Get-ChildItem -Path $source
foreach ($file in $files) {
$Destfilename = $Destination + "\" + $date + "-" + $file.name
$Destfilename
Copy-Item $file.FullName $Destfilename -WhatIf
}

Open in new window

0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 664 total points
ID: 40439634
The PS script can be simplified. And as I read it, the date should get appended, not prepended,
So lines 17-22 in short:
Get-ChildItem -Path $source |
   copy-item -whatif -destination { "$($_.BaseName)-$date$($_.Extension)" }

Open in new window

The $force parameter does not have any meaning here, btw. IMHO is is not required at all.

On another note: It sounds more reasonable to only copy changed files; unless you need to have a complete set of files with the same date each time (in which case prepending the date makes more sense).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question