I have various files with the same structure, they begin with a unique key and a hyphen followed by the name of the file. Examples below:
100023525_Document_1
100023525_Document_2
102008006_Document_1
102008006_Document_2
102008006_Document_3
I want to be able to generate folders for each unique key and have the files moved into them automatically. I have found the following code below written in batch that does so but puts each file into its own separate folder.
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
This puts the following files:
to
I want the code to be able to put the initial files into folders like this, based on the unique key:
How could I modify the initial batch code to do this? Thanks!
Note: I decided to simplify the code here.
There are probably easier and faster ways to do this, but here is one option if your files are all in the same format. You will want to do this from the folder containing your files:
for /f "delims=_" %%b in ('dir /b *.pdf') do (
if not exist %%b\. #mkdir "%%b"
)
for /f "tokens=1,2,3 delims=_" %%b in ('dir /b *.pdf') do (
if not exist "%%b\%%b_%%c_%%d" #echo move "%%b_%%c_%%d" "%%b\%%b_%%c_%%d"
)
For more information on how this works, please read the documentation on for /f. Take note of how the tokens and delimiters work.
This will show you the items it will move and where. Remove #echo in front of move to do the actual move.
Related
Long time reader, first time poster - appreciate any help you can give.
Context: I am looking to tidy up a set of property folders. Currently the parent folders are the projects and the properties are sub-folders. If a property is affected by multiple projects it will be duplicated across the project folders. My goal is to flatten the structure so I simply have one folder per property. (eg., copy from E:\TESTING\Project\Property*.* to E:\TESTING\Property*.*) (NOTE: I'm looking to use a Windows batch script as I don't have permissions to load any custom software)
Issue: I need to consolidate files from the duplicate folders into a new location and ensure any duplicate files are kept and renamed with a suffix. There are thousands of folders so I will need a looping script to minimize manual effort.
Attempt: I found a great script in the forums (copied below) that handles the duplicate file renaming really well and sets the source from a *.txt file. I can't figure out how to set the variables so I can have custom/looping target folders. (I'm happy to develop a *.txt file that will show the relevant target folder per source file if I need to)
Hoping it's something simple that I just don't know yet.
#echo off
setlocal
set "source=E:\TESTING\source.txt"
set "target=E:\TESTING\Destination3\"
for /f "delims=" %%A in (%source%) do (
if not exist "%target%\%%~nxA" (
copy "%%~A" "%target%\%%~nxA"
) else (
call :index "%%~A" "%target%\%%~nxA" "1"
)
)
exit /b
:index source, target, count
setlocal
set /a "cnt=%~3"
if exist "%target%\%~n2(%cnt%)%~x2" (
call :index "%~1" "%~2" "%cnt%+1"
) else copy "%~1" "%target%\%~n2(%cnt%)%~x2"
pause
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*" '
) DO (
rem project in %%a
FOR /f "delims=" %%b IN (
'dir /b /ad "%sourcedir%\%%a\*" '
) DO (
rem property in %%b
ECHO Project\Property being processed : %%a\%%b
rem create new? property directory
MD "%destdir%\%%b" 2>NUL
rem for all files in the property directory,
FOR /f "delims=" %%p IN (
'dir /b /a-d "%sourcedir%\%%a\%%b\*" '
) DO (
IF EXIST "%destdir%\%%b\%%p" (
rem existing filename - need to change
SET "notcopied=Y"
FOR /L %%r IN (1,1,99) DO IF DEFINED notcopied IF NOT EXIST "%destdir%\%%b\%%~np(%%r)%%~xp" COPY "%sourcedir%\%%a\%%b\%%p" "%destdir%\%%b\%%~np(%%r)%%~xp" >nul&SET "notcopied="
) ELSE (
COPY "%sourcedir%\%%a\%%b\%%p" "%destdir%\%%b\%%p" >nul
)
)
)
)
GOTO :EOF
Not really sure why you're reading a file to provide your source directory - or is it directories?
Neither am I sure why you appear to be creating the copies in the same parent - Is there some way of distinguishing a property from a project?
Anyhow, the above routine simply assigns each project directoryname in turn to %%a, and then each project subdirectory to %%b. Then read each filename within %%a\%%b to %%p, create the required subdirectory in the destination (2>nul disposes of the duplicate name messages) and see whether the file already exists in the destination.
If it doesn't, copy it as-is. If it does, then try adding (1)... to the name part until the result is not found. The notcopied flag is used as a switch as if defined is interpreted with the current value of the variable, so setting notcopied to empty makes it not defined, so the loop doesn't try to execute the test further.
OK - the limitation is the final value for %%c - the duplicate-filename counter. I set 99 as the maximum. Adjust as required. The smaller the value you use here, the faster (or more accurately, less slow) the routine is.
Coding is not my speciality but I have come across a problem and google search has guided me to using batch file process in solving it. Essentially I have a couple of thousands of files that need to be moved into folders and they have a very simple file structure, listed below:
UK--London--Filename.pdf
UK--London--Filename2.pdf
UK--Manchester--Filename3.pdf
UK--Liverpool--Filename4.pdf
UK--Chester--Filename5.pdf
I would like the script to:
1. Pick up the first "--" check if the folder exists, if not create it
2. Pick up the second "--" check if the folder exists, if not create it
3. As there might be more than two "--", ignore the rest
4. Move file into the subfolder
To that end, the output should be some like (note "FILETEST" is the folder I am using to test the script):
C:\FILETEST\UK\London\UK--London--Filename.pdf
C:\FILETEST\UK\London\UK--London--Filename2.pdf
C:\FILETEST\UK\Manchester\UK--Manchester--Filename3.pdf
C:\FILETEST\UK\Liverpool\UK--Liverpool--Filename4.pdf
C:\FILETEST\UK\Chester\UK--Chester--Filename5.pdf
I have had an attempt to re-using a script from another question in stackoverflow (Batch create folders based on part of file name and move files into that folder). I understand that it would not do exactly what I need, but cant seem to get any output.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1*delims=--" %%a IN (
'dir /b /a-d *.*.*'
) DO (
ECHO MD %%a
ECHO MOVE "%%a.%%b" --\%%a\
)
POPD
GOTO :EOF
Apologies for any headaches caused, I am hoping this is a simple one to solve.
Thank you,
Panos
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
'dir /b /a-d *--*--*.*'
) DO if "%%c" neq "" (
ECHO MD "%%a"
ECHO MD "%%a\%%b"
ECHO MOVE "%%a--%%b--%%c" ".\%%a\%%b\"
)
POPD
GOTO :EOF
Read the directory list of files in the current directory, (/a-d = no directorynames) that match *--*--*. Tokenise so that %%a acquires the part before the first --sequence, %%b the second and %%c the remainder.
If %%c is not empty then make the directories ".\%%a" and ".\%%a\%%b" (quoted because any spaces in the name would otherwise be seen as "create two directories") then move the file, again quoted for the same reason.
Note that each character individually between delims= and the close-quote is a delimiter - a delimiter-string is not supported. Consequently, this code will pick up - as well as --- and any other sequence of - and try to process it. You could gate the create/move further by adding if exist "%%a--%%b--%%c" directly after the if "%%c" neq ""before the (.
The md will create a directory if the target name does not already exist, and produce an error-message if it already exists. To suppress the error message, append 2>nul to the md lines.
assume that we have a number of video files (.mp4) along with their subtitle files (.srt) in a folder, i want to loop through all files, if two files of type (mp4 & srt) have the same name, i want to move them into a new folder having the same name of either of them. if the folder exist, then just move if not then create a new one.
i used two approaches to get the list of files
#echo off
for %%a in (*.mp4 *.srt) do (
comparison here )
pause
and
#echo off
for /f "delims=" %%f in ('dir /b *mp4* *srt*') do (
comparison procedure )
pause
i tried to store the each file name of extension (mp4) in a variable to check it against all the other files that have the extension (srt) but that did not work, i can work with text tokens with no problems
any ideas ?
This should get what you need. Using the FOR variable modifiers is the key to getting done what you need.
#echo off
for %%G in (*.srt) do (
IF EXIST "%%~nG.mp4" (
MD "%%~nG" 2>nul
IF NOT EXIST "%%~nG\%%~nG.mp4" move "%%~nG.mp4" "%%~nG"
IF NOT EXIST "%%~nG\%%~G" move "%%~G" "%%~nG"
)
)
I (unfortunately) have thousands of files which need to be reorganized into different folder scheme.
All my files follow the pattern:
d:\mainfolder\0001.pic\AMY_CAT_file1.jpg
d:\mainfolder\0001.pic\EVE_CAT_file1.jpg
d:\mainfolder\0002.pic\AMY_BIRD_file2.jpg
d:\mainfolder\0002.pic\EVE_BIRD_file2.jpg
what I'm looking for is a *.bat file that could move the files into folders that go like this:
...\CAT_\AMY\0001.pic\AMY_CAT_file1.jpg
...\CAT_\EVE\0001.pic\EVE_CAT_file1.jpg
...\BIRD\AMY\0002.pic\AMY_BIRD_file2.jpg
...\BIRD\EVE\0002.pic\EVE_BIRD_file2.jpg
in other words, the files would have to be moved:
into the subsubfolder named after source directory of a file
which is inside the folder named after 1st to 3rd
symbol, and finally
which is inside the folder which is named after the 5th to 8th symbol in the filename
I realise it might sound confusing, any help is greatly appreciated.
1st to 3rd symbol and 5th to 8th symbol is a bad idea, as the names might be of different lengths (see cat, bird, and possibly elephant; or Eve, Amy, and possibly Stephan), but you have there a nice underscore that can serve as delimiter:
#echo off
setlocal EnableDelayedExpansion
cd /d "d:\mainfolder"
for /f "delims=" %%F in ('dir /s /b *.jpg') do (
for /f "tokens=1,2,* delims=_" %%A in ("%%~nF") do (
set folder=%%~dpF
for %%X in (!folder:~0^,-1!) do (
ECHO move "%%F" "...\%%B\%%A\%%~nxX\%%~nxF"
)
)
)
Trying to figure out an efficient way to sort tv shows from download folder. I
know this is far from the best solution but it's within my comfort zone :)
Anyway, I want the batch file to search the file name and move it to the correct folder
My TV Show folder structure is TV > Show Name > Season > Files
So for example if I have a file named Archer.S01E01.mkv I'd like it moved to TV > Archer > Season 1 etc. I've created a couple variables %source% and %dest% to cut down the amount of space needed as shown below:
::Show: Archer
move %source%*archer*S01* %dest%archer\"season 1"\
move %source%*archer*S02* %dest%archer\"season 2"\
move %source%*archer*S03* %dest%archer\"season 3"\
move %source%*archer*S04* %dest%archer\"season 4"\
move %source%*archer*S05* %dest%archer\"season 5"\
move %source%*archer*S06* %dest%archer\"season 6"\
Is there a way to use an array to move the files to the correct folders? Something along the lines of:
move %source%*archer*S0[1-6]* %dest%archer\"season [1-6]"\ ?
I know that particular example won't work, I'm guessing I'll need some sort of loop? But for the life of me I have no idea how to make that work.
It would be even better if the loop would go through the files and match part of the string to the show so I wouldn't have to create a command for each and every tv show I have in my library.
Any ideas or help would be greatly appreciated!
Thanks
Assuming all files share the same template of "[ShowName].S[SeasonNumber]E[EpisodeNumber].mkv", then the following should create all folders as needed, and move the files. Please note - this is untested.
#echo off
set "source=yourSourceFolder"
set "dest=yourDestinationFolder"
pushd "%source%"
for /f "delims=" %%F in (
'dir /b /a-d *.mkv ^| findstr /rix "[^.]*\.s[0-9]*e[0-9]*\.mkv"'
) do for /f "tokens=1,2 delims=." %%A in ("%%F") do (
for /f "delims=SsEe0" %%S in ("%%B") do (
if not exist "%dest%\%%A\season %%S\" md "%dest%\%%A\season %%S\"
move "%%F" "%dest%\%%A\season %%S\" >nul
)
)
Another way (in place of season it will just create the subdirectorys SO1, SO2,...SO9 in the directory named with the SHOWNAME (Archer in your example) :
#echo off
SET "SOURCE=YOUR\SOURCE PATH"
SET "DEST=YOUR\DESTINATION PATH"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1,2* DELIMS=." %%A IN ('DIR /B/A-D "%SOURCE%\*.MKV"') DO (
SET $DEST=%%B
SET $DEST=!$DEST:~0,3!
IF NOT EXIST "%DEST%\%%A" MD "%DEST%\%%A"
IF NOT EXIST "%DEST%\%%A\!$DEST!" MD "%DEST%\%%A\!$DEST!"
MOVE "%SOURCE%\%%A.%%B.%%C" "%DEST%\%%A\!$DEST!"
)