1.

Solve : DOS percent commands %1 ? ??

Answer»

I have a DOS batch file that converts some data

(runs app input filename output filename)

convert.exe %1 converted.sgm

I use %1 because that brings in the entire pathname of the file - with Windows drag-and-drop and I can pull a file from any other window onto my batch file shortcut.

That works great for one file at a time, and then I have to rename "converted" or move it somewhere ELSE. But I want to convert several at a time

Is there some other % command that I can use for the output filename (not the entire pathname - just the filename)? So that SMITH.TXT as the input filename could be SMITH.SGM as the output?

JHarper
Are you really using DOS? A lot of people say "DOS" when they really mean NT family (Windows 2000/XP/Vista) Command Prompt. I am not just being picky here, it does MAKE a difference to the answer.

In NT family command scripts ("batch files") you can modify the PARAMETER (if it is a file name!) referenced in the batch file as %1 using the following modifiers in the list below (edited from the output of FOR /?)

In this list, N is a number between 1 and 9.

So to get just the file name without an extension you would use e.g. %~dpn1

Your output file would be referenced in full as %~dpn1.sgm

so if %1 is D:\top folder\sub folder\smith.txt
then %~dpn1.sgm is D:\top folder\sub folder\smith.sgm

You may need quotes if there are spaces in the path name. Play around a bit and it will all be clear. Put echo lines in your batch to see what's going on and delete them later when it's all working the way you want it.

%~N - expands %N removing any surrounding quotes (") so if %1 is "C:\Program Files" then %~1 is C:\Program Files
%~fN - expands %N to a FULLY qualified path name
%~dN - expands %N to a drive letter only (including the colon e.g. "C:")
%~pN - expands %N to a path only
%~NN - expands %N to a file name only
%~xN - expands %N to a file extension only (including the dot e.g. ".txt")
%~sN - expanded path contains short names only
%~aN - expands %N to file attributes of file
%~tN - expands %N to date/time of file
%~zN - expands %N to size of file (in bytes)

The modifiers can be combined to get compound results:

%~dpN - expands %N to a drive letter and path only
%~nxN - expands %N to a file name and extension only
%~fsN - expands %N to a full path name with short names only

Touche...Windows command prompt.

I will try %~dpn1 thanks for the info.



Discussion

No Comment Found