Context
I'm trying to make a batch script that can be thrown into a pre-existing folder and upgrade it. Imagine a folder that looks like this:
domfolder
LICENSE
merge_new_stuff.bat
words.txt (has 6 words)
awesomefolder
garfield.png
upgrade_assets
upgradeinfo.txt
new_files
words.txt (has 7 words)
awesomefolder
odie.png
awesomest
MONKEY.txt
subfolder
subsubfolder
numbers.txt
The sole purpose of merge_new_stuff.bat is to take everything inside the new_files folder, and bring it into domfolder, replacing any pre-existing files like words.txt, leaving anything else alone, and getting rid of the upgrade_assets folder. So if everything goes right, the folder structure should like this after running:
domfolder
LICENSE
merge_new_stuff.bat
words.txt (has 7 words)
awesomefolder
garfield.png
odie.png
awesomest
MONKEY.txt
subfolder
subsubfolder
numbers.txt
What I've Tried
First instinct is to just use a simple move command: move /y upgrade_assets\new_files\*
That upgrades words.txt, but it totally ignores the folders. Only part of what I want.
I tried a for loop to see if it'd be different somehow, same results:
for %%A in (upgrade_assets\new_files\*) do move /y %%A
Next, I tried looping through dir results alongside the basic move:
for /f "tokens=*" %%A in ('dir /b /s /a:d "upgrade_assets\new_files"') do move /y "%%A\*.*" "%~dp0%%~nxA"
This almost worked, properly moving odie.png and subfolder\subsubfolder\numbers.txt. But once it tries to move MONKEY.txt, it... decides to make a file called "awesomest" that has the contents of MONKEY.txt? So clearly, it doesn't handle going down pre-existing folders, which is definitely not helpful.
Looking around, I only found one thread that seems close to what I want. Most of the replies mention xcopy/robocopy, but I want to avoid the copy/delete method as much as possible considering the potential size of updates. And when I changed the accepted answer to fit what I'm doing, it still had issues going more than one folder deep.
for /d /r upgrade_assets\new_files %%A in (*) do (
if exist "%~dp0%%~nA" (
dir "%%A" | find "0 File(s)" > NUL
if errorlevel 1 move /y "%%A\*.*" "%~dp0%%~nA"
) else (
move /y "%%A" %~dp0
)
)
I'm pretty sure the issue is centered around %~dp0%%~nA, I need a way to have it read more than just the folder the file being copied is in, which is all %%~nA shows.
Preferably, the solution should be completely independent of what actual files are in the folder, so it can be used with any update. If I need external tools, I would like them to be free/libre software if possible. I'm running Windows 10, but I know this script will be run by people using Windows 7.
Quite frankly this will do exactly what you want.
robocopy .\upgrade_assets\new_files\ . /E /MOVE
It will create the directories and files as well as delete from source once done.
i need something that will move files from one folder to another network location, but leave any locked files that are currently being written to!
any ideas?
apols i am no developer, but i have been trying to use
ROBOCOPY C:\test c:\test\q /move /R:0 /W:0
but even if i open a file, it still moves it to the new directory
mal
You can use for %%f in (*) do move %%f destination\%%f (in a batch file) and just allow each individual invocation of move to fail, watching the errors scroll by.
There are also options for making for %%f work recursively, though that's a bit involved.
Every month I have a very large number of files (in many subfolders) in a particular folder. I need to move them all into a different folder. In an attempt to automate the process of moving them I used robocopy in a batch file. It works fine, but takes HOURS to run. (It is many many GB).
Now, if I do it manually in Windows Explorer, by opening said folder, selecting all, and right-dragging to destination folder, and choosing "Move Here", it moves INSTANTLY. (Windows XP must be pruning and grafting the directory entries, without ever making a second copy of the files. ... and yes, source and destination are on same partition.)
So, QUESTION IS: Does anyone know of a program I can run from a batch file to move files in this instantaneous way? (need to move entire sub-folder tree)
You can use MOVE for this:
C:\>MOVE /?
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.
/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
For example:
C:\Users\test>mkdir to
C:\Users\test>move from\*.txt to
C:\Users\test\from\1.txt
C:\Users\test\from\2.txt
2 file(s) moved.
With a point in the right direction from #psmears, and a lot of googling, I found the (or a) solution:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM ** Change to source dir
L:
cd "L:\Backups\Source"
REM ** Move files recursively
for /R %%G in (.) do (
set mydir=%%G
set mynewdir=!mydir:~18!
md "L:\DEST\!mynewdir!"
cd "L:\DEST\!mynewdir!"
move "%%G\*.*" .
)
REM ** Remove now-empty sub-dir structure inside source
L:
cd "L:\Backups\Source"
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"
I've been looking around the Internet and finding bits of pieces of what I need but I figured coming here is my best bet.
I'm creating a project right now and it requires a batch file to locate a specific file on my system (Yes its unique) to be ran.
Say the file I have in mind is named helloWorld.vbs, I want that opened and its located on my Desktop which is C:\Users\Myself\Desktop
What would be a line of code I could input into the CLI to
A.) Find the specified file no matter where it actually is in my system, it could be in documents or anywhere the user places it.
B.) Running the actual program and not just listing the directory of where its currently at.
C.) Staying within the specified directory.
A1.) change to Users HomeDir: (cd C:\ to search the whole drive - but this would take some time...)
cd %userprofile%
A2.) find the file and it's location
for /f "delims=" %%i in ('dir /s /b "my filename.exe"') do (
set "file=%%~nxi"
set "filepath=%%dpi"
)
echo found %file% in %filepath%
B.) goto the directory and execute the file
cd %filepath%
"%file%"
REM or start or call (depends on your needs)
C.) stay in the directory (nothing to do)
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).