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)
Related
The files names are a part of the name of the directories, which can be at the end or at the beginning of the directory. For example:
pic.jpg into directory \newyork-pic
flower.gif into directory \italy-flower
computer.jpg into directory \computer-informatic
etc
Do you know a way to do that with a batch file?
Here's a piece of example code which should get you most of the way there:
#Echo Off
For /F "Tokens=1-2 Delims=-" %%A In (
'Dir/B/AD-S "*-*"^|FindStr/BRIE "[^-]*[-][^-]*"') Do For %%C In (
"%%A" "%%B") Do If Exist "%%~C.*" Move /-Y "%%~C.*" "%%A-%%B"
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".
I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)
I have a bunch of folders (and subfolders) in the structure of...
Test/Student001/ABC,
Test/Student001/DEF,
Test/Student002/ABC,
Test/Student002/DEF, etc...
What I then need to do is relocate those folders (and their subfolders and the files within) to another location so that it is like...
Test/Class01/ArronAmos(Student001)/ABC
Test/Class01/ArronAmos(Student001)/DEF
Test/Class02/BrettBesty(Student002)/ABC
Test/Class02/BrettBesty(Student002)/DEF
I have text files with all the folders (and subfolders) original and new names (saved like so)..
studentdata.txt
A (studentcode), B (studentnewname), C (Class)
Student001, ArronAmos (Student001), Class01
Student002, BrettBesty (Student002), Class02
Is there a way to get a batch to basically go like this (using A,B and C from text file above - preferably the one txt file if possible)...
md 'C' ::which will skip past if folder exists
rename folder 'A' to 'B' ::only rename of root folder and leave subfolders intact
move folder 'B' to 'C' ::move new named folder (B) and all subfolders and contents to new root folder (C)
The creation of the new directories and subfolders (for new and future students) goes like this and works great (except if there was a way to call a 2nd text file for the subfolder creation rather than coded in that would be awesome - but no biggy I guess)...
The Creation Batch
cd /d %~dp0 pause
FOR /F "delims=~" %%f in (dirlist.txt) DO md "%%f"
:: This code creates directories referenced from a .txt file: - :: FOR /F "delims=~" %%f in (dirlist.txt) DO MD "%%f"
pause
FOR /D %%x in (*) do mkdir "%%x\Individual Behaviour Plans" "%%x\Individual Learning Plans" "%%x\Student Reports" "%%x\Student Support Group Meetings"
:: This code creates a new dir within every folder in batch location: - :: FOR /D %%x in (*) do mkdir "%%x\value"
pause
and this is the Rename Batch that I received off one of the other techs and don't quite understand it or know to modify it to make it work..
*rename_users.bat** :: Script to Rename folders - prefixing from a text file
setlocal ENABLEDELAYEDEXPANSION
Set Rootfolder=Test Set Names=names.txt
:: Goto Root Folder cd /d %~dp0
:: Start Line Counter Set LineCount=0
:: For Every folder in the directory
For /d /r %%g in (*) DO (
:: Increment the line counter by 1 (see the use of "!" >instead of "%" due to delayed expansion)
Set /a LineCount=!Linecount!+1
:: Call the Rename Folder sub - passing through the >variables of folder name and line counter
Call:RenameFolder %%g !LineCount!)
:RenameFolder :: For all of the tokens in the findstr on the names file for /f "Tokens=1* delims=:" %%a in ('findstr /n "^" "%Names%"') DO ( :: If the line counter matches the line number If %%a==%~2 ( :: Rename the Folder Move "%~1" "%~1 %%b") ) ::Return to the Primary
Goto:EOF
Set Rootfolder= Set Names= Set linecount= Set Drive=
Endlocal
The trick is we can't just use the create directories (and subdirectories) batch file, as there are some folders existing in the original format that have data in them that we need sitting in the new structures subfolders... and moving them manually would be an option... if there were not 900+ student folders to do this too...
I hope makes some form of sense... Thanks guys!
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Enter into working directory
cd C:/Test
rem Process the file with original and new names
for /F "skip=1 tokens=1-3 delims=," %%a in (studentData.txt) do (
rem Get studentNewName end eliminate spaces
set "studentNewName=%%b"
set "studentNewName=!studentNewName: =!"
rem If the new folder not exist, create it
if not exist "%%c/!studentNewName!" md "%%c/!studentNewName!"
rem Move files from old folder
move "%%a/*.*" "%%c/!studentNewName!"
rem And delete old empty folder
rd "%%a"
)
Antonio
I am trying to copy a set of files and folders recursively from a directory. The set of files are listed in a text file. Here is my script:
set src_folder=C:\Users\mmhuqx\HW
set dst_folder=C:\Unix2Windows
set filelist=C:\Unix2Windows\filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
for /f "delims=" %%i in (%filelist%) do (
xcopy /s /y "%src_folder%\%%i" "%dst_folder%"
)
But When I run the script it copies the entire Directory contents including the files and folders not listed in the text file.
How can I make it work using XCOPY, or is it not possible?
Your filelist-tm.txt seems to be wrong. It contains for example an entry TOP_LEVEL\MODEM_DEBUG. So that directory is copied completely with all its files.
It seems that you only want TOP_LEVEL\MODEM_DEBUG\AENEAS_FW.fls to be copied. So your filelist-tm.txt should only contain this entry.