I have a music directory in the format of D:%Artist%%Album%\Song.mp3. from here, i have created a script that prompts for an artist folder and then copies all MP3s into a new folder with the same name after stripping any album folders
D:\%Artist%\%Album%\Song.mp3 into D:\MP3s\%Artist%\Song.mp3
What i'd really like to do is rename all song files in the new location to include the Album name followed by an underscore. if there a song is in the Artist root folder, it should just have an underscore before the song name to indcate it wasn't within an album folder.
D:\%Artist%\%Album%\%Song% into D:\MP3s\%Artist%\%Album%_Song.mp3 OR D:\MP3s\%Artist%\_Song.mp3
the following code is what i'm using to get all songs from all subfolders into a single folder, but i am unsure on how to get the albun name setup for a rename process. this is my current code
SET /P Artist=What is the artist folder to copy?
pushd D:\Music\%Artist%
for /r %%a in (*.mp3) do (
COPY "%%a" "D:\Music\MP3s\%%~nxa"
)
popd
I found that the following code works in my case. if you have a different file structure depth, then you will need to adjust the tokens. i also found that if the script was run multiple times for the same artist, it would copy over the files but not rename them, so i have added in a file check to see if the final filename is already present.
SETLOCAL enabledelayedexpansion
SET /P Artist=What is the artist folder to copy?
MD "D:\Music\MP3s\%Artist%"
PUSHD D:\Music\%Artist%
FOR /r %%a in (*.mp3) DO (
FOR /f "tokens=4,5 delims=\" %%I IN ("%%a") DO (
SET Song=%%J
SET Album=%%I
if "!Song!" == "" (
SET Song=!Album!
SET Album=
)
SET NewFileName="!Album!_!Song!"
)
CD D:\Music\MP3s\%Artist%
IF NOT EXIST !NewFileName! (
COPY "%%a" "D:\Music\MP3s\%Artist%\%%~nxa"
REN "D:\Music\MP3s\%Artist%\%%~nxa" !NewFileName!
)
)
POPD
Related
I'm trying to rename some .png files in a lot of folders with the folder name and an increasing index, that resets in every folder.
My script doesn't work and I think that the issue is with the index resetting, can someone help me?
Note that in the folders there are some files (with a different estension, like .pdf or .log) that I don't want to rename.
Here my best attempt.
#echo off
setlocal
rem Iterate each folder in the current directory.
for /d %%A in (*) do (
rem crafting the string: the variable resets in every subfolder
for /R %%X in (*.png) do (
if not "%%~dpX"=="!LASTPATH!" (set /A num=0) else (set /A num=!num!+1)
set LASTPATH=%%~dpX
ren "%%X" "!%%A%_%num%!.png"
)
)
Edit:
Here the script edited following the tips in the comments.
#echo off
setlocal enabledelayedexpansion
rem Iterate each folder in the current directory.
for /d %%A in (*) do (
set num=0
rem crafting the string: the variable resets in every subfolder
for /R %%X in (*.png) do (
set /a num+=1 && ren "%%X" "%%~nA_!num!%%~xA"
)
)
It renames the file but the index doesn't resets and the files doesn't have the right extension.
Edit:
Using "%%~nA_!num!.png" instead of #stephan suggestion, it rename them with the correct extension.
The files are renamed like this: firstfoldername_1 firstfoldername_2 firstfoldername_n with n is the number of files of the first folder. Then in the second folder the files are named with the first folder name and with a progressive index, like n+1, so it doesn't reset the index nor change the folder name.
I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal
I'm trying to copy thousands of image files and rename them with the name of the folder they are in. The file structure is:-
C:\pictures\kitcam\1\master_01.jpg
C:\pictures\kitcam\1\master_02.jpg
C:\pictures\kitcam\2\master_01.jpg
C:\pictures\kitcam\3\master_01.jpg
C:\pictures\kitcam\3001\master_01.jpg
I would like to create a new directory C:\pictures\kitcam\all and copy and rename the files above to the following naming convention:-
c:\pictures\kitcam\all\[directoryname]_filename] (pad directory name to 4 digits so that the director name 1 becomes 0001 etc)
for example:-
C:\pictures\kitcam\all\0001_master_01.jpg
Jonathan
#ECHO OFF &SETLOCAL
SET "startfolder=C:\pictures\kitcam"
SET "targetfolder=C:\pictures\kitcam\all"
FOR /r "%startfolder%" %%a IN (*.jpg) DO (
SET "fname=%%~nxa"
SET "fpath=%%~fa"
FOR /f "delims=" %%b IN ("%%~dpa.") DO SET "nname=000%%~nxb"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO MOVE "!fpath!" "%targetfolder%\!nname:~-4!_!fname!"
ENDLOCAL
)
Look at the output and remove the word echo before move if it looks good.
I need to run a bat file to rename the photo in each sub-folder as the sub-folder name and copy all the photos to a new folder. Each sub-folder contains only one photo.
I would like to write a batch file to do this task as there are many sub-folders, however, I can only successfully copy the photo in the last sub-folder.
The sub-folders are naming by number sequence starting from "00000001".
I found that the photos are renamed as the same thus only one photo can be copied.
Here's my code:
md "D:\photo"
for /r %%d in (.) do (cd %%d
for /r %%* in (.) do (set CurrDirName=%%~n*
echo %CurrDirName%
ren "*.jpg" "%CurrDirName%.jpg"
copy *.jpg "D:\photo"))
Please advice so that I could modify my code and do what I would like to do successfully, thanks a lot!
EDIT with more details
In cmd line, for example the program are running in the sub-foler "00000127"
set CurrDirName=00000127
but echo the %CurrDirName%, shows 00000128 which is the last sub-folder and every sub-folder return 00000128 but not the CurrDirName
Try this & remove the echo if the output is ok.
#echo off &setlocal
set "destination=d:\photo"
md "%destination%" 2>nul
for /d /r %%d in (*.*) do (
pushd "%%d"
for %%i in (*.*) do echo copy "%%~i" "%destination%\%%~nd.jpg"
popd
)
endlocal
There must be only one photo in each subfolder!
I have 1000 files with the suffix -PRO1 and -PPR2 (1000 each) so I have 1000 folders with the same names but without the suffix...
For example I have a folder called Abstract_Colorful and I have the files Abstract_Colorful-PRO1 and Abstract_Colorful-PPR2 and so on...
I want to make a batch to be able to move all files automatically, I have this code (from another post)
#echo off
setlocal enabledelayedexpansion
pushd "C:\Folders\"
for %%a in (*) do (
set fldr=%%~na
set fldr=!fldr:~0,4!
md "!fldr!"
move "%%a" "!fldr!"
)
popd
pause
exit
but what it does is that if the file has more than 4 characters it creates a folder with the first 4 chars... What I want to do is that the batch recognizes the filename and stops at the - and moves to the folder...
Thanks for your time :)
#echo off
pushd "C:\Folders"
rem Process all files in this folder separating the names at "-"
for /F "tokens=1* delims=-" %%a in ('dir /B *.*') do (
rem At this point %%a have the name before the "-" and %%b the rest after "-"
rem Create the folder, if not exists
if not exist "%%a" md "%%a"
rem Move the file there
move "%%a-%%b" "%%a"
)
popd
A very simple way to do this:
use the search bar in the Windows gui to view all files with the specific suffix (ie. -PRO1)
select all in the GUI
use the Move option in the window menu to create your new folder in the right directory and move all selected files into it