Powershell脚本,用于根据创建时间戳将文件移动到年/月文件夹中

时间:2014-01-13 23:46:03

标签: powershell

这里的第一篇文章......如果格式不正确,我道歉...我将继续努力。我正在开发一个Powershell脚本,它将获取以下信息,以便我可以使用另一个完全适用于此点的批处理文件。随着我对Powershell的理解的增长......我很可能会更改批处理文件,因为它非常冗长。

TL:使用Powershell的DR Newb需要帮助

我希望Powershell为不包含任何子文件夹的文件夹中的每个文件输出单行信息。我希望我的txt文件看起来像这样:

  

文件创建date_full文件名路径

每行一个文件。这将在以后解析为文本文件

这是我到目前为止所看到的...似乎我只需要一个for循环来运行像这样的psuedocode,我应该好好去。在这一点上,任何帮助都将受到赞赏。

谢谢大家,我希望我不会因格式化而杀死你。

$USS_Folder="path\USS_Files\"
$uss_year=#4 digit year of file creation date
$uss_month=#2 digit year of file creation date
$uss_file=# Full filename including path and Creation_Date

New-Item -ItemType directory -Path $USS_Folder\$uss_year\$uss_month

Move-Item $uss_file $USS_Folder\$uss_year\$uss_month

4 个答案:

答案 0 :(得分:5)

所以花了一些时间倾注了一些充满脚本的页面......我想出了下面的解决方案并进行了修改以满足我的需求......我将它用于生产并且效果很好。告诉我你的想法。

    <#
File modified by Joshua as taken from
http://www.marcvalk.net/2012/06/powershell-moving-files-into-subfolder-based-on-date/

Set Variables of Source folder and Destination folder
Assign variable of files to be the files with uss extension
For each file with uss extension assign the Directory variable the information for file creation year and month
    if the year and month folder do not exist, then create them from file creation information
Move file to sub-folder of year and month from file creation information passed into Directory variable

#>

$SourceDir = "path to uss files\USS_Files\"
$DestinationDir = "path to uss files\USS_Files\"

$files = get-childitem $SourceDir *.uss

foreach ($file in $files) 

{

$Directory = $DestinationDir + "" + $file.CreationTime.Date.ToString('yyyy') + "\" + $file.CreationTime.Date.ToString('MM-MMM')

if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
Move-Item $file.fullname $Directory 
}

答案 1 :(得分:1)

喜欢这个吗?

$USS_Folder = "path\USS_Files"

get-childitem | % {

    $file = $_.FullName 
    $date = Get-Date ($_.CreationTime)
    $month = $date.month
    $year = $date.year

    new-item -type Directory -path "$USS_Folder\$year\$month"
    move-item $file "$USS_Folder\$year\$month"
}

答案 2 :(得分:1)

这是我在我的环境中使用但已转换为您的要求的解决方案:

 $USS_Folder = "path\USS_Files\"
 Get-ChildItem -File| Sort-Object LastWriteTime |`
  Group {$_.LastWriteTime.ToString("yyyy-MM")} |`
  % {
    $folder = $USS_Folder + $_.name 
    if (!(Test-Path $folder)) `
      {new-item -type Directory -path $folder -ErrorAction SilentlyContinue}
    $_.group|move-item -Destination $folder
    }

答案 3 :(得分:0)

假设您正在使用它来将照片排列到文件夹中,并且文件名以YYYYMMDD开头,那么以下脚本可以很好地工作。此方法优于上面的方法,因为我们复制/粘贴/移动了它,因此创建和修改的日期与拍摄的日期不同。

另外,要运行此脚本,只需将其粘贴到扩展名为.ps1的文本文件中,右键单击该文件,然后选择“使用powershell运行”。

# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}

# List Files which will be moved
$files

# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'

foreach ($file in $files)
{
# Get year and Month of the file using the filename
$bn = $file.basename.ToString()
$year = $bn.substring(0,4)
$month = $bn.substring(4,2)
$day = $bn.substring(6,2)

# Out FileName, year and month
$file.Name
$year
$month
$day

# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}

# Move File to new location
$file | Move-Item -Destination $Directory
}

如果要使用上次修改日期,请使用以下内容(先前代码的稍作修改的版本)

# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}

# List Files which will be moved
$files

# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'

foreach ($file in $files)
{
# Get year and Month of the file
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString()
$day = $file.LastWriteTime.Day.ToString()

# Out FileName, year and month
$file.Name
$year
$month
$day

# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}

# Move File to new location
$file | Move-Item -Destination $Directory
}

如果要使用创建的日期,请使用以下内容(先前代码的稍作修改的版本)

# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -Recurse | where {!$_.PsIsContainer}

# List Files which will be moved
$files

# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'C:\Users\YourName\SourceFolder\YourAlbumnsFolder'

foreach ($file in $files)
{
# Get year and Month of the file
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.CreationTime.Year.ToString()
$month = $file.CreationTime.Month.ToString()
$day = $file.CreationTime.Day.ToString()
$hour = $file.CreationTime.Hour.ToString()

# Out FileName, year and month
$file.Name
$year
$month
$day

# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}

# Move File to new location
$file | Move-Item -Destination $Directory
}

GitHub上的整个项目代码