Batch rename file invert position name - batch-file

My files have names like:
andromeda_01.02.19.xlsx
I need to change it to:
andromeda_20190201.xlsx
Removing the dots, using a batch file.
I could only isolate the date, using batch:
#echo off
RENAME "andromeda_*xlsx" "//////////////////*.xlsx"
Can someone help?

For andromeda_dd.mm.yy.xlsx files, you should use:
#echo off
for /F "tokens=2-4 delims=._" %%A IN ('dir /B /A-D andromeda_*.xlsx') do (
rename "andromeda_%%A.%%B.%%C.xlsx" "andromeda_20%%C%%B%%A.xlsx"
)

This will ensure that we do not rename files already renamed. i.e only rename andromeda_dd.mm.yy.xls and wil not rename andromeda_yyyymmddxls Additionally, we will check if a file ro rename to does not exist already and tell you about it if it does. Also, this assumes the century 20yy as there are no other indicators in your filename proving another:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=_" %%i in ('dir /b /a-d andromeda_*.*.*.xlsx') do (
set "manip=%%~nj"
if exist "%%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj" (
echo file %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj already Exists!
) else (
rename %%i_%%j %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj
)
)

Related

batch with a lot of for loops

So i made this batch to psarc extract all *.pak files, which will always give *.mbin files that often have the same name. it's then supposed to look for all extracted subfolders and then look for the last created *.mbin file from all of the subfolders and rename it to its current name plus a number. but i get very weird results.
setlocal ENABLEDELAYEDEXPANSION
set number=0
cd /d "C:\Users\storm\Desktop\No Man's Sky\GAMEDATA\PCBANKS\MODS"
for /r %%f in (*.pak=) do (
psarcMBIN.exe extract %%~nxf
set /A number=!number!+1
FOR /F "tokens=" %%a in ('dir /a:d /s') do (
cd /d "%%a"
FOR /F "tokens=" %%G in ('dir /b /a:-d /o:d "*.mbin"') do set newest=%%G
rename "!newest!" "!newest!!number!.mbin"
)
)
pause
The result basically is just:
1. psarc extracted
2. psarc tried to extract next file but cannot overwrite
The other parts of the main for loop are not even shown

Renaming .txt files in bulk

I downloaded about 34000 books in .txt format from Project Gutenberg. Now I want to rename all of them by its content. For example every text file includes its "Title" and "Author's Name" so I want to rename all the text files on its "Title" and "Author's Name" by some commands.
I created a batch file. It runs but is not renaming the files. This is my code:
#echo off&setlocal
cd E:\Test
for /f "delims=" %%i in ('dir /a-d/b *.txt') do (
set "nname="
set "fname=%%~i"
for /f "usebackqskip=7delims=" %%f in ("%%~i") do if not defined nname
set "nname=%%f"
setlocal enabledelayedexpansion
set "nname=!nname:~0,40!"
echo rename "!fname!" "!nname!"
endlocal
)
You can use this as a base
#echo off
setlocal enableextensions disabledelayedexpansion
rem Change to source folder
pushd "e:\test" && (
rem Where the renamed files will be placed to avoid re-rename
if not exist renamed\ md renamed
rem For each input file
for %%f in (*.txt) do (
rem Retrieve the data from inside the file
set "author=" & set "title="
for /f "tokens=1,* delims=: " %%a in ('
findstr /b "Author: Title:" "%%~ff"
') do if not defined %%a set "%%a=%%b"
rem If the fields have been retrieved then do the rename
if defined author if defined title (
setlocal enabledelayedexpansion
for /f "delims=" %%a in ("!author! - !title!") do (
endlocal
echo move "%%~ff" "renamed\%%a%%~xf"
rem NOTE: operation is only echoed to console
rem if console output seems correct, then
rem remove the echo command
)
)
)
rem Done. Return to previous active directory
popd
)
Of course, filesystem has rules about what is allowed in a file name and, not knowing what kind of characters can be found, this code could and probably will fail to rename some files.
Your current script will just print the rename commands, not execute them. You should remove echo (after checking what it produces) in this line:
echo rename "!fname!" "!nname!"
Your script also has a few formatting issues. There should be spaces like this:
for /f "usebackq skip=7 delims=" %%f in ("%%~i") do
And there should be no newline just after:
if not defined nname

return the name of a file using a batch script

I am trying to write a script that will search a given directory, find files ending in a .ready_go extension, rar them (saving the rar as the file name.rar) and then move onto the next file with the same extensions and do the same.
I am not having luck assigning a variable "filename" to the name of the file found in the loop. this variable will be used to name the rar archive
so far this is what i have:
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo %filename%
echo %dsource%
pause
)
its echo'ing the dsources variable fine, but will not echo the filename variable.
I also tried just "echo %%f" which also did not work.
some help would be appreciated. thanks.
You need to use delayedexpansion since you are setting the varable inside the loop.
try this:
setlocal enabledelayedexpansion
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo !filename!
echo %dsource%
pause
)

batch to rename multiple files

I have some files that I need to rename and as the volume is quite big, I was thinking of using a batch file since all I need to do is:
u_ex140429.log >> u_ex140429_01.log
But all the codes I found either place the _01 at the beginning or at the end of the file:
_01u_ex140429.log or u_ex140429.log_01
If there is a thread somewhere here about this, I do apologize for I have been looking for quite a while.
Thanks for your help guys.
This adds _01 before the extension of each .txt filename in the current folder without the limitation of a plain for-in-do command.
#echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do ren "%%a" "%%~na_01%%~xa"
Try using this.
#echo off
for %%f in (*.*) do (move %%f %%~nf_01%%~xf )
technet
a for loop works just fine ...
#echo off
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
for /L %%n in (1 1 %%f) do (
for %%a in (*.*) do (
rename "%%a" "%%~na_%%n%%~xa"
)
)
)
place the batch file in the directory containing your files to be renamed

Create folder based on group of files

I have files with varying names...
Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
I want to move all tim files into Tim folder and jack files to Jack folder etc.
Can it be done using .bat files ? If yes, please share the code for it.
Thank You.
#echo off
setlocal
set sourcedir=c:\sourcedir
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d "%sourcedir%\*-*.*") do (
md "%sourcedir%\%%a" 2>nul
echo move "%sourcedir\%%a-%%b" "%sourcedir%\%%a\"
)
Note that the 2>nul suppresses error messages created when an attempt is made to recreate an existing directory
The MOVE is merely ECHOed. Remove the ECHO keyword to activae the move. It may also be prudent to append >nul to the MOVE statement to suppress the "1 file(s) moved" message.
As you may know, there are no arrays in Batch. So let's just use one.
#ECHO OFF &SETLOCAL
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d *-*.jpg') do set "$%%a=%%a"
for /f "tokens=1*delims==" %%a in ('set $') do robocopy "%cd%" "%cd%\%%b" "%%b*" /mov /l
Remove /l from robocopy to make it working.
EDIT - Changed code to reduce the number of file moves. Now, all files with the same prefix get moved in one command.
#echo off
setlocal enableextensions
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-*.jpg") do (
rem if the file still exists (maybe it has been moved)
rem then split the file name with the dash as delimiter
if exist "%%~ff" for /F "tokens=1 delims=-" %%s in ("%%~nf") do (
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "%%~s"=="" (
robocopy "%_dir%" "%_dir%\%%~s" "%%~s-*.jpg" /mov /njs /njh
)
)
)
endlocal
EDIT2 - Changed to adapt to comments
#echo off
setlocal enableextensions enabledelayedexpansion
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-??.jpg") do (
rem if the file still exists (maybe it has been moved)
if exist "%%~ff" (
rem get the filename without the 3 last characters
set "_target=%%~nf"
set "_target=!_target:~0,-3!"
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "!_target!"=="" (
robocopy "%_dir%" "%_dir%\!_target!" "!_target!-??.jpg" /mov /njs /njh
)
)
)
endlocal

Resources