How to bulk add folder name to file name? - image

I have a folder struktur like this:
/1/1/master.jpg
/1/2/master.jpg
/1/3/master.jpg
/2/1/master.jpg
/2/2/master.jpg
/2/3/master.jpg
...
I need to import all images to a website, but the file name have to differ to each other, so I cannot import two (or) more files with the same name. Just numerating the images to master1.jpg, master2.jpg, ... with e.g. AntRenamer is no proper solution, because the image paths/names are assigned to an item number in a csv file I also need to import.
So: How can I bulk add the folder names to the files like this?
/1/1/1_1_master.jpg
/1/2/1_2_master.jpg
/1/3/1_3_master.jpg
/2/1/2_1_master.jpg
/2/2/2_2_master.jpg
/2/3/2_3_master.jpg
...
Thanks for your help!
Timo

#echo off
setlocal EnableDelayedExpansion
cd C:\Parent\Folder\OfFirstNumberedFolders
for /F "delims=" %%a in ('dir /B /S /A-D master.jpg') do (
set "fullName=%%a"
for /F "tokens=1-3 delims=\" %%b in ("!fullName:%CD%=!") do (
ECHO ren "%%a" "%%b_%%c_%%d"
)
)

Try this in Windows. Remove the echo to make it actually perform the rename.
#echo off
for /f "delims=" %%z in ('dir "master.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
echo ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
)
)
)
pause

Related

Delete all files in a folder but skip files that cointain certain string

I'm trying to clean the temp files, but I want skip some files that cointain a certain name. No matter the extension it is.
Tried this way:
#echo off
setlocal EnableDelayedExpansion
for /f "eol=: delims=" %F in ('dir "%windir%/temp" /b /a-d * ') do find "TESTE" %F > nul || del "%F"
pause
Wanted all files that cointains TESTE in name got skipped from deletation.
But my script not even run.
Can someone explain me what is wrong?
It is not absolutely clear whether you want to exclude files from deletion whose names or whose contents include a certain string; anyway, this is for the former:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*" ^| findstr /V /I /C:"TESTE"
') do (
ECHO del /F "%%F"
)
popd
)
Once you are satisfied with the output, remove the upper-case ECHO command.
The above script would also not delete files whose extensions contain the given string. But if you want to regard only the base name, you might want to use this code instead:
pushd "%WinDir%\TEMP" && (
for /F "delims= eol=|" %%F in ('
dir /B /A:-D "*.*"
') do (
set "NAME=%%~nF"
setlocal EnableDelayedExpansion
if /I "!NAME:TESTE=!"=="!NAME!" (
endlocal
ECHO del /F "%%F"
) else endlocal
)
popd
)
You should be able to use findstr.exe with its /V and /M options to list your unwanted files.
#SetLocal EnableExtensions
#For /F "Delims=" %%G In (
'%__AppDir__%findstr.exe /VPMLI "TESTE" "%SystemRoot%\TEMP\*" 2^>NUL'
)Do #Del /A/F "%%G"
#Pause
Please note that the \Windows\Temp directory is usually protected, so you may need to run this script 'as administrator'.

Change the names of pictures in a folder using a text file

I have several pictures of my students in a folder, and a list with their names in a text file.
I would like to creat a batch file to rename the pictures using the text file (names.txt) so that every picture has the name of the student.
All the pictures are in .png format. I searched this site and tried the following code :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem Load the list of new filenames
set i=0
for /F "delims=" %%a in (names.txt) do (
set /A i+=1
set "newname[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F "delims=" %%a in ('dir /b /o:n *.png') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "!newname[%%i]!"
)
I creat the batch file in the folder and when I execute it, there is nothing happening.
I think it is not picking the right folder to work into, but I'm not sure.
Example of files:
1.png
2.png
3.png
Example of names.txt
1_john_dalton
2_carol_denvers
3_steve_austin
Based on your comments and the construction of the current code, it seems you want something like this:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a%%~xi"
)
This above will simply echo the result and not do the rename, only if you are happy with the results, can you remove echo from the last line, before ren.
Your example seems to want to resuse the old numeric name as well, so if indeed the case, then this should be it:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a[%%~ni]%%~xi"
)

two condtions in one loop (Batch file)

I have built 2 batch files, which each have his own function first one:
Changes the file name to today's date
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for /f %%a IN ('dir "%src_folder%" /b') do REN *.xml %time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
pause
Second one:
Move files from DIR_A to DIR_B
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for /f %%a IN ('dir "%src_folder%*.xml" /b') do move %src_folder%\%%a %tar_folder%
pause
My question is: how can I combine them both in one loop?
Second question is with the naming loop. The loop only names the first XML file, and says that the name already exist. That's true therefor is there anyway to make it pause before it name again, so the files have different names?
Simple: to use several commands, use a code block:
for %%a IN ("%src_folder%") do (
echo RENAME %%a
echo COPY %%a
)
Note: keep in mind, you may need to use delayed expansion (not in this example though)
Applied to your code:
#echo off
setlocal enabledelayedexpansion
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for %%a IN ("%src_folder%*.xml") do (
REN "%%a" !time:~0,2!!time:~3,2!!time:~6,2!_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
move "%src_folder%\%%a" "%tar_folder%"
timeout 1 >nul
)
Note: here you need delayed expansion (at least) with the time variable. Consider to delay date too (would be "best practice")
Alternatively: first rename all files, then copy them in one go:
#echo off
setlocal enabledelayedexpansion
SET src_folder="C:\DIR_A\"
SET tar_folder="C:\DIR_A\DIR_B"
for %%a IN ("%src_folder%*.xml") do (
REN "%%a" !time:~0,2!!time:~3,2!!time:~6,2!_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml
timeout 1 >nul
)
move "%src_folder%\*.xml" "%tar_folder%\"
Simplest is to do it in one go, without rename, just move them with a new name:
#echo off
for %%a IN (*.xml) do (
move "%source_folder%\%%a" "%tar_folder%\%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
pause
Other methods:
#echo off
setlocal enabledelayedexpansion
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
set "myren=%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
ren "%%a" "!myren!"
move "%src_folder%\!myren!" "%tar_folder%"
)
pause
Or without delayedexpansion:
#echo off
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
move "%%a" "%tar_folder%"
ren "%tar_folder%\%%a" "%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
pause
or even:
#echo off
for /f %%a IN ('dir /b /a-d "%src_folder%\*.xml"') do (
ren "%source_folder%\%%a" "%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.xml"
)
move /Y "%src_folder%\*.xml" "%tar_folder%"
pause
Simplest, do it in one go:

Batch Script File Transfer with Variables

Thank you for taking your time to help me with the problem I'm trying to solve. I have written some of the batch file but I don't have much experience so this a bit difficult for me. I'm trying to transfer pdf files from a specific location to individual folders in another location. Each file will contain this format
"GOOGLE EARTH_2018-08-07_5485A635.pdf" and based on "GOOGLE EARTH_" it will transfer it to it's rightful folder named as such "GOOGLE EARTH_Google Corporation". So based on the initial part of the file, transfer it to folder that begins with specified file name. This is what I have so far but doesn't work.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.pdf" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") ) DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
MOVE "%%a" "%sourcedir%\%%d\"
)
)
GOTO :EOF
In my answer I have aligned your code for extreme readability to show you how the closing parentheses lines up with each FOR command. I changed the MOVE command to use the source directory variable because the first FOR variable only holds the file name. Also changed the first IN clause to iterate PDF files.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Files"
SET "destdir=C:\Users\Alpha\Documents\NOTEPAD Coding\File Transfer Coding\Transfer"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*.pdf" ') DO (
FOR /f "tokens=1 delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ('dir /b /ad "%destdir%\*%%b*" ') DO (
MOVE "%sourcedir%\%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF

Extract folder name from path in batch file

I want to traverse all files within a specific directory and all its subdirectories and then print out the folder name of each file.
I don't know how to get the folder name of each file.
FOR /F "delims=" %%x IN ('dir /B /A /S *') DO (
:: Suppose %%x is 'C:\myfolder\a.txt', the desired output is 'myfolder'
:: %%~nx is not correct
echo ???
)
If you want just the path (without drive, without filename), %%~px is what you need
If you want just the last folder, not the complete path. This is indeed not that trivial:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('dir /b /a /s *') do (
set "line=%%~dpx"
for /f "delims=" %%y in ("!line:\=.!") do set folder=%%~xy
echo %%~nxx is in: !folder:~1!
)
I think this is what you're looking for:
#echo off
FOR /F "delims=" %%F IN ('dir /B /A /S *') DO (
for %%D in ("%%~dpF\.") do echo %%~nxD
)
pause

Resources