Batch file to rename images for WD Live TV - batch-file

Apologies if this has been answered, the search gives something similar but not exactly what I'm after.
I use XBMC and a Western Digital TV Live as media centers. XBMC uses folder.jpg for movie box art. However, the WD uses both folder.jpg and moviename.jpg.
My folder structure is as follows:
Films\movie1\movie1.mkv(or avi etc...)
Films\movie1\folder.jpg
Films\movie2\movie2.mkv (etc...)
Films\movie2\folder.jpg
What I'm after is a .bat file that will scan the entire films directory, copy the folder.jpg and rename the new jpg using the name of the folder that folder.jpg is in. The original folder.jpg should remain.
Essentially from:
Films\movie1\folder.jpg
To:
Films\movie1\folder.jpg
and
Films\movie1\movie1.jpg
This should happen to each folder in Films.
I'm sure this must be possible but to be honest my knowledge of .bat files is very limited.

You can use a FOR /D loop to iterate over subdirectories of Films:
#ECHO OFF
FOR /D %%I IN ("D:\Path\to\Films\*") DO (
COPY "%%I\folder.jpg" "%%I\%%~nxI.jpg"
)
In the loop, the subdirectory's full path is referenced as %%I and its name alone as %%~nxI (could be just %%~nI if the name never includes a .).
You could run the loop directly from the command prompt, but you'd need to replace the double % characters with single %:
FOR /D %I IN ("D:\Path\to\Films\*") DO COPY "%I\folder.jpg" "%I\%~nxI.jpg"
Please note also that if a moviename.jpg already exists, the COPY command will stop for confirmation of overwriting the file. If you just want to overwrite it anyway without manual confirmation, add the /Y switch:
COPY /Y ...

for /d %%a in (*) do if exist "%%~a\folder.jpg" echo copy /b "%%~a\folder.jpg" "%%~a\%%~na.jpg"
Remove the echo to get it working.

Related

Open multiple directories without knowing the names of said directories and delete folders within them using a Windows batch file

Okay, I do not really know how to describe fully what I want and I feel bad for asking something so advanced. I do not know how to do anything other than move folders and delete files and folders using batch.
I am trying to make a batch file to delete certain Steam files and folders which are used to cache data. I could easily make this for myself and call it a day however I would like to share it and have it work for other people also. The problem is one of the cache folders is named with a unique Steam identifier. I will put what I have made so far below.
REM Makes a temporary folder.
mkdir %LocalAppData%\Temp\steam_cache
REM Deleted all none essential files in Steam's configuration folder.
move "%ProgramFiles(x86)%\Steam\config\config.vdf" %LocalAppData%\Temp\steam_cache
move "%ProgramFiles(x86)%\Steam\config\loginusers.vdf" %LocalAppData%\Temp\steam_cache
rmdir "%ProgramFiles(x86)%\Steam\config" /s /q
mkdir "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\config.vdf "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\loginusers.vdf "%ProgramFiles(x86)%\Steam\config"
REM Delete all none essential files in Steam's userdata folder.
I will put what the file structure goes like below.
%ProgramFiles(x86)%\Steam\userdata\256283931
As you can see the number at the end is completely random and I do not know how to open that directory without knowing the number first. There are sometimes multiple of these folders with random names if you login with multiple account and I would like the batch file to go into them one by one and delete certain folder inside of them.
I will put below the folders that I would like to delete that are inside of the random numbered folder below.
%ProgramFiles(x86)%\Steam\userdata\256283931\ugcmsgcache
Sorry if what I am asking it too much if so just ignore this post, thanks.
What you are looking for is the for /D loop, which enumerates directories:
rem // Enumerate all directories under `userdata`:
for /D %%I in ("%ProgramFiles(x86)%\Steam\userdata\*") do (
rem // Check if there is really a sub-directory called `ugcmsgcache`:
if exist "%%~I\ugcmsgcache\" (
rem // Do something with the full path, like echoing, for instance:
echo "%%~I\ugcmsgcache"
)
)
If you want to ensure that the name of the enumerated directory/-ies name consist(s) of decimal figures only, you could use dir and findstr, together with a for /F loop:
rem // Change into parent directory, because `dir /B` returns plain directory names:
cd /D "%ProgramFiles(x86)%\Steam\userdata"
rem // Enumerate all directories under `userdata`, whose names are pure numbers:
for /F "delims=" %%I in ('
dir /B /A:D "*" ^| findstr /I /X "[0123456789]*"
') do (
rem // Do something with the full path, like echoing, for instance:
echo "%%~fI\ugcmsgcache"
)
The cd command could also be replaced by pushd and popd.
The ~f modifier ensures that the full path is derived, related to the current working directory.
Note that the pipe (|) needs to be escaped here, because it is in the set of the for /F loop.

Replace file.old with file.new in multiple subfolders

I am trying to replace over 400 audio files, in just about as many sub-folders, with a updated format required by the program. It's the same file in each location but its how they require it for their xml file. I found several similar post, replacing files in multiple sub-directories like this one Replacing a file into multiple folders/subdirectories this I assumed would meet my needs.
I want to find all AR33.mp3 files in SFX'x folders, and replace with the new required AR33.wem my issue is as follows:
FOR /R C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX %%I IN (AR33.mp3) DO echo COPY /Y C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem %%~fI
This does the reverse and copies the file I want to overwrite, ar33.mp3, all over the folder structure in every folder where there shouldn't be any. but it does say 1 file copied after each entry.
(AR33) creates extension-less copies. adding (*AR33.mp3) does nothing, same with (*AR33.mp3 *) does nothing, even with "1 item copied" pointed out in the console. At this point I'm trying what I can to get this to work randomly doing stuff. Changing COPY to replace give a invalid switch -/y. Am I using the wrong "script" to do what I want? Because even changing one of the AR33.mp3 over to a .wem didn't see any change after replacing the line .mp3 to .wem. I assume I need to fix what its searching for and what its copying.
Updated bat:
FOR /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I IN (AR33.mp3) DO echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~fI"
Output sample:
C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted>echo COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
COPY /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\AAC0\ShipName__PASC045\isPlayer__False\AR33.mp3"
You want to replace .mp3 with .wem as well. For now you are simply copying the .wem file and naming it with a .mp3 extension. So we could just rename the .mp3, then copy the .wem file to it.:
#echo off
for /R "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\GoldenEye\SFX\" %%I in (*AR33.mp3) do (
echo ren "%%~fI" "%%~nI.wem"
echo copy /Y "C:\Users\trevo\Desktop\ProjectGoldenEYE\811converted\sfxwems\AR33.wem" "%%~dpnI.wem"
)
Please take note before continuing. The above will only echho the result, once you are happy that it does what you want, then only remove `echo from both strings in the code block.
It is also always a good idea to backup files before running a big copy process like this.

How can you use Windows CMD to save a copy of a file name to a blank text document?

I've been experimenting with Xcopy on my flash drive, which as the reader most likely knows, is a way to transfer multiple file copies very efficiently. During my experimentation, i had a thought. The code i ran(which will be disclosed at the end of the comment) was set to copy files from my desktop, music, photos etc. However, on my desktop was a particularly large file for a game i frequent. When the program got to the games files, it took an extremely long time to copy the files, as the game ran a 3D environment, and i needed this code to run quickly and efficiently. So, my thought was:Is there a way to copy the name of a file(in this case, the name of the game, FTB), have the program copy the name of the file only, then create a text file on the flash drive where the code is being run, then overwrite the name of that document with the copied name?
Here is the code being run, and i am aware that it has some obvious problems, but its being worked on:
#echo off
:: variables/min
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /EXCLUDE:MyExcludes.txt
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"
copy "Bookmarks.bak" "all"
removedrive D: -L -H -A -W:1000
if you need any extra information, just ask.
add the large file to the myexcludes.txt file so it wont copy
just create a new file on your backup drive using something like the following (assuming your file is called mylargefilesname.ext)
echo mylargefilesname.ext > "%drive%\location\mylargefilesname.ext"
------ edit ------
To do this for all .exe files you can exclude *.exe and then use a loop.
for /f %%f in ('dir /b "%USERPROFILE%\Pictures\*.exe"') do echo %%f > "%drive%\all\My pics\%%~nxf"
this is looping over the results of dir /b "%USERPROFILE%\Pictures\*.exe"
for each line of the output it will echo the full file path to your new folder using the filename + extension of the file (thats what the nx part does)
I know this is resurrecting an old thread, but I've found this code all over the internet (it's been copied a lot) with no clear explanation of the nonsensical %odrive% initialization and the non-initialization of %drive%.
It doesn't work because they left out some code. Here's how to fix it:
Replace:
SET odrive=%odrive:~0,2%
With:
SET odrive=%CD%
SET drive=%odrive:~0,2%
The first line gets the current directory as %odrive% and the second line strips out the first two characters (e.g., C:) and assigns them to %drive%
Note the script assumes it's being run from the removable drive. So you could simply replace all instances of %drive% with . and it would work just as well.

Command Promp to copy ONLY images from FOLDERS and SUBFOLDERS

I'm wondering if it is possible to set up a batch command to perform this action.
Once .bat file is executed, ALL images from folders and sub-folders would be copied to my location on the desktop.
Example:
Original folder is located:
\intranet\file_location\PP Complete Images (in this folder will be loads of other folders and in those folders there will be .jpg images)
Destination file would be based on the desktop.
So I need to extract .jpg images from all folders and sub-folders.
If image already exists in original folder, skip the image or overwrite as script will be executed every morning.
Or should I look for a software to do this for me?
Existing code:
cd c:
cd\
copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder"
copy "\\intranet\PP Complete Images*.jpg"
exit
How do you want it?
It isn't quite clear to me, how the result exactly should be -- should it be flattened or should it be hierarchical as well?
Look at this for example:
source
folder-1
folder-1-1
image1.jpg
folder-1-2
image2.jpg
cheese.jpg
image3.jpg
some_text.txt
folder-2
folder-2-1
image3.jpg
some_music.mp3
cheese.jpg
target
Should the result be basically a copy of the shown hierarchy (without any other file than the jpgs), or should it be a flattened result like this one:
source
... (see above)
target
image1.jpg
image2.jpg
cheese.jpg
image3.jpg
image3.jpg
How can you do it?
Flattened
You can use DOS' for command to walk directories1 and make a custom function2 to handle the files:
#ECHO OFF
for /r %%f in (*.jpg) do call:copyFile %%f
GOTO END
:copyFile
copy /V /-Y %~1 ..\target
GOTO:EOF
:END
Meaning: for every %%f in the listing of *.jpg from the current working dir, execute function copyFile. The /r switch makes the listing recursing (walk through all subdirectories).
In the function, the argument passed to it (now known as %~1) is passed to the copy function: Copy the file to the target directory which is ..\target in this case. /V lets copy verify the result, /-Y lets it ask for permission to overwrite files. See copy /?!
Very big problem: If you have one or more files in different subfolders of your source directory, which have the same name (like the two cheese.jpgs in my example), you will loose data!
So, I wouldn't recommend this approach, as you risk loosing data (digital cameras are not very creative in naming pictures!).
Hierarchical
Just use robocopy:
robocopy /S <sourcedir> <targetdir> *.jpg
/S creates and copys subfolders as well. You can also use /DCOPY:T to make the directories have the same timestamp than the original ones or /L to preview the actions of robocopy.
Small problem: The /E switch handles subfolders as well, even if they are empty. /S handles subfolders as well, but not, if they are empty. But it handles them, if they are not empty, but have no JPG inside -- so, subfolders without JPGs will result in empty folders in the target folder.
Robocopy has loads of parameters, so check out robocopy /?.
Hope this helps! :-)
1Found here: How to traverse folder tree/subtrees in a windows batch file?
2Found here: http://www.dostips.com/DtTutoFunctions.php
Your existing code:
the cd c: is incorrect. To switch the current drive to c: use
c:
The cd \ is redundant. Your remaining code specifies the directories, so the current directory is irrelevant.
Your first copy command has three problems. Master Image Folder*.jpg means all filenames beginning Master Image Folder and ending .jpg. You probably meant Master Image Folder\*.jpg meaning all files ending .jpg in ...\Master Image Folder\
C:\Users\username\Deskto... is probably an error. It is a literal path, so the actual directory would be C:\Users\username\Deskto... You would probably need C:\Users\%username%\Deskto... to substitute-in the current username.
And then the job would stop on a filename-match, so either you'd be pressing A to overwrite all or you'd be pressing y or n for each name-match.
Your final copy command has no specified destination directory.
You can edit-in your actual code by using the edit button under the original text window, cutting-and-pasting your actual code - censoring if necessary, selecting the resultant code block and pressing the {} button above the edit box which indents each line with the effect of formatting and hilighting the code.
The simplest solution is probably to use
xcopy /d /y /s "\\intranet\PP Complete Images\Master Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
which will copy updated files (/d) with automatic overwrite (/y) and scanning subdirectories (/s) from-name/mask to-directory.
This would create an identical directory-hierarchy to the original subtree under the destop's Master Image Folder directory.
You could extend this to
for %%a in (
"\\intranet\PP Complete Images\Master Image Folder"
"\\intranet\wherever\somewhere"
) do xcopy /d /y /s "%~a\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
to perform the same action on multiple directory-subtrees; but you need to ensure that the destination directory is not within any subtree selected for inclusion in the list within the parentheses.
I'd advise against "flattening" the output because if you do that, the latest whatever.jpg from each of the subtrees will end up in your destination directory, without notification that there are many possibly different whatever.jpg versions.
I do believe the solution to your problem would be Robocopy.
Robocopy is just plain awesome!
Here is the syntax of robocopy-
robocopy [Source] [Destination] [File] [...] [options]
Source
Specifies the source folder. Where you want to take the files from.
Destination
Destination directory/folder.
File
Here we are! This is what will help you. Here you can specify an extension you want to move. So in your case, your code would look somewhat like this.
robocopy *.jpg c:\destinationdir /S /MAX:1048576
*To execute this .bat every morning go to a program called task scheduler, dont worry, its built into windows. http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
*Then Click on Create basic task, and set your task to whenever you like!
Thanks guys for your help!!!
I got it solved and there is a code below if someone would ever need something similar:
pushd Z:\intranet\PP Complete Images\
for /r %%a in (*.jpg) do (
XCOPY /Y "%%a" "C:\Users\username\Desktop\Master Image Folder"
)
popd

Batch Script to Rename JPEGS adding a 1 to the end

I have a large amount of JPEGs inside subfolders that I need to rename to their current name with an extra 1 at the end.
For example:
G:\FILENAME\Subfolder1\subfolder2\JPEGNAME.JPG
Want to rename to
G:\FILENAME\Subfolder1\subfolder2\JPEGNAME1.JPG
I have over 900 files in this format that I need to rename.
Any help?
edit I added /r as I see you have a tree of files to modify. Type this command in the main holding folder of the JPG files.
Here's a cmd prompt command. Delete the echo if you like what you see on the console.
for /r %a in (*.jpg) do echo rename "%a" "%~na1%~xa"
There may not be a need for anything more than a simple REN command. See How does the Windows RENAME command interpret wildcards?.
As long as none of your file names have multiple dots in the name, then the following should work for the current directory. Just make sure there are at least as many ? as the longest name.
ren *.jpg ??????????????????????????????????????1.jpg
Or to process the entire directory tree
for /r %F in (.) do #ren "%F\*.jpg" ??????????????????????????????????????1.jpg
Or you can iterate each file with a FOR or FOR /R loop and rename them one at a time as foxidrive does in his answer.
using renamer:
$ renamer --find '/(.*)\.JPG/' --replace '$11.JPG' *
to operate recursively, change the * at the end of of the command to **.

Resources