Batch Script Help

Status
Not open for further replies.

Blastomatic

Wise, Aged Ars Veteran
119
Hey guys,

I have a batch script that currently works to pull the name of a folder on a system that meets criteria, as follows:
Code:
@echo off
SET DIR=---

FOR /D %%d IN ("C:\BEW-*") DO (
  SET DIR=%%d
)

IF EXIST "%DIR%" (GOTO WINDIR) ELSE (GOTO NOWINDIR)

:WINDIR
  ECHO Yes
  ECHO %DIR%
  GOTO END

:NOWINDIR
  ECHO No

:END

What I am trying to do is get the %DIR% to ONLY be the name of the directory and not the full path of C:\<DIRECTORY>. I am rusty as hell with batch file logic but have to use it due to a load of ancient servers I will be running this against. Can anyone point me in the right direction here?
 

Blastomatic

Wise, Aged Ars Veteran
119
[url=http:/civis/viewtopic.php?p=29236955#p29236955:135szhpq said:
PsychoStreak[/url]":135szhpq]dir /b gives you bare folder names.

Are you looking for a standard system folder like windows, or appdata? You could probably use the variables for those in an IF Exist type command instead of pulling the directory.
It was to scour our systems of these shared out folders that Backup Exec plops in the C:\ root when it installs the client. It was all over a couple hundred of our servers so the process was two-fold, delete the directory itself, and then delete the share. But I needed just the directory's name to remove the share using net share sharename /delete.

Anyway, it worked, all clear. Yay scripting.
 

Whittey

Ars Tribunus Militum
2,026
You can just wildcard if exist.
if exist C:\BEW-* (GOTO WINDIR) ELSE (GOTO NOWINDIR)
Example:
Code:
C:\temp>if exist C:\temp\BEW-* (echo WINDIR) ELSE (echo NOWINDIR)
WINDIR

C:\temp>rmdir bew-asdfas

C:\temp>if exist C:\temp\BEW-* (echo WINDIR) ELSE (echo NOWINDIR)
NOWINDIR

If you really wanted to loop through all the directories, you can expand %%d as:
for /d %%d in (C:\BEW-*) do set dir=%%~nxd

Example:
Code:
C:\temp>type test.cmd
@for /d %%d in (C:\temp\BEW-*) do echo set dir=%%~nxd
C:\temp>test

C:\temp>echo set dir=bew-asdasfad
set dir=bew-asdasfad

The %%~nxd is shown in the 'for' command help. In this case, n is file name only and x is file extension only. You shouldn't actually need x, but if you change this up and match for *, you'll get the .dir directories too which would match the input wildcard but not be output. You can see this by just "for /d %i in (*) do @echo %~ni" in a directory with folders named ".something." You'll get a bunch of "Echo is on." lines as it echoes the .dirs.
Code:
%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string
And yes, that last one lets you write a simple which.cmd file.
Code:
C:\temp>type which.cmd
@echo off
for %%i in (%*) do @if exist %%~$PATH:i echo %%~$PATH:i
C:\temp>which psexec.exe
C:\Windows\psexec.exe
 
Status
Not open for further replies.