Can I batch rename files with folder name and sequence number? - windows

I can rename the files in all the directories and sub directories using the following script
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("index*") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&ExiT
is there a way to modify the script, which worked fine for me in the 2nd answer to rename files in a sequence when there is multiple files in the directory?
Like, the folder name is image and I want the files to be named image1.jpg image2.jpg (retaining the extension).. Help is much appreciated

Try this:
#Echo OFF
setlocal enabledelayedexpansion
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("index*") DO (
set /a "inc+=1"
Echo Ren: ".\%%~n#\%%#" "%%~n#!inc!%%~x#"
Ren "%%#" "%%~n#!inc!%%~x#"
)
POPD
)
Pause&Exit

Related

How to rename a file to the parent's parent folder name in batch

so I have a bunch of folders with similar directories to the below:
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png
I want to rename all the .png files the number just before the first randomStringofCharacters. So basically
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
Is there any batch script that can do this? I have tried:
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("*.png") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&Exit
Yet this only renames the file with the parent directory.
And if possible is there a way to move all such renamed .png files in to a newly made folder in .\page\ (the same place where the .bat is) with the folder name of page?
Thanks in advance!
Could be something like this (you have to drag and drop the main folder to the batch):
#echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
You should not use for /R to handle items in a predefined directory hierarchy depth:
#echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
Note, that this approach does not check whether there is only one .png file at each location.

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

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

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

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

Write batch file to count folders inside main folder

I am new to this, i am trying to write a batch file to calculate number of folders inside folder. Can someone please help me?
Here is folder system:
I have hundreds of folders like:
Area1
Area2
Area3
....
Inside each of above folder there is one folder named "Zone".
What i am trying to find is number of folders inside Zone folder for each Area1, Area2... so on.
start one folder above the AREAx folders:
#echo off &setlocal
for /d %%i in (*.*) do (
pushd %%i\ZONE
set /a count=0
for /d %%j in (*.*) do set /a count+=1
popd
call echo %%count%% folder(s^) in %%i\ZONE
)
endlocal
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f %%i IN ('dir /b/ad area*') DO (
IF EXIST ".\%%i\zone\." (
FOR /f %%c IN (' DIR /ad ".\%%i\zone\"') DO SET /a count=%%c-2 2>nul
ECHO ".\%%i\zone" : !count! directories
) ELSE (ECHO ".\%%i\zone" does NOT EXIST
)
)

Resources