issue Rename files in sub directories - windows

I'm trying to rename the below the name of files.
as you can see the some files have ".DSD".
So I want to remove these ".DSD"
I was just refering other ansering from here such as
for /r %x in (*.DSD) do ren "%x" *.dad
But it does not work so
what am i supposed to do this ?
before after
DDS1_150223.cpj.DSD DDS1_150223.cpj
DDS1_150211.cpj.DSD DDS1_150211.cpj
72 ranndom value.xls 72 ranndom value.xls
FREQUENCY_AGILE_MEM.mif.DSD FREQUENCY_AGILE_MEM.mif
.... ...

This command will work:
for /r %a in (*.DSD) do #move "%a" "%~pa%~na"
The command above make use of some 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

Related

How to get only file name in 'for' batch command and not file extension

When using this code to upload video's to YouTube, the title that gets named has the file extension at the end (in this case .mp4)
for %%f in (*.mp4) do python --file="%%f" --title="%%f"
use for %%f in (*.csv) do echo "%%~nf"
for more info do a for /? .. this is the part that you would liek to see
In addition, substitution of FOR variable references has been enhanced.
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

in windows 7 .bat file can one get the long filename from a short one

specifically in a batch file in windows 7 one can use the line
set MYDIR=%~dps0%
to get the directory path containing the batch file.
On the windows 7 machine it contains generated 8.3 names for elements of the path.
Is there a way to get the "long filename" version of this path? from within a ".bat" file ??
Just get rid of the s.
set MYDIR=%~dp0
From for /?:
%~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

Filename without Extension in Batch Script

for %%a in (.\*.jpg) do
The code above will store every jpg picture's name in %%a, but it stores the full name with the file extension, for example "Q.jpg".
I'm using a cmd utility for resizing images,
resize /width:100 %%a %%a.jpg
It will resize "Q.jpg" and then name it to "Q.jpg.jpg", as you can see the extension is now a part of file name!!!
I want to avoid it.
Simple, type for /? for more info:
To only get filename:
for %%a in (.\*.jpg) do resize /width:100 %%a %%~na.jpg
Quoting Windows batch help (type for /?):
%~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
And that should help you with any further problems.

CMD Script issue with Delims

I have the below cmd script that is not yielding what I would expect:
set parent=src\Sandbox
set child=src\Sandbox\SandboxTest
echo %parent%
for /F "tokens=* delims=%parent%\" %%i in ("%child%") do set DIRNAME=%%i
echo %DIRNAME%
%DIRNAME% is getting returned as "Test", but I expect DIRNAME to be 'SandboxTest'.
Anyone have any ideas why this is? Is there an issue with my delims?
Thanks,
Keith
You want to use variable reference syntax.
set parent=src\Sandbox
set child=src\Sandbox\SandboxTest
echo %parent%
for /F %%i in ("%child%") do (
set DIRNAME=%%~ni
)
delims is a array of characters used to split
set parent=aeiou
set child=abcdefghijklmnopqrstuvwxyz
for /F "tokens=1-10 delims=%parent%\" %%a in ("%child%") do (
echo.%%a
echo.%%b
echo.%%c
echo.%%d
echo.%%e
)
It splits on characters not words.
bcd
fgh
jklmn
pqrst
vwxyz
type for /? for the full range of variable reference syntax.
In addition, substitution of FOR variable references has been enhanced.
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
DELIMS does not define a delimeter phrase - it defines a set of delimiter characters. None of the characters in "Test" appear in you DELIMS set, hence the result.
In your particluar cited case, you can get your desired result using:
for %%F in ("%child%") do set DIRNAME=%%~nxF

How do I transform the working directory into a 8.3 short file name using batch? [duplicate]

This question already has answers here:
Get DOS path instead of Windows path
(12 answers)
Closed 5 years ago.
I'm writing a build script, and if the directory the user's building the script contains spaces, everything falls apart. To go around that, I thought of using 8.3 filenames so that drive:\Documents and setttings\whatever becomes drive:\Docume~1\whatever . The current directory can be found by querying the environment variable %CD%.
How do I transform %CD% into a short file path?
for %f in ("%cd%") do #echo %~sf
Edit: don't forget to use %% if you are using it in batch file. like this
for %%f in ("%cd%") do #echo %%~sf
On my machine:
C:\Users\preet>cd "\Program Files"
C:\Program Files>for %f in ("%cd%") do #echo %~sf
C:\PROGRA~1
Other options:
In addition, substitution of FOR variable references has been enhanced.
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
Better: use quote characters (") around all your paths.

Resources