获取并使用批处理文件中的当前文件夹名称

时间:2014-10-14 19:30:55

标签: batch-file cmd directory

在服务器上,我有一个根文件夹,里面有几百个名为%Computername%的其他文件夹(由各自的计算机使用mkdir创建)。

这些%Computername%文件夹包含一个bat文件,该文件必须对该文件夹是唯一的(%Computername%)。 bat文件将包含任意数量的远程管理命令,例如:

schtasks / run / s%Computername%/ tn"任务"

是否可以使bat文件自动获取文件夹的名称(%Computername%),而无需手动更改它,并在其指定的命令中使用它,如上例所示。

1 个答案:

答案 0 :(得分:1)

沿CD使用FOR命令检索当前路径,仅输出文件夹名称并对其进行ping操作。

<强> TEST.BAT:

@echo off
FOR /F %%i IN ('cd') DO set ADDRESS=%%~nxi
ping %ADDRESS%

<强>结果:

C:\Users\Aybe\127.0.0.1>test.bat

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\Users\Aybe\127.0.0.1>

另一个例子:

C:\Users\Aybe\yahoo.com>test

Pinging yahoo.com [98.139.183.24] with 32 bytes of data:
Reply from 98.139.183.24: bytes=32 time=116ms TTL=48
Reply from 98.139.183.24: bytes=32 time=120ms TTL=48
Reply from 98.139.183.24: bytes=32 time=116ms TTL=48
Reply from 98.139.183.24: bytes=32 time=119ms TTL=48

Ping statistics for 98.139.183.24:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 116ms, Maximum = 120ms, Average = 117ms

C:\Users\Aybe\yahoo.com>