You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:修改PowerShell脚本使其在父目录而非自身所在目录创建文件夹

求助:修改PowerShell脚本使其在父目录而非自身所在目录创建文件夹

我之前一直用这个脚本挺顺手的,结果有人动了我的文件,所以现在我想把文件藏起来或者移到隐藏文件夹里。但现在脚本有个问题——它会在自身所在的目录里创建新文件夹,我希望它能在父目录里创建这些文件夹,求帮忙改改脚本!

附上我当前的脚本:

###Get the year entered by the user

$year = Read-Host "Please enter year"

$nextyear = [int]$year + 1

$yearfolder = "$year-$nextyear"

###Set the starting date

$startdate = [DateTime] "01 April $($year)"

$count = 1

###Set the file's path by combining the folder path and the filename

$filename = join-Path -Path (Get-location) -ChildPath "a.txt"

###Create folder based on the year entered by the user

New-Item -ItemType Directory -Name "$($yearfolder)"

###Change directory to the newly created folder

cd "$($yearfolder)"

While ( $count -lt 13)

{

###Create folders

$newfolder = "$($count.ToString('00')) - $($startdate.ToString('MMMM yyyy'))"

New-Item -ItemType Directory -Name "$newfolder"

#Copy file to new folder

Copy-Item -Path $filename -Destination $newfolder

#Add one month

$startdate = $startdate.AddMonths(1)

#Increment the counter

$count = $count + 1

}

Hi Alan,我来帮你搞定这个问题!核心就是要把脚本里的目录指向从当前运行目录改成它的父目录,同时兼顾你隐藏文件的需求,下面给你分步说明:

一、修改后的完整脚本

### 获取用户输入的年份
$year = Read-Host "Please enter year"
$nextyear = [int]$year + 1
$yearfolder = "$year-$nextyear"

### 设置起始日期
$startdate = [DateTime] "01 April $($year)"
$count = 1

### 获取当前目录的父目录路径(关键改动!)
$parentDirectory = (Get-Location).Parent.FullName

### 设置文件路径——如果已经移到隐藏文件夹,这里要改对应路径
# 示例:如果a.txt在当前目录的.hidden文件夹里,就写成 Join-Path (Get-Location) ".hidden\a.txt"
$filename = Join-Path -Path (Get-Location) -ChildPath "a.txt"

### 可选:把a.txt设为隐藏文件(如果不需要可以删掉这行)
Set-ItemProperty -Path $filename -Name Attributes -Value ((Get-ItemProperty -Path $filename).Attributes -bor [IO.FileAttributes]::Hidden)

### 在父目录创建年份文件夹
New-Item -ItemType Directory -Path (Join-Path $parentDirectory $yearfolder) -Force

### 切换到父目录下的年份文件夹
Set-Location (Join-Path $parentDirectory $yearfolder)

While ($count -lt 13)
{
    ### 创建月度文件夹
    $newfolder = "$($count.ToString('00')) - $($startdate.ToString('MMMM yyyy'))"
    New-Item -ItemType Directory -Name $newfolder

    # 复制文件到月度文件夹
    Copy-Item -Path $filename -Destination $newfolder

    # 日期加一个月
    $startdate = $startdate.AddMonths(1)

    # 计数器加1
    $count++
}

二、关键改动点解析

  1. 获取父目录路径:新增了$parentDirectory = (Get-Location).Parent.FullName,这行能拿到当前脚本运行目录的上级目录绝对路径,是实现“在父目录创建文件夹”的核心。
  2. 指定年份文件夹的创建位置:原来的New-Item只给了文件夹名,默认在当前目录创建;现在用Join-Path把父目录和年份文件夹名拼接,明确指定在父目录下创建,加-Force是为了避免文件夹已存在时报错,实用又省心。
  3. 切换到正确的目录:把原来的cd "$($yearfolder)"改成Set-Location (Join-Path $parentDirectory $yearfolder),确保后续的月度文件夹都创建在父目录的年份文件夹里。
  4. 隐藏文件的处理:新增了设置文件隐藏属性的代码,如果你已经把文件移到隐藏文件夹,直接修改$filename的路径就行,不用这行代码也可以。

这样调整后,脚本就会完全按照你的要求,在父目录下创建年份文件夹,再在里面生成每个月的子文件夹,同时把目标文件复制进去,还能帮你把文件设为隐藏,完美解决你的问题~

备注:内容来源于stack exchange,提问作者Alan-53

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 09:39:39