I have the folder and file structure as given below. I am in need of a MS DOS batch file to rename the files in multiple folders. Can anyone please help out?
- Main Folder
-->Sub Folder1
--- File1_EN.txt
--- File2_EN.txt
--> Sub Folder2
--- File3_EN.txt
--- File4_EN.txt
I want to rename the suffix "EN" in file names to "ENU".
#echo off
for /D %%d in (*) do (
ren "%%d\File*_EN.txt" "File*_ENU.txt"
)
You can do it by this way:
#Echo OFF
Set "Folder=C:\Users\Administrador\Desktop\Nueva carpeta"
Set "Suffix=_EN"
Set "Replace=_ENU"
Set "RegEx=\".*%Suffix%\"$"
FOR /R "%Folder%" %%# in ("*") DO (
(Echo "%%~n#"| FINDSTR /I "%RegEx%" 1>NUL) && (
Set "NewFileName=%%~nx#"
Call Set "NewFileName=%%NewFileName:%Suffix%=%Replace%%%"
Call Echo [+] Renaming: "%%~nx#" "%%NewFileName%%"
Ren "%%#" "%%NewFileName%%"
)
)
Pause&Exit
The Findstr is to ensure the matched string is a suffix, is better than doing a substring or splitting the filename from "_" character to the right.
Try this:
ren folder1\file*.txt file*_enu.txt
ren folder2\file*.txt file*_enu.txt
If you want all child folders to be changed use:
for /f "delims=*" %a in ('dir File*_EN.txt /b /s') do ren "%a" File*_ENU.txt
Related
I've got a script that I'm using for all kinds of things, including renaming and repacking comics. Variants of this script are used by others as well.
I've been seeing a limitation in repacking comics, however; I've found that some comics don't have the leading zeroes at their pages, which makes the pages appear out of order. So, I've added a part that should add leading zeroes.
The file does the following:
loop over all subfolders of the current folder
rename comic archives to their proper extension (so I can see where it is)
loop over all archives, and extract them to a temporary folder
loop over all files in the temporary folder, and add leading zeroes
repack it as a 7zip
rename the file to a comic book extension
Somehow, it's not renaming the files properly, and the name of the archive appears as a subfolder in the archive. For example:
let's repack 'testfolder', which contains images from 1 to 100. It renames, extracts, packs, and renames again, without problem. However, the new archive contains the folder named 'testfolder' in the archive next to the images, which don't have the leading zeroes. I'm not sure what's going on, and I've been fighting with it for a while now, so I thought to put it online (it's a good script to share, anyway). Does anyone have an idea on what's going wrong here?
#ECHO ON
rem mode con: cols=80 lines=60
for /f "delims=" %%F in ('dir /ad/s/b') do (
cd %%F
IF EXIST *.cbr (
RENAME *.cbr *.rar
)
IF EXIST *.cbz (
RENAME *.cbz *.zip
)
IF EXIST *.cb7 (
RENAME *.cb7 *.7z
)
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"C:\Program Files\7-Zip\7z.exe" e "%%I" -oC:\TMPPACKDIR\* -y | FIND /V "ing "
echo %%~nI
cd C:\TMPPACKDIR\%%~nI\
FOR /f "delims=" %%P IN ('dir *.JPG, *.PNG, *.BMP') DO (
SET %%N = %%P
SET %%N = 00%%N
SET %%N = %%N:~-2%
echo %%P
echo %%N
pause
rename 'C:\TMPPACKDIR\%%~nI\%%P' %%N
)
pause
echo %%F
cd %%F
ECHO Repacking
"C:\Program Files\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9 | FIND /V "ing "
IF %ERRORLEVEL% EQU 0 RD /S /Q C:\TMPPACKDIR
ECHO Renaming new file
RENAME *.7z *.CB7
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
)
REM del /f/q "%~0" | exit
Yes, the problem is in the code portion between cd C:\TMPPACKDIR\%%~nI\ and pause. You are trying to set a for variable reference %%N, which does not work. You need to use a normal environment variable instead, like NAME, for example; you can only do sub-string expansion (like ~-2 in your code) using normal environment variables. In addition, since you are setting and reading the same environment variable within a single block of code, you need to use delayed expansion; otherwise, you would always receive the value present when the entire block is read.
The code portion should look like this:
cd /D "C:\TMPPACKDIR\%%~nI"
for /F "delims=" %%P in ('dir /B *.JPG, *.PNG, *.BMP') do (
set "FILE=C:\TMPPACKDIR\%%~nI\%%P"
set "NAME=00%%~nP"
setlocal EnableDelayedExpansion
set "NAME=!NAME:~-2!"
rename "!FILE!" "!NAME!%%~xP"
endlocal
)
pause
I've removed some unnecessary stuff and made a few changes, (the main error being that which aschipfl has already identified).
FOR /F "DELIMS=" %%F IN ('DIR/AD/S/B') DO (
PUSHD "%%F"
IF EXIST *.cbr REN *.cbr *.rar
IF EXIST *.cbz REN *.cbz *.zip
IF EXIST *.cb7 REN *.cb7 *.7z
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"%ProgramFiles%\7-Zip\7z.exe" e "%%I" -o"C:\TMPPACKDIR\*" -y
PUSHD "C:\TMPPACKDIR\%%~nI"
FOR %%P IN (*.JPG, *.PNG, *.BMP) DO (
SET "_N=100%%~nP"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_N=!_N:~-2!"
REN "%%P" "!_N!%%~xP"
ENDLOCAL
)
POPD
ECHO Repacking
"%ProgramFiles%\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9
IF NOT ERRORLEVEL 1 RD/S/Q "C:\TMPPACKDIR\%%~nI"
ECHO Renaming new file
REN "%%~nI.7z" "%%~nI.CB7"
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
POPD
)
Things to look into:Can 7z.exe not just extract .cbr, .cbz & .cb7 directly without renaming them first. In the same way, when repacking once you've provided the file type, -t7z can the file not be given the name "%%~nI.CB7" directly instead of later renaming it.
Hi all and thanks for the answers,
Firstly, I tried to find the answer to my problem but I did not find anything.
I have a tree of folders and sub-folders and I want to use 7zip to compress the files within those folders separately.
I have got this piece of code from this very website, it does what I want to get but it places the compressed files on the main folder:
set extension=.*
for /R %%a in (*%extension%) do "%sevenzip%" a -mx "%%~na.zip" "%%a"
I wonder if I can get a zip file of every file and have it in the sub-folder containing the source file. Or doing the process above and place every zip file inside the appropriate sub-folder.
I tried with a double 'For /d' but I was unable to get it:
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set
sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set
sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for /D %%O in (*) do (
for /R %%I in ("%%O\*") do (
"%sevenzip%" a -mx "%%~na.zip" "%%a"
:: rd /s /q "%%I" **Because I do not want to delete anything by now.
)
)
Again, thank you.
Alex.
If you have somewhat complex folder structure then you probably better use plain list from dir:
dir /a:-d /s /b /o
Just use its output in for:
for /f %%f in ('dir /a:-d /s /b /o') do (
echo %%f <-- %%f is a full path to a file, do something with it
)
Btw, 7zip has useful option -sdel to remove the source file when archive has been created successfully.
This is the final code:
#echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
#echo searching...
for /R %%I in (*) do (
"%sevenzip%" a -mx -mmt4 "%%I.7z" -r -x!*.bat "%%I"
)
del "Compressing_files_7zip.bat.7z"*
del *.7z.7z
del *.zip.7z
::::::::::::::::::::::::::For setting up shutdown 60' after the end of the process.Remove colons in the line below.
::shutdown.exe /s /t 3600
pause
Thanks all for the support, especially to Frost.
For a simpler version without using 7zip:
for /f %%f in ('dir /a:-d /s /b /o *.mdb') do (
zip -r -p "%%f.zip" "%%f"
)
I have a directory structure that reads like:
nnnnnn~substring
Where n are numbers, and substring are letters.
I am trying to write a batch file that tests if a particular file exists inside the directory, and if it does, it should rename the directory to substring.
The batch file should look like:
for /f "tokens=\*" %%a in ('dir /b') do if exist filename (rename nnnnnn~substring substring)
How do I trim all the numbers and the ~ character of the directory name so I can rename it using only the final part of the name?
The numbers before the ~ separator have different lengths, so does the substring after it.
#echo off
setlocal enableextensions disabledelayedexpansion
rem Change to the target folder
pushd "x:\somewhere" && (
rem For each folder inside it matching the indicated pattern
rem Uses a dir command to search only folders and a
rem findstr filter to ensure only matching folders
for /f "delims=" %%a in ('
dir /ad /b *~* ^| findstr /r /c:"^[0-9][0-9]*~..*$"
') do (
rem Check if the folder contains the file
if exist "%%~fa\flagFile.txt" (
rem Split the folder name using the ~ as delimiter
for /f "tokens=1,* delims=~" %%b in ("%%~na") do (
rem Check that the new folder name does not exist
if not exist "%%~c%%~xa" (
rem Execute the rename operation
echo ren "%%~fa" "%%~c%%~xa"
)
)
)
)
rem Restore previous active directory
popd
)
Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes ren command
This may work - test it in a copy of your folder structure:
It assumes that substring in your question does not contain any ~ characters.
#echo off
:loop
for /d /r "d:\base\folder" %%a in (*~*) do (
if exist "%%a\filename" for /f "tokens=1,* delims=~" %%b in ("%%~nxa") do (
ren "%%a" "%%c"
goto :loop
)
)
pause
This is designed for only the folders within the current directory.
#echo off
for /d %%a in (*~*) do (
if exist "%%a\filename" for /f "tokens=1,* delims=~" %%b in ("%%~nxa") do ren "%%a" "%%c"
)
pause
I need to get the relative filepath of files with bat.
The folder structure
file.bat
- folder1
-- subfolder1
----- abc.image
----- def.image
-- subfolder2
---- more.image
- folder2
-- subfolder1
---- alsoimages.image
-- subfolder2
For abc.image, I'd like to get the name of the folder (here e.g. folder1) and the combination folder+subfolder (here e.g. folder1/subfolder1)
I looked at this post: batch programming - get relative path of file
but I cant make it work for me. The output keeps giving me too many hierachies.
What I want to do ultimately is this:
set "prefix=td"
set "file=list_of_images.xml"
echo ^<?xml version="1.0" encoding="UTF-8" standalone="yes"?^> > %file%
echo ^<files^> >> %file%
for /r %%x in (*.image) do (
echo ^<file folder="(here e.g. folder1)" subfolder="(here e.g. folder1/subfolder1)" id="%prefix%%%~nx" type="image" /^> >> %file%
)
echo ^</files^> >> %file%
Thanks for help and tips!
for /f "tokens=1,* delims=\" %%a in ('
xcopy ".\*.image" "%temp%" /s /e /l
') do if not "%%b"=="" echo(%%b
The easiest way to get a list of files with relative paths is to use an xcopy command to generate the list (/l) of files.
You can also use a subst command to create a "virtual" drive letter with the root of the drive pointing to the required starting folder and then use your same code referencing this drive, that now will not retrieve the upper folder structure
edited to adapt to comments
for /f "tokens=2-4 delims=\" %%a in ('
xcopy ".\*.image" "%temp%" /s /e /l
') do echo folder=%%a subfolder=%%b file=%%c
The output from xcopy command is in the format
.\folder\subfolder\file
so, using backslash as delimiter we skip the first token and retrieve the second (folder), third (subfolder) and fourth (file)
One of next code snippets could help:
for /r %%x in (*.image) do (
for /F "tokens=1* delims=\" %%G in ("%%~px") do (
echo "%%~G" "%%~H" "%prefix%%%~nx"
)
)
Splitting to more than one subfolders:
for /r %%x in (*.image) do (
for /F "tokens=1,2* delims=\" %%G in ("%%~px") do (
echo "%%~G" "%%~H" %%~I "%prefix%%%~nx"
)
)
And so on...
I have files with varying names...
Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
I want to move all tim files into Tim folder and jack files to Jack folder etc.
Can it be done using .bat files ? If yes, please share the code for it.
Thank You.
#echo off
setlocal
set sourcedir=c:\sourcedir
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d "%sourcedir%\*-*.*") do (
md "%sourcedir%\%%a" 2>nul
echo move "%sourcedir\%%a-%%b" "%sourcedir%\%%a\"
)
Note that the 2>nul suppresses error messages created when an attempt is made to recreate an existing directory
The MOVE is merely ECHOed. Remove the ECHO keyword to activae the move. It may also be prudent to append >nul to the MOVE statement to suppress the "1 file(s) moved" message.
As you may know, there are no arrays in Batch. So let's just use one.
#ECHO OFF &SETLOCAL
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d *-*.jpg') do set "$%%a=%%a"
for /f "tokens=1*delims==" %%a in ('set $') do robocopy "%cd%" "%cd%\%%b" "%%b*" /mov /l
Remove /l from robocopy to make it working.
EDIT - Changed code to reduce the number of file moves. Now, all files with the same prefix get moved in one command.
#echo off
setlocal enableextensions
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-*.jpg") do (
rem if the file still exists (maybe it has been moved)
rem then split the file name with the dash as delimiter
if exist "%%~ff" for /F "tokens=1 delims=-" %%s in ("%%~nf") do (
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "%%~s"=="" (
robocopy "%_dir%" "%_dir%\%%~s" "%%~s-*.jpg" /mov /njs /njh
)
)
)
endlocal
EDIT2 - Changed to adapt to comments
#echo off
setlocal enableextensions enabledelayedexpansion
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-??.jpg") do (
rem if the file still exists (maybe it has been moved)
if exist "%%~ff" (
rem get the filename without the 3 last characters
set "_target=%%~nf"
set "_target=!_target:~0,-3!"
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "!_target!"=="" (
robocopy "%_dir%" "%_dir%\!_target!" "!_target!-??.jpg" /mov /njs /njh
)
)
)
endlocal