创建以当前日期和时间命名的文件夹

创建以当前日期和时间命名的文件夹

我正在尝试创建一个 powershell 脚本,以当前日期(格式为 yyyy-MM-dd)作为名称创建一个新文件夹。

以下是我目前所掌握的信息:

PS C:\Users\me\Desktop> powershell.exe -command "new-item ($(get-location)
+ (Get-Date).year + "-" + (Get-Date).month + "-" + (Get-Date).day) -type directo
ry"
Die Benennung "C:\Users\me\Desktop" wurde nicht als Name eines Cmdlet, ein
er Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überp
rüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern ent
halten), und wiederholen Sie den Vorgang.
Bei Zeile:1 Zeichen:35
+ new-item (C:\Users\me\Desktop <<<<  + (Get-Date).year +  - + (Get-Date).
month + - + (Get-Date).day) -type directory
+ CategoryInfo          : ObjectNotFound: (C:\Users\j.moore\Desktop:String
) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

答案1

尝试这个:

 New-Item "$((get-date).toString('yyyy-MM-dd'))" -ItemType directory

或者:

md "$((get-date).toString('yyyy-MM-dd'))"

解释来自本网站

$() 使你可以在字符串中使用命令的输出

get-date 周围的 () 再次表示“首先执行此操作”,以便我们可以使用 get-date 返回的 DateTime 对象的方法

DateTime 对象的 toString() 方法以格式字符串作为输入。

答案2

尝试这个:

mkdir (Get-Date -Format "yyyy-MM-dd")

相关内容