3

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?

Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

7

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
  • 40,627
  • 11
  • 95
  • 117
  • I moved my acceptance to this answer, to have on top of the list. Thanks again for your support! – Filburt Apr 28 '10 at 08:52
1

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
  • 40,627
  • 11
  • 95
  • 117
  • 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 Apr 23 '10 at 14:18
  • 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 Apr 23 '10 at 14:30
  • If I'm not mistaken using `Move-Item -Force` should create missing destination directories. – Filburt Apr 23 '10 at 14:48
  • I have just tried with -Force and got the same result. Help > Get-Help Move-Item -Parameter Force does 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 Apr 23 '10 at 15:02
  • 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 Apr 23 '10 at 15:21