Need batch program that moves files without copying then deleting them - windows

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"

Related

Move Batch files to folder with similar name

I am trying to move batch files to their respective folder, they have similar names expect the batch files I want to move have "Build" on the end. For example:
Folder name: wld-vine-tstrm
Bat file name: wld-vine-tstrmBuild.bat
In the same parent directory, I also have batch files with identical names to the folder, I do not want these to move, only the ones with "Build" at the end of the file names.
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
This is very close to the result I am looking for; it moves the "Build" batches to the respective folder, but it also removes the last 5 characters from the batches I dont want to move.
The below batch files are now missing the last 5 chars, "tstrm"
To summarize, There are two batch files for each folder in a directory, I only want to move the one ending "Build" to its folder.
You should use this:
setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*build.bat) do (
set "foldername=%%~nf"
move "%%f" "!foldername:~0,-5!"
)
The wildcard *build.bat finds all batch files ending with "build" but wildcard "*.bat" shows them all.

robocopy copy files to a new folder in the current folder without the new created folder

i want to copy the WHOLE folder in a new subfolder. currently i am using:
(robocopy "C:/Test" "C:/Test/a" /E> output.log) ^& IF %ERRORLEVEL% LSS 8 SET ERRORLEVEL = 0
after running this, there is in "C:/Test/a" also the "/a" folder. i dont want that new folder.
The robocopy command seems to get confused when a full source directory tree is to be copied and the destination directory is located somewhere in the source directory tree. Therefore your command line results in a directory C:\Test\a\a that partially contains stuff from the source.
Try with the /L option (do not copy but list items that would be copied), so the output log shows exactly what you would expect. As soon as you actually do copy (so with /L removed), there are some more items listed in the log unexpectedly. This behaviour indicates that robocopy does not evaluate the whole source directory tree in advance, but during the actual copying operation.
The easiest way to avoid that is to exclude the destination directory from being (re-)copied:
robocopy "C:\Test" "C:\Test\a" /E /XD "C:\Test\a"
By the way, the correct Windows path separator is the \. Although robocopy also accepts /, you should stick to \, because many commands do misinterpret the /.

Moving file filter (Windows)

I'm facing problem to create a specific filter to move files in Windows. The problem is, I have a file with the name ADB.inp and based on him the system will create others files with the name ADB001.inp, ADB002.inp, ADB976.inp, etc., and I need to move these files to another folder but keep the original ADB.inp in the folder.
Another observation is that the ADB.inp need to be in the same folder that the files is created. This way I can't make a filter like
*.inp
or
ADB*
or
ADB0*.
Anyone knows a way that a can filter these files?
The Windows batch file name masking capabilities are limited to * (any 0 or more characters), or ? (0 or 1 character, except .). So it cannot be done with a single MOVE command.
The simplest solution is to use two MOVE commands - one to move everything, and then another to move the template back to the original location.
Assuming your current directory is the starting location of all the files:
move adb*.inp newLocation
move newLocation\adb.inp .
Another option is to temporarily hide the template, but that requires three steps:
attrib +h adb.inp
move adb*.inp newLocation
attrib -h adb.inp
If the template must remain (unhidden) in the correct folder at all times, then you will have to iterate the files and move them one by one:
for %%F in (adb*.inp) do if /i "%%F" neq adb.inp move "%%F" newLocation
or
for /f "delims=" %%F in ('dir /a-d /b adb*.inp^|findstr /lix adb.inp') do move "%%F" newLocation

Need a BAT file to copy & rename all files in specific tree

I need to create a .bat that runs through a multilayered directory... copying certain files that contain the following suffix: '.full.jpg' to save as '.jpg'
What I've tried:
copy /y "C:\Users\myname\Desktop\maindir\*.full.jpg" "C:\Users\myname\Desktop\maindir\*.jpg"
However, I cannot get it to work.
The .bat is located in the 'maindir' directory and ran from the terminal (cmd).
Here's an example scenario that maps closely to mine:
Existing Files:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
Example Output Wanted:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture1.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.jpg
I'd appreciate any help towards this as I haven't been able to do it yet. I will run across a directory structure whereby the top level directory will contain 15+ directories and each containing 20+ directories with 100+ files in each lowest directory.
Thanks.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\Users\myname\Desktop\maindir"
FOR /r "%sourcedir%" %%a IN (*.full.jpg) DO (
FOR %%b IN ("%%~dpna") DO ECHO(COPY "%%a" "%%~dpnb.jpg"
)
GOTO :EOF
The inner for examines the drive-path-name only of the complete filename in %%a (ie. it drops the .jpg) and delivers the drive-path-name of that name (ie. drops the .full) to which you add .jpg and job done.
You would need to change the setting of sourcedir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files.

Windows batch command to move all folders in a directory with exceptions

I am trying to write a Windows Batch file that will allow me to move all directories within a given source directory into a target directory that exists within that source directory.
Obviously my move command with need to only apply to directories and also exclude the target directory from being processed.
Is this possible with a Windows batch command?
Robocopy (present in recent versions of windows or downloadable from the WRK) can do this, just use the /xd switch to exclude the target directory from the copy;
robocopy c:\source\ c:\source\target\ *.* /E /XD c:\source\target\ /move
FOR /d %%i IN (*) DO IF NOT "%%i"=="target" move "%%i" target
That won't work - you'll get an error telling you the target directory is inside the source directory or so, even if you explicitly exclude the target directory. What you can do is move the directories to a temporary location which is not under the source, and then move them into the target.
BTW, using the move command won't let you specify folders to exclude. For that you can use xcopy, but note that it will copy the folders, as opposed to move them. If that matters, you can delete whatever you want afterwards, just make sure you don't delete the target dir, which is in the source dir...
Using robocopy included with Windows 7, I found the /XD option did not prevent the source folder from also being moved.
Solution:
SET MoveDirSource=\\Server\Folder
SET MoveDirDestination=Z:\Folder
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"
This loops through the top level folders and runs robocopy for each.
NB: Robocopy mentioned above using the /move flag will copy the files and then delete them from the source folder rather than moving the files. This may be critical if moving large numbers of files from one location to another on the same disk (because move is virtually instantaneous, while copying is a much slower operation)
On windows batch:
FOR /d %%i IN (MySourceDirectory\*) DO move "%%i" MyTargetDirectory\%%~ni
The above command moves all directories found in MySourceDirectory (/d) to MyTargetDirectory using the original directory name
(~ni) Robocopy's move first does a copy, then delete, so it is slower.
This works for me:
move c:\fromDir\*.* c:\toDir\

Resources