Need a renaming script that strips filenames and adds folder name with increments - file-rename

I need a script that will allow me to strip filenames to files that are placed within folders, and apply the folder names to the files, and add an incremental number to the end of each filename.
So the filenames would look something like this in their original state:
gdgeregdja34gtj.jpg
And then look like this after the script is executed:
foldername>foldername001.jpg
foldername>foldername002.jpg
I have this script, which allows the folder name to prefix any filename of files within folders. But it doesn't strip the filenames.
#echo off
pushd "Folder"
for /d %%D in (*) do (
for %%F in ("%%~D\*") do (
for %%P in ("%%F\..") do (
ren "%%F" "%%~nxP_%%~nxF"
)
)
)
popd

This will echo the rename commands to the screen, so if it does what you want then remove the echo to make it work.
Filenames with ! characters will not be renamed.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (
set n=0
pushd "%%a"
for /f "delims=" %%b in (' dir /b /a-d 2^>nul ') do (
set /a n=n+1
set num=0000!n!
echo ren "%%b" "%%~nxa!num:~-4!%%~xb"
)
popd
)
pause

Related

Rename files depending on parent and grand-parent folder names

I need windows batch file to prefix filename in subfolders with name of folder & subfolder name
e.g. I have folder created for date 12012017 with following structure
d:\12012017\1234\ABC\file1.txt
d:\12012017\1234\ABC\file2.txt
d:\12012017\5678\PQR\file3.txt
d:\12012017\2345\XYZ\file4.txt
Need them to rename to
d:\12012017\1234\ABC\1234_ABC_file1.txt
d:\12012017\1234\ABC\1234_ABC_file2.txt
d:\12012017\5678\PQR\5678_PQR_file3.txt
d:\12012017\2345\XYZ\2345_XYZ_file4.txt
Tried following code but didn't work
set mydate=%date:~0,2%%date:~3,2%%date:~6,4%
REM ECHO %mydate%
D:
CD D:\%mydate%
#ECHO STARTING RENAME
pushd D:\%mydate%
for /d %%D in (*) do (
#ECHO 1- pushd "%%D"
#ECHO CD %%D
for /d %%X in (*) do (
#ECHO pushd "%%X"
for /r %%F in (*) do (
#ECHO 2- pushd "%%F"
for %%P in ("%%F\..") do (
#ECHO 3- pushd "%%P"
#ECHO 4 - ren "%%F" "%%~nxD_%%~nxF"
)
)
)
popd
)
popd
CD\
There are better ways to get the current date in the correct format, but I will preserve your existing method that is locale dependent.
It is important to use FOR /F with DIR on the inner loop instead of simple FOR because simple FOR can iterate files that have been already named, but FOR /F will not.
#echo off
:: Set current directory to today's date
pushd "d:\%date:~0,2%%date:~3,2%%date:~6,4%"
for /d %%A in (*) do ( %= Iterate all child folders =%
for /d %%B in ("%%A\*") do ( %= Iterate grandchild folders = %
for /f "eol=: delims=" %%F in ( %= Iterate result of DIR command =%
'dir /b /a-d "%%~B\*" 2^>nul' %= List all files in grandchild folder =%
) do ren "%%~B\%%F" "%%~A_%%~nxB_%%F" %= Rename each file =%
)
)
popd

Adding Parent Directory name as Prefix to sub directory files using batch file

Adding Parent Directory name as Prefix to sub directory files using batch file
I need to add the prefix of the parent directory to all the files present in sub directories
for example we receive html and text files within Directories 101 as parent directory and Creatives as sub directory
F:\Files\101\Creatives\filename.htm and filename.txt
F:\Files\102\Creatives\filename.htm and filename.txt
F:\Files\103\Creatives\filename.htm and filename.txt
here I want to omit the sub directory name (Creatives) and add 101_, 102_, 103_ as the prefix to the filenames present in sub directories
example 101_filename.htm also for the text file as 101_filename.txt in sub folders
with the below code I can add the prefix to the sub folder files but I can only replace it with a static value (Prefix_), here I need to add the first directory name(101, 102, etc) as prefix to all the files present in (F:\Files) folder
#echo off
pushd "F:\Files"
for /r %%j in (*) do (
rename "%%j" "Prefix_%%~nxj"
)
popd
#echo off
pushd "F:\Files"
for /d %%P in (*) do for /f "delims=" %%F in ('dir /b /s /a-d "%%P"') do rename "%%F" "%%P_%%~nxF"
popd
The inner loop is FOR /F with DIR command instead of FOR /R to eliminate possibility of loop renaming the same file twice.
I guess you could find a solution involving 2 for loop.
Something like
for /f "tokens=*" %%d in ('dir /b') do ( for /f "tokens=*" %%f in ('dir /s /b %%d') do ( rename "%%f" "%%d_%%~nxf" ) )
Best Regards,
Michel.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "source=u:\Files"
pushd "%source%"
for /r %%j in (*) do (
SET "prefix=%%~dpj"
CALL :setpfx
ECHO rename "%%j" "!Prefix!_%%~nxj"
)
popd
GOTO :EOF
:setpfx
SET prefix=!prefix:*%source%=!
SET prefix=%prefix:\=.%
FOR /f %%t IN ("%prefix%") DO SET prefix=%%~nt
SET prefix=%prefix:.=_%
SET "prefix=%prefix:~1%"
GOTO :eof
This may do what you require. The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO RENAME to REN to actually rename the files.

Rename folder in a loop using batch script

I am trying to write a script which will get all mt .avi files in a specific directory, rename the directory after each file and will call another script in a loop:
(the name of the directory is the input)
cd /D %1
dir *.avi > avi.list
set x=%1
set z=%1
cd ..
FOR /F "tokens=1-5" %G IN (avi.list) DO (
#set y=%K
rename %x% %y%
set x=%y%
C:\indexer\Indexer.exe %y%
)
rename %y% %z%
cd /D %z%
del avi.list
Thanks!!
Both %G and %K need to be %%G and %%K inside a batch file.
You will also need double quoting if the filenames contain spaces or other awkward characters.
There are better methods to get the filename and path (added filename alone): try this
#echo off
cd /d "%~1"
for /f "delims=" %%a in (' dir *.avi /b /a-d ') do (
echo filename without extension is "%%~na"
echo name is "%%~nxa"
echo filepath is "%%~dpa"
pause
)
At a cmd prompt read the help from: FOR /? and on the last page is the list of modifiers.

Dos Command rename folders and subfolders with special characters

How can I remove the special character using DOS Command on folder and subfolders?
Folders:
C:\FILE\FOLDER#1\PICTURE#100
C:\FILE\FOLDER#2\PICTURE#200
C:\FILE\FOLDER#3\PICTURE#300
C:\FILE\FOLDER#4\PICTURE#400
C:\FILE\FOLDER#5\PICTURE#500
OUTPUT:
C:\FILE\FOLDER1\PICTURE100
C:\FILE\FOLDER2\PICTURE200
C:\FILE\FOLDER3\PICTURE300
C:\FILE\FOLDER4\PICTURE400
C:\FILE\FOLDER5\PICTURE500
Thanks
Try this on some sample folders first. It will rename two levels of folders.
#echo off
setlocal enabledelayedexpansion
pushd "c:\file"
for /d %%z in (*) do (
pushd "%%z"
for /d %%a in (*) do (
set "folder=%%a"
ren "%%a" "!folder:#=!"
)
popd
)
for /d %%a in (*) do (
set "folder=%%a"
ren "%%a" "!folder:#=!"
)
popd
using renamer:
$ renamer --find "#" **
Removes the "#" from all files and folders recursively

Rename all files to lowercase, replace spaces

In a Windows command prompt, how can I rename all the files to lower case and remove all spaces?
Make a batch file
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ( ' dir /b /a-d *.* ') do (
set name="%%i"
set newname=!name: =!
rename "%%i" !newname!
)
NOTE: Run under a test directory and see if you have the expected result. I have not tested it.
EDIT: Forgot to say this will only remove the spaces.
I used this batch file to rename all folders and subfolders to lowercase names:
#ECHO OFF
CALL:GETDIRS
:GETDIRS
FOR /F "delims=" %%s IN ('DIR /B /L /AD') DO (
RENAME "%%s" "%%s"
CD "%%s"
CALL:GETDIRS
CD ..
)
GOTO:EOF
To make the trick of "lowercase" and "remove spaces" ...
In the given solution, in the 'dir' statement, use also "/l"
The /L statement in dir forces to lowercase the filenames in the
result.
As "Windows-RENAME" command, if you use the "same" filename, it will note convert from uppercase to lowercase.
ren XPTO.TXT xpto.txt
The result will always be : XPTO.TXT
To 'bypass' this, we use the ephemeral technique:
move old to temp, then -> move temp to new
Then the solution would be:
#echo off
if exist temporaryfilenametorename del temporaryfilenametorename /f/q
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir *.csv /l /b /a-d') do (
set name="%%i"
set newname=!name: =!
rename "%%i" temporaryfilenametorename
rename temporaryfilenametorename !newname!
)

Resources