Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

batch copy and rename files

Posted on 2014-11-12
3
Medium Priority
?
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 83

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

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

661 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