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
Solved

batch copy and rename files

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
robocopy 33 115
Problem to line 23 53
Align > Distribute multiple objects in PowerPoint by the order inserted (back to front) 2 44
enabling cortana on Windows 10 6 20
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...
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

856 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