Move Folder with parent path Windows - windows

Does somebody know a way to move a folder from one path to another with the "parent" folder ?
For example, all these folders:
Contain a folder named "Win" see below:
i want to copy the folder "Win" with the parent folder ( the numbers ) and move it to a new location example -> "C:\Storage\migration"
there are over 200 folders, so right now i am manually creating a folder named these numbers and copy & pasting the Win folder into it, it is very time consuming and not a very good long term solution :(
Maybe something could be done with a BAT. script or like ?

for /d %%a in ("d:\BaswareRay\OES\*") do (
xcopy "%%~fa\win\*" "c:\storage\migration\%%~nxa\win\" /y /s /e
echo rmdir /s /q "%%~fa\win"
)
For each folder under the indicated source, recursively copy its contents to the target folder and remove the source folder.
For each folder, the for command replaceable parameter %%a will hold a reference to the folder being processed. Using this reference, %%~fa will return the full path of the folder and %%~nxa the name and extension of the folder.
The deletion of source folder is only echoed to console. If the output is correct, remove the echo that precedes the rmdir command

Related

Is it possible to self destruct the directory of a batch and all its contents?

I have a batch which copies some files from a directory and then should delete the directory the batch file is in, but it just deletes the contents and not the folder itself. I tried with rmdir and with del. The folder structure is the following:
F:\Copy\
copy.cmd
files\
something.jar
something2.jar
Copy.cmd should copy the .jar files, to the documents folder of the user and then delete the "Copy" folder, so it leaves no traces and does not occupy disk space, but my solutions always left the empty "Copy" folder.
My three tries
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
rmdir %cd%
and
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
del %cd%
and like Compo suggested
You cannot remove the directory if it's your current working directory. Just step out of it, then delete it using its absolute or relative path.
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
cd ..
rmdir %~dp0 /S
did not work.

copy paste files using CMD

I want to accomplish the following: copy File A from Directory A to Directory B but File A already exist in Directory B and i don't want to overwrite it, simply put the new file in Directory B with a (1) or something so i'll know the difference. i can't manually name the file in the script as i plan to make it a batch file and run it every few hours therefore resulting in the same file existing with different names. IE. File A, File A(1), File A(2) so on and so on. please help. Thank you
Use Xcopy command with parameter /D
C:\> xcopy D:\source_dest E:\target_dest /E /D
/E parameter make that it copy files with subfolders
/D parameter make that it copy files without overwrite
if want more help inform me .if it works vote up
A cyclic copy refers to the situation where the source directory
contains the destination directory. Since the destination directory
is part of the source, the copying process eventually starts copying
the destination directory to a deeper portion of the destination.
This process will continue until, of course, the finite disk space
eventually runs out.
To avoid this condition but to achieve the objective, one can
specify a temporary destination which is on another volume (e.g.,
D:\temp\) and later copy the temporary destination to the final
destination, and delete the temporary directory at the end.
just check file exist before run xcopy command then run copy command
if NOT EXIST c:\directory B\File A ( xcopy c:\Directory A\File A c:\Directory B )
The following is made for folders but you can modify it for files.
Makes a backup of existed folder directory (and all contents in it) with new increase number in folder name [like TEST(1)... TEST(2)... folder].
The first part :Start will set the %PathFolder% variable with path to folder name (%userprofile%\Desktop\TEST) and then will search if exist. If NOT exists, will create it (xcopy), else, If exist, will directing the %PathFolder% variable to :Search label (for-)loop section for further handling...
Tip: Can be used %1 variable to set PathFolder set "PathFolder=%1" so it will work when you Drag-n-Drop a folder on this current saved batch file.
The second part :Search will search in the %PathFolder% variable (%userprofile%\Desktop\TEST) and will make a COPY of "TEST" folder (and all contents in it) with an increase number added in parenthesis at the end of folder name [like TEST(1)]. If already exist TEST(1) then will copy TEST folder as TEST(2) ... or TEST(3) ... and so on.
::Make a backup of existed folder directory (and all contents in it) with new increase number in folder name.
#echo off
setlocal EnableDelayedExpansion
set "IncrNum=1"
:Start
::The following will set the "%PathFolder%" variable with path to folder name (%userprofile%\Desktop\TEST) and then will search if exist. If NOT exists, will create it (xcopy), else, If exist, will directing the "%PathFolder%" variable to :Search label (for-)loop section for further handling...
::Tip: Can be used %1 instead of %userprofile%\Desktop\TEST in [set "PathFolder=..."] line so it will work when you Drag-n-Drop a folder on this current saved batch file.
set "PathFolder=%userprofile%\Desktop\TEST"
if NOT exist "%PathFolder%" (
xcopy %PathFolder% %PathFolder% /i /y
exit /b
) else (
goto :Search
)
exit /b
:Search
::The following will search in the "%PathFolder%" variable (%userprofile%\Desktop\TEST) and will make a COPY of "TEST" folder (and all contents in it) with an increase number added in parenthesis at the end of folder name [like TEST(1)]. If alredy exist TEST(1) then will copy TEST folder as TEST(2) ... or TEST(3) ... and so on.
for /f "tokens=*" %%G in ('dir /b /s /a:d "%PathFolder%*"') do (
if exist %%G^(%IncrNum%^) (
echo At "%%~dpG" a folder "%%~nG(%IncrNum%)" alredy existed.
set /a IncrNum+=1
goto :Search
) else (
echo.
echo.
echo The "%%~nG" folder and all contents in it
echo will be copied now as "%%G(%IncrNum%)".
echo.
pause
xcopy %%G %%G^(%IncrNum%^) /i /y
exit /b
)
)
:EndBatch
set "PathFolder="
pause
exit

Batch file or script to move files up X levels

We currently use a machine that generates a complicated folder structure when outputting it's data and would like to simplify the folder hierarchy of the generated data afterwards.
In our current setting, the directories look like this:
C:\Dir01\Dir02\Dir03\Dir4\*.*
C:\Dir01\Dir02A\Dir03\Dir4\*.*
C:\Dir01\Dir02B\Dir03\Dir4\*.*
C:\Dir01\Dir02C\Dir03\Dir4\*.*
C:\Dir01\Dir02D\Dir03\Dir4\*.*
etc.
The Dir01 folder is where all of our samples data are copied
The Dir02 folder(s) are created for each unique sample and will each have a unique name.
The Dir03 only contains the Dir04 folder
Dir03 and Dir04 typically have the same name every time (within a unique named Dir02)
Dir04 is where all the actual data lives and will all have a series of filenames within for every sample...i.e. file.001 file.002 file.003
The Dir04 data would ideally be moved into the Dir02 folder for each sample and then the Dir03 and Dir04 folder should be deleted after moving.
I would like to further separate out specific files within Dir02 after moving and create a new folder (Dir02A) under Dir01 to relocate those files.
In the end I would like to end up with a directory structure like this:
C:\Dir01\Dir02\*.*
C:\Dir01\Dir02A\*.*
C:\Dir01\Dir02B\*.*
C:\Dir01\Dir02C\*.*
C:\Dir01\Dir02D\*.*
etc.
Once this is complete I have a batch file that uses robocopy to duplicate the entire directory structure and separate out the non *.dcm files into a new folder, but this still leaves my complicated directory structure.
robocopy.exe "%SourcePath%" "%DestPath%" /s /xf *.dcm
That is my primary goal...rid myself of unnecessary directory navigation.
===============
Recap:
1) Data is generated beneath Dir01 to unique folders (Dir02, Dir02A, Dir02B...)
2) Run a batch file or script to perform post processing on data under Dir01
3) Move data from under each Dir04 subfolder to their parent Dir02, Dir02A, Dir02B, etc. folders
4) Clean up (now empty) Dir03 and Dir04 folders under each Dir02, Dir02A, Dir02B, etc. folder
5) Run my Robocopy batch file to move all non *.dcm files to a new location
Have you tried something like this?
cd /d c:\dir01\dir02
forfiles /s /m "*.dcm" /c "cmd.exe /c move #path #path\..\.."
This as a batch file should do the first part:
Run through every sample folder
for each one, e.g. c:\out\Dir02F move c:\out\Dir02F\dir03\dir04\* into c:\out\Dir02F\*
Remove the Dir03 and Dir04 subfolders
Edit it to cover the right folders, of course, and maybe add a bit at the end to call your Robocopy script for each folder. Tested briefly, but try it on a copy first.
for /D %%s in (c:\out\*) do (
move "%%s\Dir03\Dir04\*" "%%s\"
rmdir "%%s\Dir03\Dir04"
rmdir "%%s\Dir03"
)
Edit for comment: It will handle every Dir02 folder within Dir01. I think it could be re-written even as:
for /D %%s in (d:\t\*) do (
for /D %%y in (%%s\*) do (
for /D %%z in (%%y\*) do (
move "%%z\*" "%%s\"
)
rmdir /Q /S %%y
)
)
At least in my quick test that works without caring what Dir03 and Dir04 are called.

Windows Command Copy all files recursively to a Main Folder

I was wondering if there is a quick and dirty command that can copy all files in a directory and it's subdirectory to another folder. The destination folder will have no subdirectories just all the source files.
Also as an added bit of fun, in case there is a file name conflict, not to overwrite but to rename the destination file with something unique, maybe append _1 to the filename?
This will copy the files and prompt if there is a filename conflict.
The third party tool XXcopy has the ability to flatten a directory tree and handle filename conflicts.
#echo off
for /r "d:\folder" %%a in (*) do copy "%%a" "E:\target folder"
To copy for instance all files from the current folder and its sub directories to the parent folder of the current folder you can use:
for /r . %a in (*) do copy %a ..

How can I move the contents of one directory tree into another?

I have a directory which contains files and a number of levels of subdirectories:
C:\Source
I would like to move the contents of C:\Source into:
C:\Destination
Requirements:
All files and all subdirectories
within C:\SourceData must be moved
I will be running the command in a
batch file
I can't use Powershell or
any other scripting languages
Attempt 0
XCOPY /E "C:\Source" "C:\Destination"
This works perfectly, but it copies instead of moves. I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time.
Attempt 1
MOVE "C:\Source" "C:\Destination"
This moves the entire C:\Source directory into C:\Destination so I end up with:
C:\Destination\Source
Attempt 2
With some help from this question and accepted answer I came up with:
for /r "C:\Source" %%x in (*) do move "%%x" "C:\Destination"
This moves the files within C:\Source but not the subdirectories or their contents. Note that I used %%x instead of %x as I'm using it in a batch file.
Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?
Attempt 3
As suggested by Nick D, I tried rename:
RENAME "C:\Source" Destination
For the example scenario I gave this works fine. Unfortunately my real Destination directory is at a different level to the Source directory and this doesn't seem to be supported:
C:\>REN /?
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
I get "The syntax of the command is incorrect." errors if I try to specify a more complex destination path, for example:
RENAME "C:\Source" "C:\MyOtherDirectory\Destination"
RENAME "C:\Source" "MyOtherDirectory\Destination"
Undoubtedly use robocopy. It is a simple but brilliantly useful tool.
robocopy /move /e sourcedir destdir
This will move all the files and folders, including empty ones, deleting each original file after it has moved it.
If you don't have robocopy installed you can download it by itself or as part of a Microsoft resource kit.
Update:
Adjusted code to a. check whether folders already exist at the destination, in which case move files in that folder over (and continue traversing the source directory structure), otherwise move the folder wholesale.
At the end of the script the source folder is removed altogether to eliminate these folders which have had their files moved over to an already existent folder at the destination (meaning these folders have been emptied but not deleted at the source).
Additionally we check whether a folder is both empty and already exists at the destination in which case we do nothing (and leave the source folder to be deleted to the last line of the script). Not doing this results in "The filename, directory name, or volume label syntax is incorrect." errors.
Phew! Please let me know how you get on with this! I have tested this and it seems to be working well.
for /d /r "c:\source" %%i in (*) do if exist "c:\destination\%%~ni" (dir "%%i" | find "0 File(s)" > NUL & if errorlevel 1 move /y "%%i\*.*" "c:\destination\%%~ni") else (move /y "%%i" "c:\destination")
move /y c:\source\*.* c:\destination
rd /s /q c:\source
In response to ufukgun's answer:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
/s - Copies directories and subdirectories except empty ones.
/e - Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/i - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/y - Suppresses prompting to confirm you want to overwrite an existing destination file.
As stated in another answer, using xcopy may be not as efficient as it would be a native move command that only changes pointers in the filesystem.
Since XCOPY works, you could use XCOPY and DELETE, it's a workaround, but should work?
on Vista use
robocopy source destination /MIR
/MIR .. mirror a complete directory tree (also deletes files in the destination)
else
xcopy
Of course you have to delete the source afterwards :)
I have a directory which contains
files and a number of levels of
subdirectories:
C:\Source
I would like to move the contents of
C:\Source into:
C:\Destination
Maybe I'm missing something, but can't you just rename the folder?
As sent on twitter:
Try combining attempt 3 with attempt 1. Rename to get the destination folder correct, then move "Destination" to the correct place.
#echo on
set SOURCE=C:\Source
set DESTINATION=C:\Destination
xcopy %SOURCE%\* %DESTINATION%\* /s /e /i /Y
PAUSE
i use batch file like this...
or simply call:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
I use a utility called xxcopy. It can move files and have many useful options, you can use it like this :
xxcopy C:\Source C:\Destination /E /RC
the options :
/E to copy everything even empty folders
/RC to remove every source file after each successful copy
you can download free copy of xxcopy for personal use from :
http://www.xxcopy.com/xcpydnld.htm
Maybe a little off-topic, but still usefull:
Some people suggest to just copy + delete the source files, but
moving files is not the same as copying + deleting!
When using the (x)copy function, you allocate new space on the same volume on a harddisk. After the copying the files, the old files (the old allocated space which was required for the files) are beign marked as deleted. While you will achieve the same result as an end-user, moving does something diffrent and more efficient.
Moving files actually only changes some records in the MFT (master file table). These changes only consist of a different path for the user to locate its files. Physically the files still remain in the same sectors on the harddisk.
If all your trying to do is move a directory and the content up one level:
MOVE folder_you_wan_to_move ..
Note that .. refers to the next directory up.
I leave this code I have written based on ljs's attempt,
It moves directory trees as Windows does, overwriting the files that already exists in destination with those in source, and avoiding the slow copy and erase method except if the destination is in a different drive.
If your S.O is not in english must change line 6 with the text used by the Dir command when it finds 0 files.
movedir.bat source_dir destination_dir
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if %1.==. echo movedir dir1[\*] dir2 & echo move dir1 inside dir2 overwriting coincident files & echo option \* moves only dir1 content & goto end
set S=%~f1
set D=%~f2
set "noFiles= 0 File"
set "R=0" rem R is option [\*] flag
if %S:~-2%.==\.. set "S=%S:~,-2%" & set "R=1"
if not exist "%S%" goto paramERR
if not exist "%D%" goto paramERR
set "Trim=0" & set "Sc=%S%"
:LP
set /A Trim+=1 & (set "Sc=!Sc:~1!") & if NOT !Sc!.==. goto LP
if %R%==0 (if exist "%D%\%~n1%~x1" (set "D=%D%\%~n1%~x1")) else move /y "%S%" "%D%" & goto end
CALL:movefiles "%S%" "%D%"
for /D /R "%S%" %%I in (*) do (set "Way=%%~fI") & (set "Way=!Way:~%Trim%!") & if exist "%D%!Way!" (CALL:movefiles "%%I" "%D%!Way!") else (move /y "%%I" "%D%!Way!\.." > NUL)
rd /s/q "%S%" & if %R%==1 md "%S%"
goto end
:movefiles
dir %1 | find "%noFiles%" > NUL & if ERRORLEVEL 1 move /y "%~1\*.*" %2 > NUL
goto :eof
:paramERR
echo Source or Destination does not exist
:end
I think the easiest thing you could do is to modify your Attempt 2
from
MOVE "C:\Source" "C:\Destination"
to
MOVE "C:\Source\*" "C:\Destination"
KISS ;-)
Edit: this seem not to work, so my advice is to trash away the crappy DOS command line and use Cygwin with BASH as a shell! (or just add the cygwin binaries to the path so you can use mv within DOS, thus not having to change the shell as your requirements state).

Resources