David
David

Reputation: 159

Get and use current folder name inside a batch file

On a server I have a root folder with several 100 other folders named %Computername% inside it (created by their respective computers using mkdir).

These %Computername% folders contain a bat file which must be unique to that folder (the %Computername%). The bat file will consist of any number of commands for remote administration, for example:

schtasks /run /s %Computername% /tn "Task"

Is it possible to make the bat file automatically grab the folder's name (the %Computername%) without having to manually change it, and use that in its specified commands as the above example.

Upvotes: 3

Views: 6621

Answers (1)

aybe
aybe

Reputation: 16662

Use the CD command along FOR to retrieve current path, output only the folder name and ping it.

Test.bat:

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

Result:

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>

Another example:

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>

Upvotes: 1

Related Questions