I know there are similar posts, but non covers what I need.
I need to rename all the files and sub folders of a given folder and make them uppercase. I found this post which is great but only does the files OR subfolders:
Rename all files in folder to uppercase with batch
I tried doing nested FOR with no joy. According to the manual, /R is suppose to recur through folders, but it is not doing anything. Tried /D /R with no luck either. So I was hoping to use something like below:
#echo off
setlocal enableDelayedExpansion
pushd C:\MyFolder
for /R /D %%f in (*) do (
set "filename=%%~f"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Any ideas?
Parse a static list dir /B /S * to get all files and subfolders.
Read entire for /? for %%~nxf explanation.
#echo off
setlocal enableDelayedExpansion
pushd C:\MyFolder
for /F "delims=" %%f in ('dir /B /S *') do (
set "filename=%%~nxf"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%~f" "!filename!" >nul 2>&1
)
popd
endlocal
Read ren /? as well:
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
Related
I'm trying to make a batch file to find a file (for example Raihan.txt) and delete it or forced delete it(if its running).
In this case I want it to search all drives including USB drives.
I actually don't know much about batch coding, I searched it on internet and came up with those line and problem is I can't search all drives. I need some help here.
#echo off
set file_to_delete=Raihan.txt
set dir_to_look_in=C:\
:: Starting loop
for /f "tokens=1* delims=" %%f in ('dir /s /o /b "%dir_to_look_in%" ^| findstr "%file_to_delete%"') do (
echo INFO: Deleting: %%f
del /q "%%f"
)
I haven't run it because it's not even complete, one wrong move and I will be in big trouble.
Here is one example
#echo off
set file_to_delete=Raihan.txt
:: Starting loop to search through all drives
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%d:\%file_to_delete%" (
echo INFO: Deleting: "%%d:\%file_to_delete%"
del /q "%%d:\%file_to_delete%"
)
)
Here is another
#echo off
setlocal enabledelayedexpansion
set "file_name=Raihan.txt"
for /f "tokens=1-2" %%a in ('fsutil fsinfo drives ^| findstr /r "^[A-Z]:"' ) do (
if exist "%%a:\%file_name%" (
echo Deleting "%%a:\%file_name%"
del "%%a:\%file_name%"
)
)
#echo off
setlocal enabledelayedexpansion
set "file=example.txt"
for %%i in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%i:\" (
for /f "delims=" %%j in ('dir /s /b "%%i:\%file%"') do (
set "filepath=%%j"
del "!filepath!"
echo !filepath! deleted
)
)
)
echo All instances of %file% have been deleted.
pause
endlocal
I want to create a script, which lower and remove special character multiple text files.
my files in folder like this:
- ⚡ Document.txt
- [Review] Test File.txt
i want remove special char of filename like this
- document.txt
- review test file.txt
i've tried like this, but only lower filename. how to remove special character?
#echo off
setlocal enableDelayedExpansion
pushd %currentfolder%
for %%f in (*) do (
set "filename=%%~f"
for %%A in (a b c d e f g h i j k l m n o p q r s t u w x y z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Before
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\your files"
set "validchars=abcdefghijklmnopqrstuvwxyz1234567890. "
pushd "%sourcedir%"
FOR %%b IN (*) DO (
set "newname="
set "oldname=%%b"
call :validate
if /i "%%b" neq "!newname!" ren "%%~sb" "!newname!"
)
popd
GOTO :EOF
:validate
if not defined oldname goto :eof
set "c1=%oldname:~0,1%"
set "oldname=%oldname:~1%"
if "!validchars:%c1%=!" neq "%validchars%" set "newname=%newname%%c1%"
goto validate
Always verify against a test directory before applying to real data.
I predict it will have problems with some unicode characters and the usual suspects.
You could use pure powershell for this, or if you feel like continuing the use batch-file, just call powershell to assist:
#echo off
for %%i in (*) do for /f "delims=" %%a in ('powershell "$string='%%~ni';$String.tolower() -replace '[\W]', ''"') do echo ren "%%~i" "%%a%%~xi"
Note the echo at the end of the line, that is to test functionality by printing to screen before you do the actual renaming. Only remove echo when you're happy with the printed results.
From Filename : 1ab12345_def7890.txt
to 1AB12345_def7890.txt. Plese notice 1AB in uppercase.
I tried following but it is renaming whole file name to uppercase including extension.
#echo off
setlocal enableDelayedExpansion
pushd c:\some_dir
for %%f in (*) do (
set "filename=%%~f"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Can anybody please help me with this?
Thank you !
You simply need to isolate the leading three characters of filename first, perform your replacement, then prepend it back to the string.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
PushD "C:\some_dir" 2> NUL || GoTo :EOF
For %%G In (*) Do (Set "filename=%%~nG"
SetLocal EnableDelayedExpansion
Set "leading=!filename:~,3!"
For %%H In (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
) Do Set "leading=!leading:%%H=%%H!"
Ren "%%G" "!leading!!filename:~3!%%~xG" 1> NUL
EndLocal)
PopD
How to rename folder name using batch script ? I need to rename file name like:
a Filename1
b filename2
to
A
B
i.e make it uppercase and substring only to initial part.
So far I have come up with :
#echo "Renaming file"
for /D %%f in (C:\REN\*) do rename "%%f" "%%~nxf_myname"
pause
I'm assuming you actually want to rename folders found in C:\REN\, and not files.
I'm also assuming that by "initial part" you mean up until the first space character. If this is correct, then you want to use FOR /F to parse the name into tokens.
The SET search and replace functionality is case insensitive with regard to the search portion.
I'm creating variables for both the original and new names and I'm toggling delayed exapnsion on and off just in case the folder name contains a ! character. For variables are corrupted if value contains ! and delayed expansion is enabled.
#echo off
setlocal disableDelayedExpansion
echo Renaming folders
for /d %%F in (C:\REN\*) do (
for /f "eol= " %%A in ("%%~nxF") do (
set "name=%%F"
set "newName=%%A"
setlocal enableDelayedExpansion
for %%C in (
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
) do set "newName=!newName:%%C=%%C!"
ren "!name!" "!newName!"
endlocal
)
)
How to find all the *.txt files in any directory(i.e. c:\,d:\ etc.) through command prompt?
c:
cd \
dir /s *.txt
d:
cd \
dir /s *.txt
Following will search from root directory and its all accessible sub folders regardless of the folder you currently in.
dir \*.txt /s
or
dir c:\*.txt /s
dir d:\*.txt /s
etc
Try using dir *.txt
setlocal ENABLEEXTENSIONS
FOR %%A IN (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO #call :dumpdrive %%A
echo Done...
goto :EOF
:dumpdrive
FOR /R "%1:\" %%B IN (*.txt) DO #echo.%%~fB
goto :EOF