I am after a script to be able to do the following
I have about 2000 directories with files and folders inside them
I want to delete the main folder but keep everything inside the folder
So an exmaple of one folder
Folder A / Folder 2 / Video 1
Image1
Image2
So I want it to be able to delete the main first folder, in this case it would be 'Folder A'
Folder 2 / Video 1
Image1
Image2
Hope you can understand what I am wanting to do if not just ask! Cheers
Extra info
From this
C:/folder A/Stuff
C:/folder B/Stuff
C:/folder C/Stuff
To this
C:/stuff
C:/sfuff
C:/sfuff
I just want this bat to be able to delete the folder and move everything from within that folder to the place I ran the .bat, I also want to be able to run the .bat on other drives D:/ E;/ F;/ many locations
If you have this structure in your disk:
C:\Folder A
| | Folder 2
| | | Video 1
| | | | Image1
| | | | Image2
... and you execute these commands:
cd "\Folder A"
move "Folder 2" ..
... then you get this result:
C:\Folder A
| Folder 2
| | Video 1
| | | Image1
| | | Image2
That is, it move "Folder 2" one level up, to the same level of "Folder A". If you want to do the same thing with all folders inside "Folder A" and then delete it, this Batch file do that:
@echo off
cd "\Folder A"
for /F "delims=" %%a in ('dir /B /AD') do (
move "%%a" ..
)
cd ..
rd "\Folder A"
Is this what you want?
EDIT: New version added as answer to new comments
@echo off
rem Eliminate all folders present at the same level of the Batch file
rem and move their contents one level up
for /F "delims=" %%a in ('dir /B /AD') do (
cd "%%a"
for /F "delims=" %%b in ('dir /B /AD') do (
move "%%b" ..
)
move *.* ..
cd ..
rd "%%a"
)
You must note that if two or more folders or files in the stuff have the same name, and they were originally placed directly below the top-level folders that are being deleted, then the program will issue an error and the folder or file will not be moved (and the top-level folder will not be deleted).
Antonio
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "relroot=c:\destdir"
SET "relrootd=c:\destdir"
SET "eliminate=folder a"
XCOPY /t /e "%relroot%\%eliminate%" "%relrootd%"
FOR /f "delims=" %%i IN ('dir /s /b /a-d "%relroot%\%eliminate%"') DO (
SET dest=%%i
SET dest=!dest:%relroot%\%eliminate%=%relrootd%!
ECHO MOVE "%%i" "!dest!" >nul
)
This should do it - but it likely won't deal with some poison
file/directoryname characters that have a special meaning to CMD.EXE
like ^&!%
- regular alphamerics, symbols and spaces should be fine
relroot
is the source, relrootd
is the destination. They can be the same or different if you like. I used c:\\destdir
because that's the directory I use for testing batchfiles.
The XCOPY
creates an appropriate empty tree.
The DIR
output in /b
basic mode /s
including subdirectories /ad
excluding directorynames is processed line-by-line, the original relroot
and directory-to- eliminate
is replaced by the destination relroot and the PROPOSED MOVE
operation displayed.
After verification, remove the ECHO
keyword to ACTIVATE the move.
I'd check your original directory relroot\\eliminate
for emptiness before removing it - just to be on the safe side...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.