Create folder using cmd and passing formatted date from powershell

依然范特西╮ 提交于 2019-12-13 04:07:36

问题


I would like to create folder where it's name is date in specific format. Date format is from powershell echo result. How could I combine this two things?

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

It must be in one line and starts with cmd. I will appreciate for any help.


回答1:


This should do what you are looking in powershell:

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

This is the one liner from the cmd prompt:

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

Hope it helps.




回答2:


If you want to run it in a single line in the cmd, try this. This will still just powershell to run. But you can run it from a bat file, powershell $date = get-date -format "{yyyy-MMM-dd HHmm}; md c:\temp\$date

Else you can run it in powershell

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

Hope this helps.

/Anders




回答3:


If you're still wanting a right-click context menu entry, (which creates a directory named using the format you've provided), you can do so by just entering this single line into a Command prompt 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"

This should create a context menu entry named NOW Folder Here, seen when you right click in an empty area of an explorer window, (and probably the desktop). When the entry is selected a powershell command should run creating the directory in that folder.



来源:https://stackoverflow.com/questions/49108364/create-folder-using-cmd-and-passing-formatted-date-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!