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

用于根据创建时间戳将文件移动到年/月文件夹中的Powershell脚本,powershell,Powershell,第一篇文章在这里…我很抱歉,如果这是格式不正确…我会在这个工作。我正在编写一个Powershell脚本,该脚本将为我提供以下信息,以便我可以使用另一个批处理文件,该批处理文件可以完美地工作到这一点。随着我对Powershell理解的加深,我很可能会更改批处理文件,因为它相当长 TL:Newb博士与Powershell合作需要帮助 我希望Powershell为文件夹中的每个文件(不包括任何子文件夹)输出一行信息。我希望我的txt文件如下所示: 文件创建日期\u文件名的完整路径 每行一个文件。这将在

第一篇文章在这里…我很抱歉,如果这是格式不正确…我会在这个工作。我正在编写一个Powershell脚本,该脚本将为我提供以下信息,以便我可以使用另一个批处理文件,该批处理文件可以完美地工作到这一点。随着我对Powershell理解的加深,我很可能会更改批处理文件,因为它相当长

TL:Newb博士与Powershell合作需要帮助

我希望Powershell为文件夹中的每个文件(不包括任何子文件夹)输出一行信息。我希望我的txt文件如下所示:

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

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

这是我到目前为止所做的…似乎我只需要一个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
像这样

$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"
}

因此,在花了一段时间浏览了大量充满脚本的页面之后……我提出了下面的解决方案,并进行了修改以满足我的需要……我将其应用到了生产中,效果非常好。告诉我你的想法

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

$SourceDir=“uss文件的路径\uss\u文件”
$DestinationDir=“uss文件的路径\uss\u文件”
$files=get childitem$SourceDir*.uss
foreach($files中的文件)
{
$Directory=$DestinationDir++$file.CreationTime.Date.ToString('yyyy')+“\”++$file.CreationTime.Date.ToString('MM-MMM'))
if(!(测试路径$Directory))
{
新项$directory-类型目录
}
移动项目$file.fullname$目录
}

以下是我在环境中使用的解决方案,但已转换为您的需求:

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

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

另外,要运行此脚本,只需粘贴到扩展名为.ps1的文本文件中,右键单击该文件,然后选择RunwithPowerShell

# 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
}
如果要使用上次修改的日期,请使用以下代码(先前代码的稍微修改版本)

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


如果您有数千个文件,那么测试路径上的整个项目代码是非常必要的。谢谢很抱歉这么长时间后又恢复了。。。我根据自己的需要对此进行了一些编辑(在所有子文件夹中获取所有文件,以及任何扩展名),效果很好,但当文件名中包含
[
]
时,我会遇到问题。我怎样才能摆脱这样的角色,使脚本不会失败呢?我没有使用powershell的经验,因此请非常具体。谢谢@如果您有数千个文件,是否需要sl0815?为什么?@Kiquenet,因为脚本不会尝试创建可能已经存在的目录(与此处列出的其他解决方案类似)。这大大加快了速度。在大多数情况下,只需要创建几个dir(这对我来说是重点),然后对新项目的大多数调用都将被跳过。为什么您会将它们命名为$day-$month-$year?他们不会按时间顺序那样排序。
# 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
}