Move Files to Folders with same name - sorting

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

Related

How to use a variable that resets in every folder to batch-rename files?

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.

How to select randomly a certain number of files (same filename but 4/5 different extensions) and move them to another folder?

I have a directory with hundreds of files with descriptive, different names but always the same extensions (example):
LetItBe(TheBeatles).mid
LetItBe(TheBeatles).jpg
LetItBe(TheBeatles).ran
LetItBe(TheBeatles).zip
LetItBe(TheBeatles)Scan.mid
Scan would always be at end of filename and always the same word)*
Next group:
HeyJude(TheBeatles).mid
HeyJude(TheBeatles).jpg
HeyJude(TheBeatles).ran
HeyJude(TheBeatles).zip
HeyJude(TheBeatles)Scan.mid
I want the batch to
Randomly select a number of files (say 10) and
copy ALL of the associated files (i.e. the two MID, the jpg, the ran, the zip) with the same filename to a different directory.
I don't want the batch to stop if, for example, one of the groups of files is missing the jpg file or the mid file, but just to copy the existing files to the new folder and move on to the next randomly selected group.
I've found a batch code that works in moving one MIDI file randomly, but I'm not sure how to insert the code that specifies all files with the same filename but different extensions should also be moved...
#echo off
set folder=C:\Test1
set destfolder=C:\Test2
for /f "delims=" %%C in ('dir /b /a-d "%folder%\*.mid" ^| find /c /v ""') do set /A num=%random% %% %%C
for /f "delims=" %%F in ('dir /b /a-d "%folder%\*.mid" ^| more +%num%') do set name=%%F & goto next
:next
echo (By the way, I chose %name% )
move "%folder%\%name%" "%destfolder%\%name%"
User Mofi kindly offered this help..
The command to copy all files starting with same string is copy
"%FileName%." "C:\Destination Folder" with environment variable
FileName being defined by the code selecting randomly one of the file
groups, for example, with set "FileName=HeyJude(TheBeatles)". More
safe would be using two COPY command lines: copy "%FileName%.*"
"C:\Destination Folder" and copy "%FileName%Scan.mid" "C:\Destination
Folder" 2>nul in case of there are the file sets HeyJude(TheBeatles)
and HeyJude(TheBeatles) Live and file set HeyJude(TheBeatles) should
be copied to destination folder.
But I can't figure out how to insert it into the existing code. I tried editing the last line to be
move "%folder%\%name%*.*
But then no files at all are moved and the system can't find any matching filename.
Help appreciated!
For randomly selecting files, you can use %random% and iterate 10 times. This will randomly select 10 file names, then copy the file name `.* meaning any file with the given name, with any extension will be copied.
#echo off & setlocal enabledelayedexpansion
set "folder=C:\Test1"
set "destfolder=C:\Test2"
set num=0
pushd "%folder%"
for %%a in (*.*) do (
set /a num+=1
set "name[!num!]=%%~na"
)
for /l %%i in (1,1,10) do (
call :randm
)
popd
goto :eof
:randm
set /a "rnd=(num*%random%)/32768+1"
copy "!name[%rnd%]!*" "%destfolder%"

Batch Script to delete only files with .zip extension

I want to skip the latest 3 files created and delete the rest of the files. What I need is that if there is text, xml and zip files we need to delete only the zip files and leave the text and xml files behind and the total files left should be 3.If there are 3 or more files other than .zip files, delete all .zip files; if there are less, keep the newest .zip files so that there are 3 files left in total. Can anyone help. I am stuck with this
For example(inside bracket created date of files):
Folder A contains - aa.txt(2/1/18), bb.xml(3/1/18), cc.zip(4/1/18), dd.zip(2/1/18),ee.zip(5/1/18)
What I need after deleting is
aa.txt, bb.xml, ee.zip
This is what I have written
#ECHO OFF
SETLOCAL
SET "targetdir=C:\source"
SET /a retain=3
FOR /f "skip=%retain%delims=" %%a IN (
'dir /b /a-d /o-d "%targetdir%\*.zip" '
) DO DEL (DEL "%targetdir%\%%a.zip"
GOTO :EOF
Given the parameters of your question, a very simple method, although not the most efficient or speedy, would be to do this:
#Echo Off
Set "targetdir=C:\source"
Set "retain=3"
CD /D "%targetdir%" 2>Nul || Exit /B
For /F "Skip=%retain% Delims=" %%A In ('Dir /B/A-D/O-D/TC'
) Do If /I "%%~xA"==".zip" Del "%%A"

Moving files from Subfolders into root directory, but not copying them to the next directory when ran again

I have a folder that contains subfolders with MP4 files. I'm trying to write a script that will move the MP4 files out of the subfolders into the root folder when ran. The batch file I wrote is working, but when the batch script runs again for new subfolders, the MP4 files that were already copied to the root folder, get moved up another level in the file structure. For example:
C:\MainRoot\Root\Subfolder\media.mp4
When script is ran, 'media.mp4' gets moved up to C:\Root\media.mp4 as desired.
But since I need the script to run on a scheduled task. The next time the script runs I get the following:
C:\MainRoot\media.mp4
Instead of just the MP4 file staying in C:\MainRoot\Root.
Here's my batch file so far to copy the mp4 files:
set root_folder=C:\MainRoot\Root
for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".mp4" ') do (
move /y "%%G" "%%~dpG..\%%~nxG"
)
What do I need to modify so that once moved, the MP4 files will stay in place?
Any help would be greatly appreciated!
Since all your source files seem to be at a certain directory level, a for /D loop could be wrapped around your for /F loop, which parses the output of a non-recursive dir command line (no /S):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=C:\MainRoot\Root"
set "_PATTERN=*.mp4"
rem // Loop through sub-directories:
for /D %%D in ("%_ROOT%\*") do (
rem // Loop through matching files:
for /F "eol=| delims=" %%F in ('dir /B "%%~fD\%_PATTERN%"') do (
rem // Avoid overwriting destination file:
if not exist "%_ROOT%\%%~nxF" (
rem // Move matching file one level up:
move /Y "%%~fD\%%~nxF" "%_ROOT%\%%~nxF"
)
)
)
endlocal
exit /B
If you are happy to overwrite as in your provided example then something as simple as this may suit your purpose:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /R %%G In (*.mp4) Do If /I NOT "%~dpG"=="%root_folder%\" Move "%%G">Nul 2>&1
If the files are only one folder deep you may prefer this:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /D %%G In (*) Do Move "%%G\*.mp4">Nul 2>&1

Copy files from folder of lnk files back into folder

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

Resources