Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dos command to sperate file name and extension into variables

I need to copy a file x.dtsx from location a to location b.

If x.dtsx already exists in b then I need to rename x.dtsx to x_Standby.dtsx Then, after renaming copy x.dtsx to b

My current code looks like this:

if exists %1 rename %1 %(should be 1_standy.extension)
xcopy %1 %2
like image 346
akhil vangala Avatar asked Jan 05 '12 20:01

akhil vangala


People also ask

What is %% f in batch file?

For simple batch files, a single character such as %%f will work. You can use multiple values for variable in complex batch files to distinguish different replaceable variables.

What is DIR command in DOS?

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

How do I show only file names in a directory?

/W - Displays only filenames and directory names (without the added information about each file) in a five-wide display format. dir c:*. This form of the DIR command will also display directories. They can be identified by the DIR label that follows the directory name.

How do you find the extension of a file in bash?

Using Bash, there's also ${file%. *} to get the filename without the extension and ${file##*.} to get the extension alone. That is, file="thisfile.


1 Answers

If you use the Command Processor Extensions (which is default on Windows 2000 and later) then you can use the following optional syntax:

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file

The modifiers can be combined to get compound results:

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only

So your command would look something like this:

if exist %2\%~nx1 ren %2\%~nx1 %~n1_standby%~x1
like image 86
Neil Avatar answered Sep 30 '22 19:09

Neil