Jump to content

Determine a filename via batch-file


Recommended Posts

Hi guys out there :hello:

I'm searching for a solution to determine a complete filename by using the following command:

dir %tmp% | find ".inf"

The filename should be saved to a variable.

Why I would do this:

In the %tmp% folder is just one .inf file, but the name of the file changes sometimes so I'd like to have a flexible script to determine the .inf file name in this folder.

Hope anyone can help (I'm sure) ;)

Thank you

Edited by HØLLØW
Link to comment
Share on other sites


Is this "real" DOS or 2K/XP/later?

If the latter use variable expansion:

FOR /F %%A IN ('DIR /B %tmp% ^| FIND ".inf"') DO (

ECHO %%A

ECHO Name is %%~nA

SET Filename=%%~nA

ECHO Extension is %%~xA

)

SET Filename

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

jaclaz

It's XP/2K3.

This should save the filename to a variable named %IFN%

FOR %%# IN ("%TMP%\*.INF") DO SET "IFN=%%~n#"

Thank you Yzöwl, but it cuts the file extension if I ECHO the variable!

Is it possible to show the full filename WITH the extension?

Thanks

Link to comment
Share on other sites

Is it possible to show the full filename WITH the extension?

I don't want to seem grumpier than I normally am, but don't you think that taking spoon feeding up to this level is a bit too much? :unsure:

READ the given link :realmad: :

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

What is the difficult part in using this info:

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

to modify the given examples to get what you want? :w00t:

jaclaz

Link to comment
Share on other sites

This should save the filename to a variable named %IFN%

FOR %%# IN ("%TMP%\*.INF") DO SET "IFN=%%~n#"

Thank you Yzöwl, but it cuts the file extension if I ECHO the variable!

Is it possible to show the full filename WITH the extension?

Thanks

Of course without the knowledge to make the change directly yourself you could have simply appended the extension!
FOR %%# IN ("%TMP%\*.INF") DO SET "IFN=%%~n#.INF"

Now in order for you to implement the change as shown by jaclaz above, I'll change my code to help you understand the examples provided.

FOR %%i IN ("%TMP%\*.INF") DO SET "IFN=%%~ni"

The main thing to note is that in a batch file the percent needs to be escaped by another therefore these are what you need to use

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

Link to comment
Share on other sites

To be picky, as always, it can be considered unneeded additional information ;), after having read the original page, one could simply open the examples in Notepad and Search and replace:

searching for

%~n

and replacing it with

%~nx

which should give a result not entirely unlike :whistle::

searching for

%%~n

and replacing it with

%%~nx

:P

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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