Rename multiple folders with pattern in name using Batch - windows

I have 100 folders under a directory with naming in a pattern.
Ex: DeDeP001M1TSub, DeDeP002M1TSub,...,DeDeP100M1TSub.
I am looking for a command line option to rename all the folders. I just want to change M1 to M2 as in DeDeP001M1TSub becomes DeDeP001M2TSub. I know of 3rd party applications that can do this, but am looking for a command line option to use in a bat file along with bunch of other stuff. Please help(Fairly new to the area)!!
So far have tried ren,mv but to no effect(not very familiar with dos scripting).

This should do it:
#echo off
setlocal enabledelayedexpansion
for /d %%a in (*) do (
set "p=%%a"
set "fp=!p:~0,8!" & set "tp=!p:~10!"
echo ren %%a !fp!M2!tp!
)
Remove the echo once you verify the output is what you want to do the actual rename.

Related

How can I remove a specific part of many folders names using .bat files?

My university is using a proprietary system that outputs data in a very specific way in which each "module" is output into folders following the structure (4 digit numeral) - (name of module)
ex: 5574-CHEM104
I need to remove the name and hyphen so that only the numeral remains:
5574-CHEM104 > 5574
The problem is that there's thousands of these folders and there's no way I could do it by hand. I'm having difficulty trying to automate the process, so if anyone could at least point out a command I could look into it would help immensely
I've tried the REN command, putting "REN 5574-CHEM104 5574", but it only works for one folder. There's thousands of folders, each with different numerals, under "CHEM104", for example, and I need for the program to rename the folder no matter the original name into the first 4 original numerals, which I can't figure out. Thanks!
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "delims=" %%e IN (
'dir /b /ad "%sourcedir%\*" '
) DO FOR /f "delims=-" %%o IN ("%%e") DO ECHO REN "%sourcedir%\%%e" "%%o"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the directories.
Using a list of the directory-names, names only (/b) and directories only (/ad), tokenise the name using - as a delimiter and execute the rename using the token assigned to %%o (default is first token)

Using dos, how can I rename existing folders switching the date parts around?

I have a set of folders already named mm-dd-yyyy (for example 01-01-2014) but they will be more useful named yyyy-mm-dd (for example 2014-01-01). Using dos, how can I rename the folders switching the date parts around?
Assuming that you're talking about cmd.exe rather than the ancient MS-DOS command.com, you can do this reasonably easily(1).
The idea is to iterate over all directories in the current directory, using for /d. Then, for each one that matches a specific pattern (99-99-9999), you can use string manipulation to construct a rename command.
The checking for a correct format can be done with the regular expression capability of findstr.
#echo off
#setlocal enableextensions enabledelayedexpansion
rem Process every file. For each file with a valid format,
rem construct a rename command.
for /d %%a in (*) do (
set full=%%a
call :isvalidfmt !full!
if !res!==true (
set d2=!full:~0,2!
set m2=!full:~3,2!
set y4=!full:~6!
echo ren !full! !y4!-!m2!-!d2!
)
)
endlocal
goto :eof
rem Use findstr regex option to see if file name is correct format.
:isvalidfmt
set res=true
echo %1|findstr /r "^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$" >nul:
if errorlevel 1 set res=false
goto :eof
Note that the script above (let's call it rengen.cmd) simply outputs the rename commands, it does not actually rename the files. That's good practice so you can see what it's going to do before you destroy your directories :-)
To actually rename the files (once you've confirmed it's safe), simply capture the output to another file then run that:
rengen >nextren.cmd
nextren
(1) If you are talking about command.com, I can't help you out there. The command interpreter has come a long way since those bad old days and the one that came with MS-DOS is seriously under-powered.
use dir and export to a file. Take it to Excel and create two columns, one with current name and one with new name and then bring it back to notepad and add RENAME command to each row as a prefix.
RENAME cuurentfilename newfilename is the command syntax.

Want to rename a file through windows batch file scripting

Here is my file MY_APP_CON_123_yyyymmdd.hhmmss.txt where "yyyymmdd.hhmmss" is variable data. I want to rename it to MY_APP_CON_0123_yyyymmdd.hhmmss.txt (Zero infront of 123). When I use ren command as shown below, the first y disappears from the file name.
REN MY_APP_CON_123_* MY_APP_CON_0123_*
Any solutions ?
Not answer, but explaining why your rename isn't working:
cmd.exe/command.com's wildcard renaming is position based, e.g.
filename: abc123.txt
command: ren abc*.txt abd1*.txt
will do:
abc123.txt - original name
||||**.txt
abd1**.txt - rename pattern
----------
abcd23.txt - new filename
https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards
Wildcard renaming where you want to change parts of the filename and the new part has a different length from the original part gets very hairy.
Here is a pure batch solution.
#echo off
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&call ren "%%name%%" "%%name:*MY_APP_CON_=MY_APP_CON_0%%"
Or, with delayed expansion:
#echo off
setlocal enableDelayedExpansion
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&ren "!name!" "!name:*MY_APP_CON_=MY_APP_CON_0!"
But I would use my JREN.BAT regular expression renaming utility - a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
call jren "^MY_APP_CON_123_" "MY_APP_CON_0123_" /i /fm *.txt
for %%a in (MY_APP_CON_123_**) do set "name=%%a" & ren %name% %name:123=0123%
This worked for me

Bulk renaming files in relation to the folder names

I am very new to coding and bulk processes but i am looking for a command line SPECIFICALLY for windows command prompt and i am wondering if such a thing exists. So I have a folder containing 111 subfolders, with each subfolder containing between 20 and 40 png image files. Each subfolder is named 001-111 accordingly and the png files are ordered how i want them, however i am looking for a command line that would be able to quickly and efficiently name all the pngs in the folders to the name of the folder followed by the png number in brackets
e.g. for folder 037, i would want the png's to be renamed to: 037(1), 037(2), 037(3) etc...
I am hoping for the best although i am unsure such a code may not be possible or be simply done.
Also if you come up with a code that achieves this process, it would be great if you could reply with the simple command line that i could use rather than a full explanation because i am new to coding and far from fluent with the language or terms or how things work. I know this same process can be achieved by going select all>rename (ctrl a>f2) and renaming to the folder name however i need to use this process frequently and dont want to have to open each folder, i would rather have a command line for cmd that would do it swiftly
Thank you and a simple answer would be greatly appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "parentdir=u:\parent"
FOR /l %%a IN (1001,1,1111) DO (
SET dir=%%a&SET "dir=!dir:~1!"
FOR /f "delims=" %%i IN ('dir /a-d /b "%parentdir%\!dir!\*.png" 2^>nul') DO (
ECHO REN "%parentdir%\!dir!\%%~nxi" "!dir!(%%~ni)%%~xi"
)
)
GOTO :EOF
Test results:
Starting directory :
u:\parent\001\1.png
u:\parent\037\1.png
u:\parent\037\2.png
u:\parent\111\999 with spaces in name.png
Script response
REN "u:\parent\001\1.png" "001(1).png"
REN "u:\parent\037\1.png" "037(1).png"
REN "u:\parent\037\2.png" "037(2).png"
REN "u:\parent\111\999 with spaces in name.png" "111(999 with spaces in name).png"
Obviously, you'd need to replace the value assigned to parentdir with your actual target directory name.
The script will report the renames it proposes to do. To actually invoke the rename remove the ECHO keyword.
I would create a batch file like so:
renamepng.bat:
cd %%1
if ERRORLEVEL 1 goto end
for %f in *.png do mv "%f" "%%1(%f).png"
cd ..
:end
This will attempt to cd to the directory name provided on the command line, abort if that fails, then rename all the .png files and return to the previous directory
then call it like so:
for %d in ??? do call renamepng.bat %d
which will loop through all 3-character file and directory names in the current directory, can call the batch file on each one. Using call instead of just the batch file name causes execution to return to the loop when the batch finishes.

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

Resources