HØLLØW Posted March 2, 2010 Share Posted March 2, 2010 (edited) Hi guys out there 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 March 2, 2010 by HØLLØW Link to comment Share on other sites More sharing options...
jaclaz Posted March 2, 2010 Share Posted March 2, 2010 (edited) 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 %%AECHO Name is %%~nASET Filename=%%~nAECHO Extension is %%~xA)SET Filenamehttp://www.robvanderwoude.com/ntfor.phpjaclaz Edited March 2, 2010 by jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted March 2, 2010 Share Posted March 2, 2010 This should save the filename to a variable named %IFN%FOR %%# IN ("%TMP%\*.INF") DO SET "IFN=%%~n#" Link to comment Share on other sites More sharing options...
HØLLØW Posted March 3, 2010 Author Share Posted March 3, 2010 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 %%AECHO Name is %%~nASET Filename=%%~nAECHO Extension is %%~xA)SET Filenamehttp://www.robvanderwoude.com/ntfor.phpjaclazIt'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 More sharing options...
jaclaz Posted March 3, 2010 Share Posted March 3, 2010 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? READ the given link :http://www.robvanderwoude.com/ntfor.phpWhat 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 stringThe 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 lineto modify the given examples to get what you want? jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted March 3, 2010 Share Posted March 3, 2010 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?ThanksOf 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 useYou 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 stringThe 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 More sharing options...
jaclaz Posted March 3, 2010 Share Posted March 3, 2010 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 %~nand replacing it with%~nxwhich should give a result not entirely unlike :searching for %%~nand replacing it with%%~nx jaclaz Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now