Rename files1 to fileA in all subfolders - batch-file

Whenever I need something, I come here and at this moment I need something I found here but wanted more in this code:
#echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do (
set "name=%%F"
ren "!name!" "!name:file1=fileA!"
ren "!name!" "!name:file2=fileB!"
)
My question is: how to rename the files1 and file 2 in this folder and subfolders and write at the end a message "Rename: 2 files"
Thank you for your help.

to make it recursive (processing subfolders), add the /R switch.
Use a variable to build the new filename and execute the ren command only once per file.
#echo off
setlocal enableDelayedExpansion
set count=0
for /R %%F in (*.txt) do (
set "newname=%%~nxF"
set "newname=!newname:file1=fileA!"
set "newname=!newname:file2=fileB!"
ren "%%F" "!newname!"
set /a count+=1
)
echo %count% files renamed

Related

Batch file to rename file and add sequence number to the file name

I picked up the code below from another post. I believe it should pick up the current directory folder and include it in the renaming part of the process, however that doesn't seem to work for me.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD
I have a .txt file that will be received into a folder each day named and time stamped. Example as below:
FileNameA_20170418153000.txt
Essentially I'd like to amend the code above to rename the file: filenam0001.txt and continue to update the sequence number (which works perfectly well).
i.e.
filenam0001.txt
filenam0002.txt
filenam0003.txt
Any help would be greatly appreciated.
you need to add the leading zeros manually (add some zeros, then cut the last x characters):
#echo off
setlocal EnableDelayedExpansion
set inc=0
for /l %%a in (1,1,50) do (
set /a inc+=1
set num=00000000!inc!
set num=!num:~-5!
echo !num!
)
I modified your code in order to insert the leading zeros in a simple way...
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=10000
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1_%%~na!inc:~1!%%~xa"
Ren "%%a" "%~n1_%%~na!inc:~1!%%~xa"
)
POPD

BATCH File : archive Files per Year with special format

I have a Folder full with files with perticular format (321456.done24) and I want to archive this files with a batch datei per year...that means to create inside this folder an another folder mit the year, if doesnt exist, and all the files from the last years - except the current year- to move them there.
an example that I have found and try to modified :`
#echo off
setlocal enabledelayedexpansion
set FolderIncoming=C:\Temp
set FileMask= *.done24
set FolderSorted=C:\Temp
for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
`
but dont know how can I create the folder with year?
With only necessary changes in your code snippet:
#ECHO OFF >NUL
#SETLOCAL enableextensions enabledelayedexpansion
set "FolderIncoming=D:\Temp"
set "FileMask=*.done24"
set "FolderSorted=D:\Temp"
pushd "%FolderIncoming%"
for %%a in ("%FolderIncoming%\%FileMask%") do (
set "FileName=%%~na"
echo Processing '!FileName!' ... %%a ... %%~ta
rem next tokens and delims valid for 'dd.mm.yyyy HH:mm:ss' datetime format
for /F "tokens=3 delims=. " %%G in ("%%~ta") do set "fileyear=%%G"
echo md "%FolderSorted%\!fileyear!" 2>nul
set "TargetFolder=!FileName:~5,8!"
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
popd
:: some code here
#ENDLOCAL
#goto :eof

Batch how to check specific files modified date in folder

for /f %%f in ('dir /b "\\rspdb1\e$\master10\rspadvrec.b*" "\\rspdb1\e$\master10\rspadvrec.d*" "\\rspdb1\e$\master10\rspadvrec.lg*" "\\rspdb1\e$\master10\rspadvpdt.a*"') do (
set fileDateTime=%%~tf
set fileName=%%f
echo %fileDateTime%
echo %fileName%
)
As the code above, I want to list down the modified date of the files in the folder, however, it doesn`t work, any suggestions?
You should add setlocal EnableDelayedExpansion in the beginning of the script, and use !variable! instead of %variable%.
For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.
You also need to add option /s to dir to get the full path of your file to get the data time correctly. And then if you only want the file name, use %%~nxf instead of %%~f.
Here are the codes.
#echo off & setlocal EnableDelayedExpansion
for /f %%f in ('dir /b /s "\\rspdb1\e$\master10\rspadvrec.b*" "\\rspdb1\e$\master10\rspadvrec.d*" "\\rspdb1\e$\master10\rspadvrec.lg*" "\\rspdb1\e$\master10\rspadvpdt.a*"') do (
set fileDateTime=%%~tf
set fileName=%%~nxf
echo !fileDateTime!
echo !fileName!
)

Batch Rename rightmost 3 characters

This is not working.. No errors.
Have directories:
123.abc
123.def
123.ghi
Want to rename to:
abc
def
ghi
What I have done is not working..
I have used: http://www.dostips.com/DtTipsStringOperations.php
Renaming Folder Structure in Batch
SETLOCAL ENABLEDELAYEDEXPANSION
for /d %%D in ("C:\batch\*") do CALL :RENAME %%D %%~nxD
:RENAME
set "folder=%%~nxD"
rem https://stackoverflow.com/questions/11040473/batch-file-string-character-split
set "x=%folder:~-3%"
FOR /D %%R IN (%1%) DO RENAME %%R "%x%"
ENDLOCAL
pause
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem For each directory under selected folder
for /d %%d in ("c:\batch\*") do (
rem Get the extension of the directory if any
set "name=%%~xd"
rem If there is a extension
if defined name (
rem Remove the dot from extension
set "name=!name:~1!"
rem If no file/folder exists with the new name, rename the dir
if not exist "%%~dpd\!name!" echo ren "%%~fd" "!name!"
)
)
endlocal
Final rename command is "echoed" to the console. If output is correct then remove the echo from ren command line.
This will rename the folders to the text after the first period.
Remove the echo to activate the command as currently it will only echo the commands to the console.
#echo off
for /f "tokens=1,* delims=." %%a in ('dir "C:\batch" /ad /b ') do echo REN "C:\batch\%%a.%%b" "%%b"
pause

Change File Name to Folder Name

I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"

Resources