How to remove file extension from the filename? - batch-file

I wrote some simple batch file that converts any audio/video files to .ogg (audio) format. For example music off youtube which is in .flv format. Here's the script:
set ogg_rate=1.3
set source_dir=f:\music
set dest_dir=f:\music\out
set my_temp=f:\temp
Rem Go to source dir
cd /d %source_dir%
for %%f in (*.*) do (
Rem Extract audio from avi file using ffmpeg
c:\encode\ffmpeg.exe -i "%source_dir%\%%f" "%my_temp%\%%f.wav"
rem Encode the audio with OGG encoder using 1.3 quality setting
cd /d %my_temp%
c:\encode\oggenc2.exe -q %ogg_rate% -o "%dest_dir%\%%f.ogg" "%my_temp%\%%f.wav"
rem delete all temp files
cd /d %my_temp%
del *.* /q
)
It works file, but I cannot figure out how to remove the unnecessary extension.
For example, if the input file is file.flv then after converting to wave it will be file.flv.wav, then after converting it to .ogg it will be file.flv.ogg.
How to get the file.ogg (without .flv)
BTW: instead of .flv it can be .mp4, .avi or another extension.

In your for statement, try using %%~nf instead of %%f. For statements in batch files have a lot of substitution options you can utilize:
for %%R in (*.*) do (
#echo %%~nR
)

Related

certutil -hashfile : processing multiple files in a drag-dropped folder

I'm trying to create a batch script that runs certutil -hashfile MD5 on each file in a folder and write the output to a file.
I have this code below except it only works on the files in the current folder,
I would like it to work such that when a folder is drag-dropped into the batch file .bat it processes that folder only.
for %%a in (*) do certutil -hashfile %%a MD5 >> MD5_log.txt
Also is there a way to get it to output spaces in the log file between iterations of the certutil output text?
It is actually very simple!
Simply change (*) to ("%~1\*") or other command-line arguments. If you have multiple drag-drop folders, do "%~1\*" "%~2\*", etc. Using quotes (") can prevent issue with space. So paths are now quoted. And %%a becomes %%~a, which means to de-quote.
Alternatively, you can set a variable containing all paths and process them one by one.
Result:
for %%a in ("%1\*") do certutil -hashfile "%%~a" MD5 >> MD5_log.txt
Store following file as .bat file and change testfolder and outputfile as desired.
#ECHO OFF
setlocal enabledelayedexpansion
:: Set the variables for this script.
set testfolder=C:\Test\
set outputfile=md5_files.txt
cd %testfolder%
echo > %outputfile%
for %%f in (".\*.*") do (
certutil -hashfile %%f SHA1 >>%outputfile%
echo %%ff
)
PAUSE

Batch convert wpd files to docx with LibreOffice

I'm trying to convert a bunch of wpd filed into docx with libreoffice, so far I have been able to achieve it but the resultant docx files are saved in just one folder (Ale) instead of Ale and its subdirectories, and what I want is for the docx files to be saved in the folder the wpd file are in. So far I have:
set path=%path%;"C:\Program Files (x86)\LibreOffice 5\program"
for /r %%f in (*.wpd) do (
soffice.exe -headless -convert-to docx:"MS Word 2007 XML" -outdir "S:\Temp\Ale" %%f)
Like #aschipfl said, go to the directory of each file and then do the conversion:
setlocal enableDelayedExpansion
set "path=%path%;C:\Program Files (x86)\LibreOffice 5\program"
for /r %%f in (*.wpd) do (
pushd %%~dpf
soffice.exe -headless -convert-to docx:"MS Word 2007 XML" "%%f"
popd
)
endlocal

Comparing and changing, if needed, lines of an ini file, with a batch file

I need a batch file to compare two lines, in two different files.
FILE1:
CONFIG1(TRUE / FALSE)
CONFIG2(TRUE / FALSE)
CONFIGFOCUS,99999,
FILE2:
CONFIG2(TRUE / FLASE)
CONFIG2(TRUE / FALSE)
CONFIGFOCUS,999999,
So what i would like to do, is a batch file that would
check throught every INI files of many folders, and would compare
the line of FILE1 'CONFIGFOCUS' and compare it with the config of
FILE2 'CONFIGFOCUS'. And I would like it to check IF CONFIGFOCUS == '99999',
change it for '999999'. for every INI files in different folders. IS this doable?
If yes, could you please explain how? (i am a beginner in Batch file, and I would like
to understand how to do it)
To change CONFIGFOCUS,99999, to CONFIGFOCUS,999999, in a tree of files, this should work:
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
Put repl.bat in the same folder as the batch file, which is in the main folder of your tree of INI files.
#echo off
for /r %%a in (*.ini) do (
type "%%a" |repl.bat "CONFIGFOCUS,99999," "CONFIGFOCUS,999999," >"%%a.tmp"
move /y "%%a.tmp" "%%a" >nul
)
#echo off &setlocal
set "startfolder=x:\inifiles"
:: change 'CONFIGFOCUS' from '99999' to '999999'
for /d /r "%startfolder%" %%a in (*) do sed -i.bak "/CONFIGFOCUS/Is/99999/999999/" "%%~a\*.ini"
:: list 'CONFIG1', 'CONFIG2' and 'CONFIGFOCUS' from all files to compare
for /d /r "%startfolder%" %%a in (*) do findstr /bic:"CONFIG1" /c:"CONFIG2" /c:"CONFIGFOCUS" "%%~a\*.ini"
sed for Windows

Windows Batch Lame Encode syntax

I need to get working properly a tiny Windows batch file (convert.cmd) that changes bitrate of all MP3 files in a specified folder. I need to pass 2 parameters to the batch file:
the path to the folder with MP3 files
bitrate I want to change them to.
I am using Lame.exe encoder. The lame.exe is in one place, convert.cmd can be in the same folder with lame.exe, but the folder with MP3 files can be anywhere. The original version (without parameters) was (and it's working fine if I place convert.cmd in the folder wit MP3 files):
#ECHO OFF
FOR %%f IN (*.mp3) DO (
D:\Apps\Lame\lame.exe -h -b 128 "%%f" "%%f.temp"
DEL "%%f"
REN "%%f.temp" "%%f"
)
PAUSE
Instead of 128 I need to pass "%2" and it will be a second command-line parameter, the bitrate, and for MP3 files folder path I need to pass "%1". So, I got this, but it's not working.
#ECHO OFF
FOR %%f IN (%1\*.mp3) DO (
D:\Apps\Lame\lame.exe -h -b %2 "%%f" "%%f.temp"
DEL "%%f"
REN "%%f.temp" "%%f"
)
PAUSE
How to make it work as described?
How can I ensure that my batch file convert existing files, and not creating new converted copies of them somewhere? Thanks a bunch ;) Cheers.
UPDATE
The location of convert.cmd is:
d:\Apps\Lame\convert.cmd, same folder with lame.exe
The location of MP3 files is:
d:\temp\xxx\
File1.mp3
File2.mp3
When I execute convert.cmd from the command line like this:
convert.cmd d:\temp\xxx\ 64
what I get in d:\temp\xxx\ is this:
File1.mp3.temp
File2.mp3.temp
Where did the converted files go?
Thanks.
Thanks, I have already figured out how to write this script.
If someone needs this type of conversion, here we go:
1 param - full path to the folder with mp3 files
2 param - bitrate to convert to
(remember, lame.exe does not preserve mp3 tags)
p.s. who needs mp3 tags anyways ? :)
#ECHO OFF
ECHO.
ECHO BEGIN CONVERSION
ECHO.
CD %1
DIR *.mp3
ECHO -------------------------------------------------------------------------------
ECHO THESE MP3 FILES WILL BE CONVERTED TO BITRATE %2 KBPS
ECHO -------------------------------------------------------------------------------
PAUSE
FOR %%f IN (*.mp3) DO (
ECHO -------------------------------------------------------------------------------
ECHO CONVERTING: %%f
ECHO -------------------------------------------------------------------------------
D:\Apps\Lame\lame.exe -h -b %2 "%%f" "%%~nf.wav"
DEL "%%f"
REN "%%~nf.wav" "%%f"
)
ECHO.
ECHO END CONVERSION
ECHO.
PAUSE

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

Resources