使用 cmd 创建文件夹并从 powershell 传递格式化日期

Create folder using cmd and passing formatted date from powershell

我想创建名称为特定格式日期的文件夹。日期格式来自 powershell echo 结果。我怎样才能将这两件事结合起来?

cmd.exe /c md \"%1\%%"date%%\"" /fpowershell get-date -format "{yyyy-MMM-dd HHmm}?

必须在一行中,以cmd开头。 我将不胜感激。

这应该可以满足您在 powershell 中的需求:

$format=get-date -format "yyyy-MMM-dd HHmm"
New-Item -ItemType Directory -Path "C:\Folder$format"

这是命令提示符下的一行:

Powershell -command "New-Item -Name (get-date -format 'yyyy-MMM-dd HHmm') -ItemType Directory -Path 'C:\Folder' -Force"

希望对您有所帮助。

如果你想 运行 它在 cmd 中的一行中,试试这个。这仍然只是 powershell 到 运行。但是你可以 运行 它来自一个 bat 文件, powershell $date = get-date -format "{yyyy-MMM-dd HHmm}; md c:\temp$date

否则你可以运行它在powershell

    $date = get-date -format "{yyyy-MMM-dd HHmm}"
New-Item -ItemType Directory -Path "c:\temp$date"

希望对您有所帮助。

/安德斯

如果您仍然想要 right-click 上下文菜单条目,(创建一个使用您提供的格式命名的目录),您可以这样做只需将这一行输入命令提示符 window.

Powershell -NoP -C "New-Item 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir' -Value 'NOW Folder Here' -Force"; "New-Item 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir\command' -Value '\"C:\Windows\system32\WindowsPowerShell\v1.0\Powershell.exe\" -NoP -C \"$dt = get-date -f {yyyy-MMM-dd HHmm}; MD \"\"%V\"\"$dt\"\"\"' -Force"

这应该会创建一个名为 NOW Folder Here 的上下文菜单条目,当您右键单击资源管理器 window、(可能还有桌面)的空白区域时会看到。选择条目后,powershell 命令应该 运行 在该文件夹中创建目录。