Batch file or script to move files up X levels - windows

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.

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

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.

how to copy all files in subfolders to the parent folder

I want to copy all files within subfolders to the parent folder. But want to create a new folder in it by the name "copied" in whichever folder i have copied my batch file. I tried this command.
FOR /R %%# IN (*.*) DO XCOPY "%%#"
It does the job but it keeps asking to rewrite files. Also wasn't sure how to create the folder name in which i have copied my batch file into.

Need batch program that moves files without copying then deleting them

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"

Move Folder with parent path 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

Resources