Jump to content

Batch file help needed


Recommended Posts

Posted

for /D /R "%USERPROFILE%\Desktop" %%a IN ( * ) DO echo "%%a"

Using the command above, if there was a single folder on the Desktop named "backup", the (echo) command output would read:

"C:\>Documents and Settings\username\Desktop\backup"

My question is -- how do I extract the actual folder name itself so that it echoes "backup" instead of the full path?

Thanks in Advance.


Posted

This should give you the results you want...

for /D /R "%USERPROFILE%\Desktop" %%a IN ( * ) DO echo %%~na

Run FOR /? for a list of all the variable substitutions.

Posted

There are a multitude of ways of achieving the result you require!

Here's one:

FOR /F "DELIMS=" %%? IN ('DIR/B/AD "%USERPROFILE%\START MENU"') DO ECHO/%%?

[Edit]

Note I changed the name to Start Menu from Desktop in my example because there will always be at least one folder there for testing the code. The 'Desktop' on the other hand, I believe should only really contain links to objects, not the actual objects themselves.

[/Edit]

Posted
Run FOR /? for a list of all the variable substitutions.

I have read over the variable substitutions many times and even spent hours on the net reading up on it... Just couldn't quite get the hang of it... A little to complicated for me I guess. Been doing a lot of batch scripting lately, so my brain is kinda out-of-sync atm... Interesting stuff nevertheless!

Thanks guys! Will give it a go.

Posted
Just couldn't quite get the hang of it.

Reference (with examples):

http://www.robvanderwoude.com/

http://www.robvanderwoude.com/ntfor.html

You can now use the following optional syntax:

%~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

The modifiers can be combined to get compound results:

%~dpi - expands %i to a drive letter and path only

%~nxi - expands %i to a file name and extension only

%~fsi - expands %i to a full path name with short names only

%~dp$PATH:i - searches the directories listed in the PATH environment variable for %i and expands to the drive letter and path of the first one found.

%~ftzai - expands %i to a DIR like output line

In the above examples %i and PATH can be replaced by other valid values.

Just be careful to pick your FOR variable letters to not conflict with any of the format specifier letters if you plan on using the enhanced substitution logic.

jaclaz

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...