I have subfolders with the following naming convention:
000026867_20200722_222406_SS24
I want to combine the contents of all folders that share the same last portion of the name. In this case all folders ending in SS24. It will always be after the 3rd underscore, but there is a chance it might be more than 4 characters after the last underscore.
I want all the files in all the folders ending in SS24 to be in a new folder named, let's say, All_SS24
The original folders can be deleted.
#Echo off
pushd "C:\path\to\your\base\folder"
for /f "Tokens=1* Delims=-" %%A in ( 'Dir /B /AD -' ) Do If Not Exist "%%A" (
Ren "%%A-%%B" "%%A"
) Else (
Move /Y "%%A-%%B*" "%%A\" RmDir "%%A-%%B"
)
PopD
Thanks for your help.
This is more or less what you seem to want:
#echo off
pushd "C:\path\to\your\base\folder"
for /f "tokens=1-4*delims=_" %%a in ('dir /b /ad "*_*"') do (
mkdir "%%d">nul 2>&1
copy "%%a_%%b_%%c_%%d\*" "%%d" /Y
rd "%%a_%%b_%%c_%%d" /S /Q
)
popd
Related
I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)
I want to copy a specific file to a target directory structure meaning to all folders and sub-folder and sub-sub-folder. Basically to a directory tree.
I tried using robocopy, but it has only option to defile level on source and not on target
This is what I have tried so far with robocopy and simple batch
:: copies to only one target directory
robocopy "%SOURCE_FILE_DIR_PATH%" "%TARGET_ROOT_FOLDER_PATH%" %FILE_NAME%
:: copies to only 1 sub-level
for /f "delims=" %%a in ('dir /b "%TARGET_ROOT_FOLDER_PATH%"') do (
for /f "delims=" %%b in ('dir /b "%TARGET_ROOT_FOLDER_PATH%\%%a"')
do (
copy /y "%FILE_NAME%" "%TARGET_ROOT_FOLDER_PATH%\%%a\%%b"
)
)
You can combine the /D and /R options to iterate a directory tree. So I think this would work for you.
set "TARGET_ROOT_FOLDER_PATH=C:\folder\subfolder"
set "FILE_NAME=foo.txt"
FOR /D /R "%TARGET_ROOT_FOLDER_PATH%" %%G IN (*) DO COPY /y "%FILE_NAME%" "%%G"
You could modify your code slightly to use the /AD and /S options of the DIR command.
for /f "delims=" %%G in ('dir /ad /b /s "%TARGET_ROOT_FOLDER_PATH%"') do copy "%FILE_NAME%" "%%G"
I have a folder That has subfolders as below : I was able to loop and copy the folder and subfolders from test1 to test 3. But I want to extend the functionality by removing the each folder after copying
but am not able to instead is removing all the test 1 to test 3 folder.The test folder is just an example, the folders can be any name.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Copy succesffully copied reason Files were copied without error and you can delete the content folder
rd "%%G\*" /s /q
mkdir "%%G"
)
)
popD
pause
If you resort to XCOPY, why not use the "successor" ROBOCOPY without any loop, deleting and re-creating the folder? Like in
robocopy "C:\BACKUP_FDM_FILES\localbackup\" "C:\FDM\Upload\localbackup" /s /e /COPYALL /dcopy:T /move
This will even keep the timestamp on the moved folder, and all attributes on moved files. Added benefit: you can use all the other helpful options of this command at no extra cost.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Successfully copied now you can delete folder
popd
rd "%%G\*" /s /q
mkdir "%%G"
)
pause
You are using pushd to go to backup folder so you need to use popd to go back to parent folder.
SS64 - POPD
I have fildes witht he names:
fd3_xxx
fd3_xx1
fd3_xx2
fd6_xxx
fd6_xx1
fd6_xx2
How can I write a batch file that would put all the "fd3" files in a folder called "fd3", and all the "fd6" files into a folder called "fd6'.
#echo off
for %%a in (*.*) do (
for /F "delims=_" %%b in ("%%a") do (
if not exist "%%b" md "%%b"
move "%%a" "%%b"
)
)
This should do it
xcopy fd3_* fd3 /i
xcopy fd6_* fd6 /i
del fd3_* /f /q
del fd6_* /f /q
Maybe try it on some test folders first to make sure it does what you want it to.
I need a batch file which remove all empty directories and directories which contain only specific file (.svn).Thank you very much for any help.
Example :
Folder1 -- folder conatins subfolders with some files, not remove
Folder2 -- empty folder, folder should be deleted
Folder3 -- folder contains only .svn, folder should be deleted
.svn
Folder4 -- folder contains subfolder with file, not remove
Folder41 -- folder contains some file, not remove
somefile.dat
Folder5 -- folder contains some file, not remove
.svn
somefile.dat
Folder6 -- folder contains empty subfolders, folder should be deleted
Folder61 -- folder contains only specific file, folder should be deleted
.svn
Result:
Folder1
Folder4
Folder41
somefile.dat
Folder5
.svn
somefile.dat
I was surprised at how little code this takes. The biggest trick is to use SORT /R on the output of DIR /B /S /AD so that you can process the child nodes prior to processing the parent nodes of the folder hierarchy.
I've quickly tested the code, but please test yourself in a safe manner.
This 1st version assumes ".SVN" is the full file name.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%A in ('dir /b /s /ad ^| sort /r') do (
dir /b "%%A" | findstr /livx ".svn" >nul || rd /s /q "%%A"
)
The 2nd version is only slightly modified in case you meant "*.SVN". Only one FINDSTR option is changed.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%A in ('dir /b /s /ad ^| sort /r') do (
dir /b "%%A" | findstr /live ".svn" >nul || rd /s /q "%%A"
)
Update: 2012-11-06
I just realized that the above solution(s) can be defeated if a non-empty folder is named .SVN. The code should remove folders that are either empty or only contain a file named .SVN. It should not remove a folder if it contains a non-empty folder named .SVN.
Below is a fix for the first solution:
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%A in ('dir /b /s /ad ^| sort /r') do (
dir /b "%%A"|findstr /livx ".svn" >nul||>nul 2>&1 dir /b /ad .svn||rd /s /q "%%A"
)
And here is a fix for the second solution
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%A in ('dir /b /s /ad ^| sort /r') do (
dir /b "%%A"|findstr /live ".svn" >nul||>nul 2>&1 dir /b /ad *.svn||rd /s /q "%%A"
)