3

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.

2
  • Idea 1: xcopy "Change me to %Computername% as I am run" or Idea 2: xcopy "Change me to %Computername% when this bat file is created in this %Computername% folder" Commented Oct 14, 2014 at 20:02
  • I found better answers for me in superuser.com/questions/160702/… Commented Jun 3, 2023 at 13:32

1 Answer 1

1

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

I dont think this will work for my application. I need the folder's name to replace/append %computername% in the bat file. For example: ping %Computername%, where %Computername% would be retrived from the folder's name
thanks alot for the clarification. It is working as expected. Is there a way to make it reference a folder within a folder, as in your example: where "C:\Users\Aybe\yahoo.com>" would be "C:\Users\Aybe\AnotherFolder\AndAnotherFolder\yahoo.com>", yet still ping yahoo.com. Will mark your answer correct as it is what I am looking for.
Scratch that, I realised it wasn't pinging because of a space in the foldername (New Folder). You answer works 100%. Thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.