Dos Command rename folders and subfolders with special characters - dos

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

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

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

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

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

Renaming files by folder hierarchy

I have many files with the following structure:
1969/ar/1.jpg
1969/ar/2.jpg
1969/he/1.jpg
1969/he/2.jpg
1969/en/1.jpg
1969/en/2.jpg
1970/ar/1.jpg
etc...
I want to rename all of them, with one command, to one directory, while their names reflect their original folder location.
1969_ar_1.jpg
1969_ar_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1970_ar_1.jpg
etc...
Is it possible to do so with one command or a batch file?
Thanks!
You may do that to move the files to the base folder with this command-line:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
Execute it from the folder that contain the 1969, 1970... folders. IMPORTANT!: Delayed Expansion must be active in order for this line to work, so you must previously activate it executing cmd.exe with /V switch this way: cmd /V.
For example:
>xcopy test backup /s
test\1969\ar\1.jpg
test\1969\ar\2.jpg
test\1969\en\1.jpg
test\1969\en\2.jpg
test\1969\he\1.jpg
test\1969\he\2.jpg
test\1970\ar\1.jpg
7 File(s) copied
>cd test
>dir /B
1969
1970
>for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
>dir /B
1969
1969_ar_1.jpg
1969_ar_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1970
1970_ar_1.jpg
Modify the line this way to move the files to another folder:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" "\other\folder\!f:\=_!"
Or via this Batch file:
#echo off
setlocal EnableDelayedExpansion
for /R %%a in (*) do set f=%%a& set f=!f:%cd%\=!& move "%%a" "\other\folder\!f:\=_!"
Run this from the base of the tree that contains all *.jpg files.
Change the target folder to where you want the files to go:
Test it on some samples first.
#echo off
for /f "delims=" %%z in ('dir "*.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
move "%%~dpz\%%~nxb_%%~nxa_%%~nxz" "c:\target\folder"
)
)
)
pause
try this (look at the output and remove the word echo before move, if it is OK):
#echo off &setlocal
for /d %%i in (19* 20*) do (
cmd /c "for /r "%%i" %%j in (*.jpg) do #for %%k in ("%%~dpj.") do #echo move "%%~j" "%%i_%%~nk_%%~nxj""
)

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