Filename without Extension in Batch Script - windows

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.

Related

issue Rename files in sub directories

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

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

How to create subfolders while using FOR /F in a Batch file

I want to process a folder and files in sub folders and after processing want to move files to new location (folder).
Current command is working but it is not creating sub folders and generating all new files in same folder (i.e. output folder)
for /F %%i in (filelist.txt) do (process.exe %%i > output\%%~nxi)
I need it to save it in same folder structure, as its source folder.
Where filelist.txt (source folder) is:
c:\backup\oldwork\browse.asp
c:\backup\oldwork\capital.asp
c:\backup\oldwork\make.asp
c:\backup\oldwork\conf\config.asp
c:\backup\oldwork\conf\global.asp
and I want my script to generate output (destination folder) like:
c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
c:\backup\output\conf\config.asp
c:\backup\output\conf\global.asp
Currently above For /F command is generating output like:
c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
**c:\backup\output\config.asp** (not following directory structure)
**c:\backup\output\global.asp**
The reason your code doesn't work is to do with what your outputting:
output\%%~nxi
Where if you check your documentation you would see:
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:
%~nxI - expands %I to a file name and extension only
Since you are only expanding to name and extension, the folder structure is ignored.
Solution:
What I would do in your situation is this, change your filelist.txt to this format:
browse.asp
capital.asp
make.asp
conf\config.asp
conf\global.asp
And then change your for-loop as such:
for /F %%i in (filelist.txt) do (process.exe c:\backup\oldwork\%%i > output\%%i)
Which should work out for you.
for /F %%i in (filelist.txt) do (md "output%%~pi"&process.exe %%i > "output%%~pnxi")
should do the task, creating output\backup\oldwork\browse.asp etc. (for lack of adequate description of required result)

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