Renaming parts of files with Windows Batch - windows

How could i rename a certain part of all files inside a directory.
Example
[HorribleSubs] File1 [720p].mkv
[HorribleSubs] File2 [1080p].mkv
Must be renamed into
File1.mkv
File2.mkv

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2delims=[]" %%a IN (
'dir /b /a-d "%sourcedir%\[*]*[*].mkv" '
) DO (
SET "newname=%%a"
ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv"
)
GOTO :EOF
This should fix your problem.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

This is a simple batch you can use to rename a single file manually.
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause
this has a loop to do multiple files manually
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
ping -n 3 127.0.0.1 >NUL
goto begin
its real simple hope it helps. fyi it works on windows XP

place file In directory you wanna rename
#echo off
setlocal enabledelayedexpansion
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
set /a "nof=%%f-2"
for /L %%n in (1 1 !nof!) do (
for %%a in (*.mkv) do (
rename "%%a" "file%%n%%~xa"
)
)
)

Related

compressing multiple files into bzip2

so i found this nice batch for window that would compress every file of the same extension in the same directory into bzip2 by dragging and dropping any of the files into it but i would like to take it further and make it that when i drag and drop a folder into it. it would compress all the files in it, including the sub-folders until it reaches the end. obviously i would guess it has something to do with looping with using %%d but i could not exactly figure it out.
#echo off
if [%1]==[] goto usage
for /f %%i in ("%1") do (
echo %%~di
echo %%~pi
echo %%~xi
set rootpath="%%~di%%~pi*%%~xi"
)
for %%f in (%rootpath%) do (
"C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9
del "%%f" /s /f /q
)
echo Finished operations!
goto exit
:usage
echo You have to drag and drop a file on this batch script!
echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
echo The only thing you really need is to change the path to your 7-Zip installation
echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically
:exit
pause
I share with you this helpful and well commented batch script posted by enteleform on superuser
I just modified this variable Set archivePath="%%~x.zip" to Set archivePath="%%~x.bz2"
How to make 7-zip do a whole bunch of folders
#Echo OFF
SetLocal EnableDelayedExpansion
Rem // 7-Zip Executable Path
Set sevenZip="C:\Program Files\7-Zip\7z.exe"
Rem // START: NewLine Variable Hack
Set newLine=^
Rem // END: NewLine Variable Hack !! DO NOT DELETE 2 EMPTY LINES ABOVE !!
Rem // Set ErrorLog Variables
Set errorCount=0
Set separator=--------------------------------------------------------
Set errorLog=!newLine!!newLine!!separator!!newLine!!newLine!
Set errorPrefix=ERROR #:
Set successMessage=All Files Were Successfully Archived
Rem // Loop Through Each Argument
SetLocal DisableDelayedExpansion
for %%x in (%*) do (
Rem // Use Current Argument To set File, Folder, & Archive Paths
SetLocal DisableDelayedExpansion
Set filePath="%%~x"
Set directoryFiles="%%~x\*"
Set archivePath="%%~x.bz2"
SetLocal EnableDelayedExpansion
Rem // Source Is A Folder
if exist !directoryFiles! (
Set sourcePath=!directoryFiles!
)
Rem // Source Is A File
if not exist !directoryFiles! (
Set sourcePath=!filePath!
)
Rem // Print Separator To Divide 7-Zip Output
echo !newLine!!newLine!!separator!!newLine!!newLine!
Rem // Add Files To Zip Archive
!sevenZip! A -TZIP !archivePath! !sourcePath!
Rem // Log Errors
if ErrorLevel 1 (
Set /A errorCount=errorCount+1
Set errorLog=!errorLog!!newLine!!errorPrefix!!sourcePath!
)
)
Rem // Print ErrorLog
if !errorCount!==0 (
Set errorLog=!errorLog!!newLine!!successMessage!
)
Echo !errorLog!!newLine!!newLine!!newLine!
Rem // Keep Window Open To View ErrorLog
pause
You could probably do that with a single line batch-file:
#For %%G In ("%~1")Do #If "%%~aG" GEq "d" (For /F Delims^= %%H In ('""%__AppDir__%where.exe" /R "%%~G" * 2>NUL|"%__AppDir__%findstr.exe" /EVIL ".bz2""')Do #"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1)&"%__AppDir__%timeout.exe" /T 3
If you'd like it over multiple lines for readability:
#For %%G In ("%~1") Do #If "%%~aG" GEq "d" (
For /F "Delims=" %%H In (
'""%__AppDir__%where.exe" /R "%%~G" * 2>NUL | "%__AppDir__%findstr.exe" /EVIL ".bz2""'
) Do #"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1
"%__AppDir__%timeout.exe" /T 3
)
These examples should only work if you drag and drop a directory onto it, or call it on the command line with a directory as the first argument.
#echo off
for /f %%i in ("%1") do (
echo %%~di
echo %%~pi
echo %%~xi
set rootpath="%%~di%%~pi*%%~xi"
)
for /R %%f in (*) do (
"C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9 -x!"packall.bat"
del "%%f" /s /f /q
)
echo Finished operations!
goto exit
:usage
echo You have to drag and drop a file on this batch script!
echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
echo The only thing you really need is to change the path to your 7-Zip installation
echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically
:exit
pause
Cheers to my friend Anya who found a solution, so the way it would work with the script above is that you make a batch file name it packall.bat
save it anywhere as it will delete itself at the end of the process.
when you want to compress bunch of files into bz2 you copy it and put it in inside a folder made with any name in your desktop.
make sure its name has no spaces nor its sub-folders as that may confuse batch and make it compress your desktop contents for some reason.
click on the batch, then it will compress all the files within the same folder its in and their sub-folders and automatically delete itself.
Video example:
http://billstuff.site.nfoservers.com/e79nwk69.mp4
IMPORTANT NOTE for some reason if there duplicate names of files with the same extension at sub-folders they will be deleted
Don't forget the folder and its sub-folder names should not have a space
Best of luck!

Using FOR command to list all the files in directory

I had 4 files with name like cp1_26_09_2015.txt, cp1_22_02_2015, cp1_28_09_2015 and etc in the same date format in 4 different subfolders of the D:\drive.
I wanted to search for a file one by one when I am using the FOR command to list all the files in directory and subdirectory.
Then it lists out 4 names of this text file in 4 different folders but I wanted the path to be extracted one by one so that I could perform operations one by one on each file. So till now I have constructed this:
SETLOCAL ENABLEDELAYEDEXPANSION
setlocal
SET /A MAXJ=1
SET /A J=1
set _T1=%date:.=%
set _year=%_T1:~-4,4%
set _month=%_T1:~-10,2%
set _day=%_T1:~-7,2%
cp1_26_09_2015.txt
set _prefix=cp1
PAUSE
FOR /F "usebackq tokens=*" %%i IN (`DIR /S /B D:\cp1*.txt`) DO ( IF NOT "%%~nxi"==**%_prefix%_%_year%_%_month%_%_day%.txt** (
SET XCOUNT_!J!=%%~i
SET MAXJ=!J!
SET /A J+=1
)
)
SET XCOUNT
SET /A J-=1
#echo COUNT OF FILES IS : %J%
PAUSE
Now the problem here is when I try to remove today's date file in the search with file name as cp1_28_09_2015.txt where date variables in the extension name are provided in %_month%, %_year% and %_day% this is not working, reporting a syntax error.
Make a BAT file and try like this :
#echo off
FOR /R "D:\" %%i in (*_26_09_2015.txt) do call:test %%i
echo Done !
exit/b
:test
echo working on : %1
echo Do what you want with %1
NEW EDIT :
Try this :
#echo off
set "_year=%date:~6,4%"
set "_month=%date:~3,2%"
set "_day=%date:~0,2%"
set "_prefix=cp1"
For /f "tokens=*" %%i IN ('DIR /S /B cp1*.txt') do (
If /i not "%%~nxi"=="%_prefix%_%_day%_%_month%_%_year%.txt" (
call:next "%%~nxi")
)
exit/b
:next
echo make something with : %1
echo who don't have the today date
pause
In case you want it to search for a single file, then make a file listtest.cmd like this:
#echo off
set testparams=%1
echo Listing files with name %1.
echo ====
FOR /R "D:\" %%i in (*_26_09_2015.txt) do call :filetest %%i
echo ====
echo Done.
exit /b
:filetest
if %1 == %testparams% echo %1
Then run it from the command line using the following syntax;
listtest <filesearch>
where filesearch is the file you want it to find.
If you don't know how to make CMD files and running them from command line, please comment this answer.

Batch Unrar after check file extension

i am trying to Unrar 400 files via batch script.
in my folder files mixed with videos
some file name is "love.blood.repack.mp4"
i have add .r for find rar file in my script
but script show with videos file like "love.blood.repack.mp4"
how to check if extension have .rar .r001 .r01 .r02 etc and then run unrar ?
only check extension not full file name
c:\unrar.bat c:\mymixedfolder
My windows .bat batch script
#echo off &setlocal
set /a nfile=0
echo Copying directory structure from %1 to %2 ...
xcopy /T "%~1" "%~2"
REM walk directory structure and convert each file in quiet mode
set "sourcefolder=%~1"
set "targetfolder=%~2"
for /R "%sourcefolder%" %%a in (*.r*) do (
echo converting "%%~nxa" ...
set "sourcefile=%%~fa"
set "sourcepath=%%~dpa"
set "targetfile=%%~na.flv"
setlocal enabledelayedexpansion
set "targetfolder=%targetfolder%!sourcepath:%sourcefolder%=!"
echo "%%~nxa"
endlocal
set /A nfile+=1
)
echo Done! Unrar %nfile% file(s)
try:
#ECHO OFF &SETLOCAL disableDelayedExpansion
SET "sourcefolder=."
FOR /R "%sourcefolder%" %%a IN (*.r*) DO (
FOR /f "delims=" %%b IN ('echo %%~xa^|findstr /r "\.r.*"') DO ECHO %%~a
)
Here is another option:
#ECHO OFF
SET "sourcefolder=."
FOR /R "%sourcefolder%" %%a IN (*.r??) DO (
if /i "%%~xa"==".rar" DO ECHO %%~a
)

Windows cmd sort files by file type and store them in folder

I would like to have a batch file wich will sort files by file type and sorts them into folder.
For example I will run this batch file in some folder and .PDF files will be saved in "PDF" folder, same to do with other file types. Is possible to do that in command line?
Thank you.
Please put the code below in a .bat file and save it to your folder with files and run it.
#echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpnxa" NEQ "%~dpnx0" (
rem check if extension forlder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Copy (or change to move) the file to directory
copy "%%a" "%%~dpa%%~xa\"
)
)
Try this:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "fol=%%~xa" & set "fol=!fol:.=!"
if not exist !fol! md !fol!
for /f %%b in ('dir /on /b *.*') do (
if %%~xb EQU .!fol! move %%~nxb !fol!
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET destdir=c:\destdir
SET "extdone=:"
FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
SET ext=%%~xa
IF DEFINED ext (
SET extdone|FIND /i ":%%~xa:" >NUL
IF ERRORLEVEL 1 (
SET extdone=:%%~xa: !extdone!
IF EXIST "%destdir%\!ext:~1!" (
ECHO MOVE "*%%~xa" "%destdir%\!ext:~1!"
) ELSE (ECHO no MOVE "%%~xa")
)
)
)
GOTO :EOF
This batch should do as you ask.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files.
I've assumed that you only wish to move the files if destinationdirectory\extensionfound exists. If you want to create a directory for a new extension, simply add a line
md "%destdir%\!ext:~1!" 2>nul
after the SET extdone=:%%~xa: !extdone! line.

dos script to find some files , create a folder in the same directory and move them in

i want to create a dos script (.bat) to search on all sub folders and whenever it finds a file with the word MK11 in the file name it must create a folder named archive and move the file in it.
example:
c:\folder1\folder2\folderX\fileMK11.txt -> c:\folder1\folder2\folderX\archive\fileMK11.txt
c:\folder1\folder3\fMK11ile.txt -> c:\folder1\folder3\archive\fMK11ile.txt
I tried to make the following script from examples i have seen but the problem is that it creates the folder "archive" in the directory where the script is instead of the directory where the file is found.
setlocal ENABLEDELAYEDEXPANSION
set /a c=0
FOR /R %%i in (*MK11*) do (
set /a c=c+1
md archive
move "%%i" archive
)
endlocal
I think this script will get you down the road. I echoed a COPY command rather than a MOVE command, but some of the hard part is done.
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET TEMPFILE=%TEMP%\afinder_%RANDOM%_%RANDOM.tmp
DIR /S /B /A-D *MK11* >%TEMPFILE%
FOR /F "usebackq delims=" %%f IN (`type %TEMPFILE%`) DO (
ECHO "%%f"
FOR /F "delims=\ tokens=*" %%a IN ("%%f") DO (
SET PNAME="%%~pa"
ECHO PNAME is set to !PNAME!
ECHO "!PNAME:~-9,7!"
REM Check to see if this file is already in an Archive directory.
IF "!PNAME:~-9,7!" == "Archive" (
echo got one
) else (
echo not one
IF NOT EXIST "!PNAME!\Archive" (MKDIR "!PNAME!\Archive")
echo COPY %%f "!PNAME!\Archive"
)
)
)
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B 0

Resources