How do I get the contents of a folder (including empty folders) to be moved (not copied) into a new folder excluding one folder?
I want to move the contents in the Entity folder into my newly created folder 2013\November Estimates that is in each entity.
The folder layout is 2013\2 - November Projections\Fund Family (32 of them, plus other folder not concerned with)\Entity (600 different entities unique to each fund family)
This is the current layout for each entity:
Entity_01
Entity_01\2013
Entity_01\2013\November Estimates
Entity_01\entity.pdf
Entity_01\entity.xls
Entity_01\entity.doc
Entity_01\PY folder
Entity_01\Trash Folder
Entity_01\2012
So all the files and folders are moved to November Estimates but the folder 2012 stays where it is (producing the following layout)
Entity_01
Entity_01\2013
Entity_01\2013\November Estimates
Entity_01\2013\November Estimates\entity.pdf
Entity_01\2013\November Estimates\entity.xls
Entity_01\2013\November Estimates\entity.doc
Entity_01\2013\November Estimates\PY folder
Entity_01\2013\November Estimates\Trash Folder
Entity_01\2012
Issues with the following command line:
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
Complete BAT
#echo off
setlocal
SET "sourcedrive=Z"
SET "destdrive=C"
SET /a year=2013
SET "projections=2 - November Projections"
SET "Estimates=November Estimates"
SET "Yearend=Year End Filings"
DIR /b /ad "%sourcedrive%:\%year%\%projections%" >fundfamily.txt
start /wait "Fund Family" C:\Windows\System32\notepad.exe "fundfamily.txt"
FOR /f "delims=" %%a IN (fundfamily.txt) DO (
MD "%destdrive%:\%year%\%projections%\%%~a"
DIR /b /ad "%sourcedrive%:\%year%\%projections%\%%~a" >entitylist.txt
start /wait "%%~a entities" C:\Windows\System32\notepad.exe "entitylist.txt"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%yearend%"
FOR /f "delims=" %%b IN (entitylist.txt) DO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\2012"
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
)
regarding the line that produces error, if there're only one folder you want to avoid moving, you can check if the folder to move is not the one:
FOR /f "delims=" %%b IN (entitylist.txt) DO (
if "%destdrive%:\%year%\%projections%\%%~a\%%~b\2012" NEQ "%%b" MOVE /Y "%destdrive%:\%year%\%projections%\%%~a\%%~b\%all%" "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
)
Try this as your last loop as you will need a loop to move the folders one by one, and another command to move the files in the main folder.
Perhaps something like this will do it.
FOR /f "delims=" %%b IN (entitylist.txt) DO (
pushd "%destdrive%:\%year%\%projections%\%%~a\%%~b\" && (
for /d %%c in (*) do if /i not "%%c"=="2012" move "%%c" "%estimates%"
move *.* "%estimates%"
popd
)
)
This does the following (Im going to use pictures, don't know why i didnt think of that before... -_-
CURRENT RESULTS:
DESIRED RESULTS:
it basically renames the the 2013 folder in the entity and moves everything except 2012 into that folder, which now has the November Estimates name
Related
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
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 am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.
Batch file to create folder from part of file name and move files.
I have lots of files that need to be in their own sub folder.
They are in this format:
Example
123456-ABC-XYZ
123456-DFG-XYZ
123456-HIJ-XYZ
Where I would like to create a sub folder and move the files to it by characters 8-10
or by 12-14
Used this as a base that I found on this site posted by Magoo
and it works to do it by the 1st set of characters (h:\1AAATEST = my test folder
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*_*-*-* *.*"'
DO
(MD %%b MOVE "%%a %%b" .\%%b\)
POPD
GOTO :EOF
[Editing note from Magoo:
This is incorrect. Here's what I posted:
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*_*-*-* *.*"'
) DO (
ECHO MD %%a
ECHO MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
reference: Question from nearly a year ago
Notice that there are major difference beyond the change of source directory and omission of the echo keyword as narrated. OP's syntax in this question simply won't work.
]
I modified it with the same results as the original
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*-*-*.*"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
Then again, and it created 4 folders from this file name
LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1
But didn't move any files.
1st folder = -
2nd = 11-18-10_1
3rd = Diagnostic
4th = Topography
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*-*-*.*"'
) DO (
MD %%b
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
I came up with the following:
#echo off
for /f "tokens=1-3* delims=-," %%a in ('dir /b /a-d "*-*-*"') do (
(md "%%~nb" 2>nul)
(MOVE "%%a-%%b-%%c" "%%b"))
POPD
GOTO :EOF
It works for making directory and moving files for the 1st and 2nd section of the file name,
by changing the md to a & the move target dir to a for 1st part of file name &
by changing the md to b & the move target dir to b for 2nd part of file name &
BUT changing the md to c & the move target dir to c for 3rd part of file name &
DOESN"T work. It creates the correct dir, and it doesn't move the files but it shortens the filenamnes to XYZ.
Suggestions would be appreciated.
I received an angry telephone call one day from someone who'd used a library I published and it had a major fault. In reality, it had been modified and republished by someone with no note as to who, when or why in the library source. That was the last time I ever published sourcecode for my commercial work.
The problem you are encountering is very simply explained.
The for /f examines LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1 and assigns LAST_FIRST_7-24-1936 to %%a and Diagnostic - Topography 11-18-10_1 to %%b
In the code as you have modified it, you are then executing MD %%b which will be interpreted as
MD Diagnostic - Topography 11-18-10_1
so batch creates those four directories, as requested.
Then the move is encountered. The destination directory is .\%%a\ which will attempt to move the file to .\LAST_FIRST_7-24-1936\ - a directory which is unlikely to exist.
So - to fix the problem, try replacing MD %%b with MD %%a as was specified in the original code.
As for your requirement for 123456-ABC-XYZ, then try
#ECHO OFF
SETLOCAL
SET "sourcedir=h:\1AAATEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2,*" %%a IN (
'dir /b /a-d "*-*-*"'
) DO (
MD %%b
MOVE "%%a-%%b-%%c" .\%%b\
)
POPD
GOTO :EOF
(this version to move to the directory with the middle block of characters - to move to the end-block, replace %%b with %%c in both the MD command and the destination of the move command)
BTW - it's very likely that the md will complain about attempting to create an existing directory. You can shut it up by appending 2>nul to the end of the md command.
I'm looking to make a batch script that will move all images in sub subfolders down to the layer above the batch script. For Example:
comics\move.bat
comics\series 000\series 000 blah\01.jpg,02.jpg, etc.
comics\series 001\series 001 blah\series blah\01.jpg,02.jpg, etc.
into
comics\series 000\01.jpg,02.jpg, etc.
comics\series 001\01.jpg,02.jpg, etc.
I've tried several variations of
for /r %%i in (*.jpg) do move "%%~fi" "%%~pi*.jpg"
But it won't do anything. One way I had it would move all images into the same folder as I was running the script from but that's the closest I've gotten. A clean up of the now empty folders would be nice too!
This should move the images in each branch into the first level folder, but doesn't deal with filename collisions.
Test it on a folder of copies of your files.
#echo off
for /d %%a in (*) do (
pushd "%%a"
for /r %%b in (*.jpg) do move "%%b" .
popd
)
pause
for /L %%a in (1,1,5) do for /d /r %%b in (*) do rd "%%b" 2>nul
The last line should remove only empty folders up to 5 levels deep, if they exist.
Create a .bat file with following content on the root folder and double click it.
#echo off
for /f "usebackq tokens=*" %%d in (`dir /s /b /o:n /ad`) do (
cd "%%d"
for /f "usebackq tokens=*" %%f in (`dir /b /o:n /a-d`) do (
move /y "%%f" ..
)
)
pause
To explain the code, the outer loop iterates directories recursively and cd into it. The inner loop iterates all files inside the directory and move it one up above. /y flag on move is to suppress prompting. Use dir /? for explanation of all the dir flags. As for the for part I took it from https://stackoverflow.com/a/2768660/179630
Try this:
move comics\series 000\series 000 blah\*.jpg comics\series 000