Solved

batch copy and rename files

Posted on 2014-11-12
3
465 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 82

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 78

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 68

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now