Solved

batch copy and rename files

Posted on 2014-11-12
3
482 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 84

Accepted Solution

by:
oBdA earned 167 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 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 167 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 69

Assisted Solution

by:Qlemo
Qlemo earned 166 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lenovo E550: Install Windows 10 Pro 17 34
Powershell Code 3 41
SBS 2011 server backup failing. 27 36
Why does my Dell Precision tower 5810 not boot? 13 37
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you 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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

679 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