Rename files depending on parent and grand-parent folder names - windows

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

Related

How to rename a file to the parent's parent folder name in batch

so I have a bunch of folders with similar directories to the below:
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png
I want to rename all the .png files the number just before the first randomStringofCharacters. So basically
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
Is there any batch script that can do this? I have tried:
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("*.png") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&Exit
Yet this only renames the file with the parent directory.
And if possible is there a way to move all such renamed .png files in to a newly made folder in .\page\ (the same place where the .bat is) with the folder name of page?
Thanks in advance!
Could be something like this (you have to drag and drop the main folder to the batch):
#echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
You should not use for /R to handle items in a predefined directory hierarchy depth:
#echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
Note, that this approach does not check whether there is only one .png file at each location.

Batch file to search directory for list of files and copy to single folder

I have a .txt file with a list of files that are on a network drive (M:). I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. I have tried quite a few solutions with no luck.
the text file is a list of files and extensions i.e.
abc.step
afer.iges
ca76dc7d.sldprt
Here's what I tried so far
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
set dest=C:\Users\kduquette.000\Desktop\Files
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
set i=1
for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
for %%N in ("%%F") Do (
set name=%%~NN
set ext=%%~XN
)
copy "%%~F" "%dest%\!name!_!i!!ext!"
set /A i=!i!+1
)
)
ENDLOCAL
We can just do a check if file exists and copy it:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%"
Note, for now, I added echo to the copy string, so you can test the result first. Once happy with the results, remove echo
If however you want to search for the files, recursively throughout M:\ drive then:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
pushd M:\
for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
popd
)

How to search for files with given extensions in multiple directories?

set path="D:\Work,D:\workin,D:\files"
set files ="txt,html,xml"
How to search for files with an extension defined in files in the directories defined in path and write their names into a file?
Here is what I have tried already:
set "list=list.txt"
pause
pushd "%~dp0"
>"%list%" (
for /f "delims=" %%i in ('2^>nul dir /ad /b') do (
pushd "%%i"
for /f "delims=" %%j in ('2^>nul dir /a-d /b *.txt *.html') do (
echo %%j& >nul 2>&1 copy/y "%%j" ..
)
popd
)
)
popd
You should be able to simplify this process by using a standard FOR command.
#Echo off
>"FileList.log" (
for %%G in (D:\Work,D:\workin,D:\files) do (
pushd "%%~G"
for %%H in (*.xml *.txt *.html) do (
echo %%~H
copy /y "%%~H" ..
)
popd
)
)

Windows batch file: converting all files in subfolders

I need to write a batch file to convert every file in every subfolder. I came up to the following solution:
set INDIR=<some path>
set OUTDIR=<some path>
mkdir "%OUTDIR%"
for /f "tokens=1*delims=." %%f in ('dir %INDIR% /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one . %%f %%g
)
)
for /f "delims==" %%d in ('dir %INDIR% /ad /b') do (
rem %%d is relative path
mkdir %OUTDIR%\%%d
for /f "tokens=1*delims=." %%f in ('dir %INDIR%\%%d /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one %%d %%f %%g
)
)
The problem is that it iterates through the first level subfolders only. When I add /s key to the dir command it returns full pathes instead of relative.
How can I improve the script to process all levels subfolders?
PS: I need separate values for relative path, file name and extension.
You don't show us convert_one - but here's a few clues...
for /f "delims=" %%f in ('dir %INDIR% /b /s /a-d') do (
rem %%~dpnf is file name and path
rem %%~dpf is file absolute path
rem %%~nf is file name-part
rem %%~xf is extension
call convert_one "%%~dpf" "%%nf" %%~xf
)
)
See for /?|more from the prompt for more...
(within your subroutine, %~n for n=1..9 strips quotes if needed - applying quotes means that "strings containing spaces" are regarded as a single string.
- maybe make your destination directory there...?)
#echo off
setlocal enableextensions disabledelayedexpansion
set "INDIR=%cd%"
set "OUTDIR=..\out"
subst :: /d >nul 2>&1 & subst :: "%INDIR%"
for /r "::\." %%a in (*) do (
if not exist "%OUTDIR%%%~pa" md "%OUTDIR%%%~pa"
call convert_one ".%%~pa" "%%~na" "%%~xa"
)
subst :: /d
This operates creating a subst drive, so the root of the drive point to the in folder. Then iterates over the folder structure recreating the structure in the output folder and calling the subroutine with the indicated parameters

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

Resources