Batch file moving to subfolders - file

I have lots of folders including images in it. Example:
C:\U2090_08
C:\U2111_08
C:\U2024_03
C:\U2024_08
C:\U2049_15
C:\U2049_35
There are 3-4 jpg files in every folder. I want to create a sub-folder called "kck" in every folder and move jpg files to this subfolder.
Example:
Before process:
C:\U2049_35\1.jpg
C:\U2049_35\2.jpg
C:\U2049_35\3.jpg
After process:
C:\U2049_35\kck\1.jpg
C:\U2049_35\kck\2.jpg
C:\U2049_35\kck\3.jpg
Here is what I am trying:
#echo off
cd %USERPROFILE%\Desktop
:: Sorting images in '\Desktop\images'
for /f "delims=" %%I in (' dir /b "%USERPROFILE%\Desktop\images\*.jpg" ') do (
if not exist "%USERPROFILE%\Desktop\images\%%~nI\kck" ( md "%USERPROFILE%\Desktop\images\%%~nI\kck" )
move "%USERPROFILE%\Desktop\images\%%~I" "%USERPROFILE%\Desktop\images\%%~nI\kck\"
)
exit
Folders including jpg files in a folder called images on desktop.
Any ideas ?

Advanced Renamer software is able to do this. I have just tried it and it have very good move options. It solved my problem.

Next code snippet could help:
#echo off
pushd "%USERPROFILE%\Desktop\images"
:: Sorting images in '\Desktop\images' (subfolders only)
for /f "delims=" %%I in (' dir /b /A:D ') do (
if exist "%%~nxI\*.jpg" (
md "%%~nxI\kck" 2>NUL
move "%%~nxI\*.jpg" "%%~nxI\kck\"
)
)
popd
exit
all paths used in dir, md and move commands are relative to the current directory which is changed to "%USERPROFILE%\Desktop\images" via the pushd command;
note 2>NUL: redirected STDERR stream in the md "%%~nxI\kck" 2>NUL command instead of testing created folder existence explicitly by if not exist "%%~nxI\kck\NUL" md "%%~nxI\kck".

Related

How to copy files from sub folder only using cmd?

I have a folder structure like:
-MainDir
-f0
-f0
-f0
-a.txt
-b.txt
-c.txt
-f1
-f1
-f1
-aa.txt
-bb.txt
-cc.txt
If you see above my files are in 3rd folder in terms of hierarchy. Now my requirement is to be able to copy only the main folder and it's files to another location. For example: output for above structure should be as following:
-MainDir
-f0
-a.txt
-b.txt
-c.txt
-f1
-aa.txt
-bb.txt
-cc.txt
but when I try using XCOPY, all the folders get copied as it is. So I am trying to figure out a way to achieve my target folder structure as shown above
The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:
CMD Script:
#(SETLOCAL
ECHO OFF
SET "_MainDir=C:\Main\Dir"
SET "_DestDir=%Temp%\Destination\Main\Dir"
)
FOR /D %%A in (
%_MainDir%\*
) DO (
IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA"
FOR /F %%a IN ('
DIR /A-D /S /B "%%~fA\*"
') DO (
ECHO Copying: "%%~fa"
ECHO To: "%_DestDir%\%%~nxA\%%~nxa"
COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa"
)
)
This is a quite easy task using xcopy together with a for /D loop, given that the three sub-directories in each hierarchy branch are equally named (like f0\f0\f0 and f1\f1\f1):
rem // Loop through immediate sub-sirectories of main directory:
for /D %%I in ("D:\MainDir\*") do (
rem /* Build path to files into second sub-directory (though there are no files);
rem the third sub-directory and its contents is regarded due to the `/S` option;
rem the copy destination directory then receives the whole contents of the
rem second source sub-directory, including the third source sub-directory: */
xcopy /S /I "%%~I\%%~nxI\*.*" "D:\CopyDir"
)

Script batch copy from subfolder to main folder filtering extension

I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied

Writing a batch file to move cluttered files into respective folders

I have the following batch file code which when run in a folder, checks the file extension, creates the folder against the file extension and finally moves the file into folder name with extension name i.e. it will create .gif folder to move all gifs into it and .jpg folder to move all jpg's into it and so on.
I have no expertise in writing a batch file but all I need is to change this code in such a way that all file may go into the folders in such a way that all image file should go in "Images" folder, all video files in "Videos", all documents files (pdf,docs,xls etc.) in Docs folder, all Audio files should be in Audio folder and so on......
Can anybody help???
#echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpxa" NEQ "%~dpx0" (
rem check if extension folder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
move "%%a" "%%~dpa%%~xa\"
))
#compo The file types with respect to categories are as follows: -
Docs
.docs,docx,xls,pdf
Video
.avi,.mpeg,.mp4
Audio
.mp3,.wma
Image
.jpg,.bmp,.gif
and so on......
The category name should actually the folder name.
Since your listing is small you could probably just add the information to your script file instead of a separate file:
#For /F "Tokens=1*Delims=:" %%A In ('FindStr/B : %0'
) Do #RoboCopy/MOV . "%%A"%%B>Nul
#Exit/B
:Docs: *.doc *.docx *.xls *.pdf
:Video: *.avi *.mpeg *.mp4
:Audio: *.mp3 *.wma
:Image: *.jpg *.bmp *.gif
Edit the last four lines as necessary, it should be self-explanatory to do so.
Edit
Using a separate file, FileCats.txt:
Docs: *.doc *.docx *.xls *.pdf
Video: *.avi *.mpeg *.mp4
Audio: *.mp3 *.wma
Image: *.jpg *.bmp *.gif
...and the batch file, Categorise.cmd:
#For /F "UseBackQTokens=1*Delims=:" %%A In ("FileCats.txt"
) Do #RoboCopy/MOV . "%%A"%%B>Nul
Make sure there's a space after the :
#echo off
for %%s in (Images Videos Docs Audio) do md .\%%s 2>nul
rem For each file in your folder
for %%a in (".\*") do (
set "moved="
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpxa" NEQ "%~dpx0" (
for %%s in (pdf doc xls) do if /i "%%~xa"==".%%s" set "moved=Y"&move "%%a" ".\Docs\"
for %%s in (mpg avi) do if /i "%%~xa"==".%%s" set "moved=Y"&move "%%a" ".\Videos\"
IF NOT DEFINED moved (
rem check if extension folder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
move "%%a" "%%~dpa%%~xa\"
)
)
)
Note : MD ... 2>nul suppresses the error message when the directory can't be created (because it already exists)
First, create the directories to hold the collections.
For each file, first set moved to nothing (ie. "clear" it so it's undefined)
Then check the extension in a case-insensitive manner (/i) against a list of extensions to be placed in the collection. If you find a match, set moved to some value (like Y) and move to the appropriate collection. After all collection lists have ben examined, if moved is still`nothing (ie not defined) then move the file to the directory as determined by the extension.
(untested)

bulk rename files within a folder to parent folder name

I have a large (aprox. 150,000) tif files, which all have the same filename. They are only unique because of the directory structure they are held in.
I would like to bulk rename the tiff files so they become unique, based on the directory structre that they are held within.
Does anyone have any method of acheiving this?
I am using Windows Server 2012 so a solution using a cmd script, batch file or windows GUI tool would be perfect.
Ideally, this is what I would like to acheieve, but if I have to have more or all of the directory structure in the final filename thsi would still be very, very helpful.
C:\A_001\B_0001\ABC\0001.tif -> ABC.tif
C:\A_001\B_0001\JKL\0001.tif -> JKL.tif
C:\A_001\B_0001\XYZ\0001.tif -> XYZ.tif
C:\A_001\B_0002\123\0001.tif -> 123.tif
C:\A_001\B_0002\456\0001.tif -> 456.tif
C:\A_001\B_0002\789\0001.tif -> 789.tif
This should work:
pushd C:\A_001\B_0002
for /d %%a in (*) do (
if exist "%%~a\0001.tif" ren "%%~a\0001.tif" "%%~a.tif"
)
popd
Which should do what you want. I've tested this and it works fine on my computer.
i made a similar foldertree on my drive F:.
Start a commandprompt from within folder "A_001"
FOR /F "TOKENS=3,4,5* DELIMS=\" %A IN ('DIR "%CD%\*.tif" /s /b /a:-d') DO MOVE "%CD%\%A\%B\%C" "%CD%\%A_%B%~xC"
Will result in:
MOVE "F:\A_001\B_0001\123\0001.tif" "F:\A_001\B_0001_123.tif"
MOVE "F:\A_001\B_0001\ABC\0001.tif" "F:\A_001\B_0001_ABC.tif"
MOVE "F:\A_001\B_0002\123\0001.tif" "F:\A_001\B_0002_123.tif"
MOVE "F:\A_001\B_0002\ABC\0001.tif" "F:\A_001\B_0002_ABC.tif"
So you'll have all your files in the folder "A_001".
Hope this helps?
#echo off
for /f "delims=" %%F in ('dir /b /s /a-d "C:\A_001\0001.tif"') do (
for %%D in ("%%~dpF.") do ren "%%F" "%%~nD%%~xF" || echo unable to rename "%%F"
)

Move Images from Sub folders to Main Folders

I have n number of Sub-folders with images. I want to move all the images to Main Folder for organize them correctly.
For Example -
C:\Users\HP\Downloads\NeoDownloader\Book Cover\fc02.deviantart.net\fs17\i\2007\225\9\0\front_cover_of_myths_book_by_cathydelanssay.jpg
C:\Users\HP\Downloads\NeoDownloader\Book Cover\fc02.deviantart.net\fs30\i\2008\092\8\7\The_Seagull_by_rei_i.jpg
Like the above location, I have more then 2000 image. I want to move all those images in one main Folder.
If the folders names are constant I can Write Batch File. But Those sub-folders are not the same. So I can't specify in the batch file. Its difficult to move without Coding. So help me to organize the Images in My Computer.
Notes -
All images are jpg format only
List item last folder contain only few of
images, other subfolders not have image file.
#echo off
setlocal enableextensions
set "inputFolder=C:\Users\HP\Downloads\NeoDownloader"
set "outputFolder=c:\somewhere"
for /r "%inputFolder%" %%a in (*.jpg) do (
if not exist "%outputFolder%\%%~nxa" (
move "%%~fa" "%outputFolder%"
) else (
for /f "delims=" %%b in ('dir /b "%outputFolder%\%%~na_~[*]%%~xa" 2^>nul ^| find /c /v ""') do (
move "%%~fa" "%outputFolder%\%%~na_~[%%b]%%~xa"
)
)
)
Do a recursive enumeration of the files. For each file found, if it does not exist in target folder, move to target. If there is a file with the same name, it is moved with an incremental file name. Not bulletproof but should do the work.
Make sure you create the output directory before running the batch. I learned this the hard way...

Resources