VMware Cloud Community
Thushyaa
Contributor
Contributor
Jump to solution

rename files with in a directory appending date&time

Hi ,

I am looking for a way to rename all files that are in a directory with a simple append at the end with date and time.

eg:

file1 -> file1ddmmyyhhmi

file2 -> file1ddmmyyhhmi

eg:

$A=get-date -Format "yyyymmddhhmm"

foreach -object -process ....

thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$A=get-date -Format "yyyymmddhhmm"
Get-ChildItem "C:\folder" | %{
	Rename-Item -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName
}

This assumes the files are stored in C:\folder

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$A=get-date -Format "yyyymmddhhmm"
Get-ChildItem "C:\folder" | %{
	Rename-Item -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName
}

This assumes the files are stored in C:\folder

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Thushyaa
Contributor
Contributor
Jump to solution

LucD - thx for the fast response ,

how can i use this with multiple items in the directory ? it works if there is only one file. how to use foreach ? etc...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure ?

I tested it on a folder with multiple files in there, and they were all renamed.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Thushyaa
Contributor
Contributor
Jump to solution

see my output .....

start

~PowerCLI> Get-ChildItem "C:\test"

Directory: Microsoft.PowerShell.Core\FileSystem::C:\test

Mode LastWriteTime Length Name

-


-


-


-


-a--- 07/10/2010 8:58 PM 0 abc.txt

-a--- 07/10/2010 8:58 PM 0 pqr.txt

-a--- 07/10/2010 8:58 PM 0 xyz.txt

~ PowerCLI> $A=get-date -Format "yyyymmddhhmm"

~ PowerCLI> Get-ChildItem "C:\test" | %{Rename-Item -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName}

Rename-Item : Cannot create a file when that file already exists.

At line:1 char:40

+ Get-ChildItem "C:\test" | %{Rename-Item <<<< -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName}

Rename-Item : Cannot create a file when that file already exists.

At line:1 char:40

+ Get-ChildItem "C:\test" | %{Rename-Item <<<< -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName}

~ PowerCLI>

end

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure there are no 'hidden' files in the folder ?

This is a test run I just did

PS C:\> Get-ChildItem "C:\folder"


    Directory: C:\folder


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         10/8/2010   3:22 AM          0 abc.txt
-a---         10/8/2010   3:22 AM          0 pqr.txt
-a---         10/8/2010   3:22 AM          0 xyz.txt


PS C:\> Get-ChildItem "C:\folder" | %{Rename-Item -NewName ($_.Basename + $A + $_.extension) -Path $_.FullName}
PS C:\> Get-ChildItem "C:\folder"


    Directory: C:\folder


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         10/8/2010   3:22 AM          0 abc201053080953.txt
-a---         10/8/2010   3:22 AM          0 pqr201053080953.txt
-a---         10/8/2010   3:22 AM          0 xyz201053080953.txt


PS C:\>

You could to a

Get-ChildItem "C:\folder" -Force

to check.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Thushyaa
Contributor
Contributor
Jump to solution

LucD, it was my shell version that caused the issue, now working.

thx

Reply
0 Kudos