inv*_*bl3 15 powershell batch-file
在 cmd 中可以使用如下命令mkdir /data/rs1 /data/rs2 /data/rs3
:
一切都是正确的:
但是如何在powershell中实现呢?
我正在尝试使用引号,例如:
使用引号,我只得到一个文件夹并且在 powershell 中遇到问题:
mkdir : Could not find part of the path "rs3".
??????:1 ????:1
+ mkdir "/data/rs1 /data/rs2 /data/rs3"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\data\rs1 \data\rs2 \data\rs3:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand
Run Code Online (Sandbox Code Playgroud)
没有 qoutes 也是问题并且没有任何文件夹 :
mkdir : Can not find a positional parameter that takes an argument"/data/rs2".
??????:1 ????:1
+ mkdir /data/rs1 /data/rs2 /data/rs3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [mkdir], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,mkdir
Run Code Online (Sandbox Code Playgroud)
为了避免这些问题,我可以不加空格写:
PS C:\data> mkdir /data/rs1/data/rs2/data/rs3
Run Code Online (Sandbox Code Playgroud)
但它将是一个rs1
包含内部rs2
和的文件夹rs3
:
C:\data\rs1\data\rs2\data\rs3
Run Code Online (Sandbox Code Playgroud)
我很感激任何帮助。
Sim*_*onS 10
mkdir
一次可以创建多个目录,所以不需要foreach
. 你只需要用逗号分隔它们:
在这里,我在一个目录中创建了 3 个文件夹(Hello、Hello2、Hello3)
PS C:\install> mkdir Hello,Hello2,Hello3
Verzeichnis: C:\install
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09.07.2018 10:39 Hello
d----- 09.07.2018 10:39 Hello2
d----- 09.07.2018 10:39 Hello3
Run Code Online (Sandbox Code Playgroud)
在这里,我在目录中的单独子文件夹上创建了 3 个文件夹:
PS C:\install> mkdir .\xy3\Hello, .\yz3\Hello2, .\tr3\Hello3
Verzeichnis: C:\install\xy3
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09.07.2018 10:42 Hello
Verzeichnis: C:\install\yz3
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09.07.2018 10:42 Hello2
Verzeichnis: C:\install\tr3
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09.07.2018 10:42 Hello3
Run Code Online (Sandbox Code Playgroud)
在powershell中有很多方法可以做到这一点
1..3 | ForEach {MD ".\data\rs$_"}
Run Code Online (Sandbox Code Playgroud)
或者
'RS1','RS2','RS3' | % {New-Item -Name ".\data\$_" -ItemType 'Directory'}
Run Code Online (Sandbox Code Playgroud)
或者
for ($i=1;$i -le 3;$i++){MD ".\data\rs$i"}
Run Code Online (Sandbox Code Playgroud)
或者
MD .\data
Pushd .\data
$Folder = @('RS1','RS2','RS3')
Md $Folder
Run Code Online (Sandbox Code Playgroud)
哪里md
是New-Item
and的别名
%
,ForEach
是别名ForEach-Object
归档时间: |
|
查看次数: |
26867 次 |
最近记录: |