Mass creation of folders - batch-file

I'm very fond of listening to my music and I've recently found a need to organize my folders. I have my Music folder and inside it, I have folders in the format Genre\Artist\Album. I want to create a Favorites folder for every sub-folder in the Music folder, but I already have some Favorites folders created so:
I'm trying to create a batch script that adds a Favorites folder to every sub-folder with the exception of Favorites and Discography Info folders.
I know that something along the lines of this:
for /r "%windir%\Users\%username%\My Music" %%s in (.) do md "Favorites" "%%s"
...must be used but I don't know the necessary commands to create folders in multiple folders.
How do I create exceptions in the mass folder-creating process?
How do I extend the range of the command above?

#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%windir%\Users\%username%\My Music"
for /r "%root%" %%s in (.) do if not exist "%%~fs\Favorites" (
if /i not "%%~nxs"=="Discography" if /i not "%%~nxs"=="Favorites" (
if /i not "%%~fs"=="%root%" echo md "%%~fs\Favorites"
)
)
endlocal
Adapt as needed. When output to console is correct, remove the echo command before md to create the directories

My batch scripting is a bit rusty but you can filter out strings using the findstr command. Depending on your needs, this should be good enough. It will add the folder to and path that doesn't contain the words Favorites or Discography:
set targetdir="%windir%\Users\%username%\My Music"
dir %targetdir% /ad /b /s > %temp%\#contents.txt
for /f "delims=" %%d in ('findstr /i /v "\<Favorites\> \<Discography\>" "%temp%\#contents.txt"') do md %%d\Favorites

#ECHO OFF
SETLOCAL
SET "targetdir=c:\destdir"
for /f "delims=" %%s in ('dir /b /s /ad "%targetdir%"') do SET tdir=%%s\&CALL :test&IF DEFINED tdir ECHO md "%%s\Favorites"
GOTO :EOF
:test
SET $test=%tdir:\Discography\=%
SET $test=%$test:\Favorites\=%
IF NOT "%tdir%"=="%$test%" SET "tdir="
GOTO :eof
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories.
Note that when an attempt is made to create a directory that already exists, an error message will be displayed. This is harmless but ugly. You can cause the error message to be suppressed by appending 2>nul to each MD ... line.

Related

Batch file to create folder and move file in the folder

I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.

Moving files from various folder in to single folder based on the list

I have a relatively simple task to do. I have to move 100 files which are located in various different folders, into one single folder. So far I have the script that will do this if i am moving the files from one folder to another folder based on the list of files.
#echo off
set Source=C:\IMAGES_SOURCE
set Target=C:\IMAGE_DESTINATION
set FileList=C:\ImageList.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do copy "%Source%\%%a"
"%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
So for this instance, I created the list of the items and scrip will look for the file name in source location and once find them it will move them to a destination location.
I need to adjust this so it reads the names of the files and try to find them in the list of the folders that they might be.
What i have tried is to change the set Source to be also separate list that will contain posible folder names:
set Source=C:\DirList.txt
But when i do this it does not copy the images.
Any idea how to do this?
First thing that comes to mind is dir /S meaning it will search using the dir command. This is untested, but you'll get the idea:
#echo off
cd /d C:\Images_source
setlocal enabledelayedexpansion
for /F "delims=" %%i in (C:\Imagelist.txt) do (
set var="%%i"
for /F "delims=" %%a in ('dir /B /S !var!') do echo copy "%%~fa" C:\Image_Destination
)
For more on the commands I used, try help by running from from commandline for /? and dir /?
Once confident that it will copy only what you want to copy/move, then remove the echo before copy.

Batch file to delete images

I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.

How to specify multiple wildcards in a folder directory in CMD

I found this post in regards to wildcards in directories. However, my problem is that I have multiple varying directory names between my static directories. For example:
O:\123456 Client Name\Spring\Shoot 1 12345\01 MHP 01\PlCache\GreenScreen\
O:\121212 Someone Else\Spring\Shoot 1 21212\01 MHP 02\PlCache\GreenScreen\
The above link only allows for one wildcard directory instead of muliples.
Within these GreenScreen folders, I have .png files I wish to delete. How would I write a .bat file that deletes *.png within O:\ *\GreenScreen\ ?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:"
FOR /f "tokens=1*delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.png" '
) DO (
SET "targetpath=%%~pa"
IF "!targetpath:~-13!"=="\GreenScreen\" ECHO DEL "%%a"
)
GOTO :EOF
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
I've changed to starting directory to U: to suit my system.
Here's a simpler option - it also echos the del commands to the screen until you remove the echo keyword.
#echo off
for /d /r "o:\" %%a in (GreenScreen*) do if /i "%%~nxa"=="GreenScreen" echo del "%%a\*.png"

create folders based on string in file name

I need a batch file to create a process for a list of files in a directory.
The filename structure is, for example: 00000_AAA_132144_2012021.txt
I need the batch to:
1 - Create a folder name based on the numbers after the second underscore, as this is the only constant in the naming.
2 - Move the file into the new folder.
In the example of the above the batch would create a folder called 132144 and then move the file 00000_AAA_132144_2012021.txt into the folder
For a similar requirement I used the script Endoro created for me (below). Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
If you know that the filenames will be the same length, you can do the following to get the numbers after the second underscore -
set filename=00000_AAA_132144_2012021.txt
set dirname=%filename:~10,6%
If the spacing may vary - you can do the following -
for /f "delims=_ tokens=3" %%a in ('echo %filename%') do set dirname=%%a
And yes, the script written for you seems to do essentially the same thing as what you're asking - I've edited it to do what you've asked -
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b /a-d *.txt') do (
set "filename=%%~i"
for /f "delims=_ tokens=3" %%a in ('echo !filename!') do set folder=%%a
mkdir "!folder!" 2>nul
move "!filename!" "!folder!"
)
This will move all *.txt documents to a folder created based on the third section of the text files name. Note that this will cause problems if you have .txt documents in the directory that do not follow the same naming standard.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*_*_*_*.txt" ') DO (
FOR /f "tokens=3delims=_" %%m IN ("%%a") DO (
ECHO MD "%destdir%\%%m"
ECHO MOVE "%sourcedir%\%%a" "%destdir%\%%m\"
)
)
GOTO :EOF
Endoro's routine selects .pdf files, you've specified .txt
Find filenames matching the mask, find the third _-separated token in the name, make that directory and then move the file.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories and change ECHO MOVE to MOVE to actually move the files.
Append 2>nul to suppress error messages (eg. when the directory already exists)
Append >nul to suppress report messages (eg. 1 file moved)

Resources