tags:

views:

123

answers:

2

I try to move my old logfiles to a yyyy\MM\dd folder structure by

Get-ChildItem . -Recurse -Include *.log | 
Move-Item -Dest {"D:\Archive\{0:yyyy\\MM\\dd}\{1}" -f $_.LastWriteTime, $_.Name} -Force

but i get a path-not-found error.

update

The source path does not seem to be the problem. It looks like using -Force on Move-Item does not create missing destination directories.


sub question: Could the same be done without Get-ChildItem?

+1  A: 

I guess that for a source file “some.log” the destination is supposed to be something like “D:\Archive\2010\04\23\some.log” and the directory “D:\Archive\2010\04\23” actually does not exist. In this case Move-Item fails. Is this the case?

Roman Kuzmin
No, the source directory (.) is an existing folder somewhere the same drive and the source item positivly exists because running the same command with -WhatIf renders the source item FullName correctly.
Filburt
I am talking about destination directory D:\Archive\2010\04\23 for a file whith last write time 2010-04-23. Move-Item does not create missed destination directories, it fails.
Roman Kuzmin
If I'm not mistaken using `Move-Item -Force` should create missing destination directories.
Filburt
I have just tried with -Force and got the same result. Help> Get-Help Move-Item -Parameter Forcedoes not say anything about creation of missed destination directories. I would recommend a test: for a single *.log file try to create the required destination (take it from -WhatIf) and then run the command. If it works then missed destination directory is still the reason.
Roman Kuzmin
I tried as you suggested an moving into the existing directory works. Interestingly enough a german version of Powershell claims that the -force switch will create missing destination directories.
Filburt
+1  A: 

As far as I found the proposed way of moving logs practically interesting, I decided to complete the task:

Get-ChildItem . -Recurse -Include *.log |
Move-Item -Force -Destination {
    $dir = "C:\Temp\{0:yyyy\\MM\\dd}" -f $_.LastWriteTime
    $null = mkdir $dir -Force
    "$dir\$($_.Name)"
}
Roman Kuzmin
I moved my acceptance to this answer, to have on top of the list. Thanks again for your support!
Filburt