Updating Sub folder and File renaming batch script - batch-file

Need a bit of help with this script
for /d %%D in ("*") do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*).txt" "(*) %%~nF.*"
)
)
This is the original script and this is what it does
Before
filename.jpg
(1).txt
Result
filename.jpg
(1) filename.txt
it copies the filename from the jpg and adds it to the filename of the txt file
what I have been trying to do is two things
I want to add a controlled Sub folder reader to it, and I would like to the filename to be copied between certain points of the txt files
Before
filename.jpg
(1)(name).txt
Result
filename.jpg
(1) filename (name).txt
I have tried like 10 different ways to make this work and for some reason I can't
tried this
FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for /d %%D in ("*") do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
)
)
and I removed this as well for /d %%D in ("*") do (
and tried this
FOR /f "delims=" %%q IN ('dir /b /s /a-d "Ready\(*)(Name).txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for %%F in ("*.jpg") do (
ren "%%~dpF%~1" "(*) %%~nF (*).*"
)
and tried this
for /d %%D in ('dir /b /s /a-d "*"') do (
for %%F in ("%%D\*.jpg") do (
ren "%%~dpF(*)(Name).txt" "(*) %%~nF (*).*"
)
)
Any help would be great
Thank you

#ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name which include 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"
PUSHD "%sourcedir%"
for /d %%B in ("*") do (
for %%E in ("%%B\*.jpg") do (
FOR /f "tokens=1,2delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*)(*).txt" 2^>nul') DO (
rem files matching "(*)(*).txt only
REN "%%~dpnxB\(%%q)(%%r).txt" "(%%q) %%~nE (%%r).txt"
)
FOR /f "tokens=1*delims=()" %%q IN ('dir /b /a-d "%%~dpnxB\(*).txt" 2^>nul') DO IF /i "%%r" equ ".txt" (
rem files matching "(*).txt only
REN "%%~dpnxB\(%%q).txt" "(%%q) %%~nE.txt"
)
)
)
popd
GOTO :EOF
Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.
The outer loop on %%B gets the directory names. No surprise there.
The next loop on %%E gets the .jpg names. No surprise there.
The first loop on %%q looks at the .txt files that fit the pattern (*)(*).txt and re-assembles the parts as required for the rename.
The second loop on %%q looks at the .txt files that fit the pattern (*).txt which may include the just-renamed files matching (*)(*).txt now (*) jpgfilename (*).txt, so this time, %%r must be .txt to exclude these newly-renamed files.
I'll repeat
Caution : This batch is armed. It will rename files. Always verify against a test directory before applying to real data.

Related

batch with a lot of for loops

So i made this batch to psarc extract all *.pak files, which will always give *.mbin files that often have the same name. it's then supposed to look for all extracted subfolders and then look for the last created *.mbin file from all of the subfolders and rename it to its current name plus a number. but i get very weird results.
setlocal ENABLEDELAYEDEXPANSION
set number=0
cd /d "C:\Users\storm\Desktop\No Man's Sky\GAMEDATA\PCBANKS\MODS"
for /r %%f in (*.pak=) do (
psarcMBIN.exe extract %%~nxf
set /A number=!number!+1
FOR /F "tokens=" %%a in ('dir /a:d /s') do (
cd /d "%%a"
FOR /F "tokens=" %%G in ('dir /b /a:-d /o:d "*.mbin"') do set newest=%%G
rename "!newest!" "!newest!!number!.mbin"
)
)
pause
The result basically is just:
1. psarc extracted
2. psarc tried to extract next file but cannot overwrite
The other parts of the main for loop are not even shown

Find first Movie file in every subfolder and create txt file with same name

I would like the batch scriptto find the first movie file(with first i mean alphabeticly first) in all the subfolders and create a txt-file with the same name. I got as far as getting all the Folders that contain the files. with the first for-loop but the second doesn't seem to work as intended
Movie formats: wmv mkv mp4 avi
#echo off
set CurDir=%CD%
for /f "tokens=*" %%G in ('dir /b /s /a:d %CurDir%') do (
For /f "tokens=*" %%H in ('dir /A:-D "%%G"') do (
echo %%H
)
)
echo %CurDir%
pause
#echo off
setlocal enableextensions disabledelayedexpansion
set "curdir=%CD%"
for /r "%curdir%" /d %%d in (*) do (
pushd "%%~fd" && (
set "first=1"
for /f "delims=" %%a in ('
dir /b /a-d /on *.wmv *.mkv *.mp4 *.avi 2^>nul
') do if defined first (
set "first="
type nul > "%%~nxa.txt"
)
popd
)
)
For each folder in the structure, change to the folder and for each file in the list if it is the first one in the folder generate the txt file using the name and extension of the found file.

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

Batch file: sorting folders and files alphabetically

The batch file below recursively echos files and folders while adding some simple formatting, such as indenting to show the recursion depth, adding "/ " before folder names, "*" before certain files, and skipping folders named "Archive". It works great except that files and folders are sorted randomly, rather than alphabetically. How could this be changed to sort both files and folders alphabetically?
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for %%F in (*.txt *.pdf *.doc* *.xls*) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /d %%F in (*) do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
Very little change required. Convert FOR loops to FOR /F running sorted DIR commands. The /A-D option lists files only, and /AD lists directories only.
This version sorts files by name
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* 2^>nul'
) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /f "eol=: delims=" %%F in ('dir /b /ad /one 2^>nul') do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
To sort by extension first, then by name, simply change /ONE to /OEN.
Try changing your for /d loop from
for /d %%F in (*) do
to
for /f "delims=" %%F in ('dir /b /o:n *.') do
and see whether that makes a difference. Actually, ordering by name is the default behavior for dir, so you could probably get away with
for /f "delims=" %%F in ('dir /b *.') do
If some of your directory names have dots in them, you'll need to change it a little.
for /f "delims=" %%F in ('dir /b') do (
rem Is this a directory?
if exist "%%F\" (
rem do your worst....
)
)

Batch file to do everything in a directory

So I'm looking to make a jar to take everything in a directory of type "jar" then have it list it's directory name, and it's filename:
So something like this:
Listing of files in Directory c:\abc\123
myJar1.jar
myJar2.jar
...
myJar10.jar
How I want my output to appear is something like:
<jar href="123/myJar1.jar"/>
<jar href="123/myJar2.jar"/>
...
<jar href="123/myJar10.jar"/>
I've been having a lot of issues with this script, I can't seem to take variable that I'm setting and get them to append to text how I normally do.
Any help would be appreciated.
There is no need for an intermediate file, nor environment variables.
#echo off
>myJars.txt (for %%D in (.) do for %%F in (*.jar) do echo ^<jar href="%%~nD/%%F"/^>)
If you really must include hidden and system files, then the following longer version works
#echo off
>myJars.txt (for %%D in (.) do for /f "eol=: delims=" %%F in ('dir /b /a-d *.jar') do echo ^<jar href="%%~nD/%%F"/^>)
Both of the above can be reformatted to be a bit more readable:
:: excludes hidden and system files
#echo off
>myJars.txt (
for %%D in (.) do (
for %%F in (*.jar) do (echo ^<jar href="%%~nD/%%F"/^>)
)
)
:: includes hidden and system files
#echo off
>myJars.txt (
for %%D in (.) do (
for /f "eol=: delims=" %%F in (
'dir /b /a-d *.jar'
) do (echo ^<jar href="%%~nD/%%F"/^>)
)
)
Turns out that my batch-fu is way strong:
#echo off
dir /b /A *.jar > myJars.txt
for %%* in (.) do set CurrDirName=%%~n*
for /F "tokens=*" %%A in (myJars.txt) do echo ^<jar href^="%CurrDirName%/%%A" /^> >> myJars2.txt

Resources