Powershell 脚本根据创建时间戳将文件移动到年/月文件夹中 [英] Powershell script to move files into year/month folders based on creation timestamp

查看:84
本文介绍了Powershell 脚本根据创建时间戳将文件移动到年/月文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的第一篇文章...如果格式不正确,我深表歉意...我会努力解决这个问题.我正在编写一个 Powershell 脚本,它将为我提供以下信息,以便我可以使用另一个在这一点上完美运行的批处理文件.随着我对 Powershell 了解的加深……我很可能会更改批处理文件,因为它很长.

First post here...I apologize if this isnt formatted correctly...I will work on this. I am working on a Powershell script that will get me the following information so that I can work with another batch file that works perfectly to this point. As I grow in my understanding of Powershell...I will most likely change the batch file since it is pretty lengthy.

TL:DR Newb 使用 Powershell 需要帮助

TL:DR Newb working with Powershell needs help

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

I am wanting Powershell to output a single line of information for each file in a folder excluding any subfolders. I would like my txt file to look like this:

文件创建日期_文件名的完整路径

file creation date_full path to filename

每行一个文件.这将在稍后解析成文本文件

one file per line. This will be parsed into a text file later

这是我到目前为止所拥有的......似乎我只需要一个 for 循环来运行类似这个伪代码的东西,我应该很高兴.在这一点上,任何帮助将不胜感激.

Here is what I have so far...seems like I just need a for loop to run something like this psuedocode and I should be good to go. Any help would be appreciated at this point.

谢谢大家,我希望我不会被格式化杀死.

Thanks all and I hope I am not killin you with formatting.

$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

推荐答案

所以在花了一段时间倾倒了一些充满脚本的页面之后......我想出了下面的这个解决方案并进行了修改以满足我的需求......我已将其用于生产,并且效果很好.告诉我你的想法.

So after spending a while pouring over a number of pages filled with scripts...I came up with this solution below and modified to fit my needs...I have it employed into production and it works great. Tell me what you think.

    <#
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 
}

这篇关于Powershell 脚本根据创建时间戳将文件移动到年/月文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆