I have a bunch of folders (and subfolders) in the structure of...
Test/Student001/ABC,
Test/Student001/DEF,
Test/Student002/ABC,
Test/Student002/DEF, etc...
What I then need to do is relocate those folders (and their subfolders and the files within) to another location so that it is like...
Test/Class01/ArronAmos(Student001)/ABC
Test/Class01/ArronAmos(Student001)/DEF
Test/Class02/BrettBesty(Student002)/ABC
Test/Class02/BrettBesty(Student002)/DEF
I have text files with all the folders (and subfolders) original and new names (saved like so)..
studentdata.txt
A (studentcode), B (studentnewname), C (Class)
Student001, ArronAmos (Student001), Class01
Student002, BrettBesty (Student002), Class02
Is there a way to get a batch to basically go like this (using A,B and C from text file above - preferably the one txt file if possible)...
md 'C' ::which will skip past if folder exists
rename folder 'A' to 'B' ::only rename of root folder and leave subfolders intact
move folder 'B' to 'C' ::move new named folder (B) and all subfolders and contents to new root folder (C)
The creation of the new directories and subfolders (for new and future students) goes like this and works great (except if there was a way to call a 2nd text file for the subfolder creation rather than coded in that would be awesome - but no biggy I guess)...
The Creation Batch
cd /d %~dp0 pause
FOR /F "delims=~" %%f in (dirlist.txt) DO md "%%f"
:: This code creates directories referenced from a .txt file: - :: FOR /F "delims=~" %%f in (dirlist.txt) DO MD "%%f"
pause
FOR /D %%x in (*) do mkdir "%%x\Individual Behaviour Plans" "%%x\Individual Learning Plans" "%%x\Student Reports" "%%x\Student Support Group Meetings"
:: This code creates a new dir within every folder in batch location: - :: FOR /D %%x in (*) do mkdir "%%x\value"
pause
and this is the Rename Batch that I received off one of the other techs and don't quite understand it or know to modify it to make it work..
*rename_users.bat** :: Script to Rename folders - prefixing from a text file
setlocal ENABLEDELAYEDEXPANSION
Set Rootfolder=Test Set Names=names.txt
:: Goto Root Folder cd /d %~dp0
:: Start Line Counter Set LineCount=0
:: For Every folder in the directory
For /d /r %%g in (*) DO (
:: Increment the line counter by 1 (see the use of "!" >instead of "%" due to delayed expansion)
Set /a LineCount=!Linecount!+1
:: Call the Rename Folder sub - passing through the >variables of folder name and line counter
Call:RenameFolder %%g !LineCount!)
:RenameFolder :: For all of the tokens in the findstr on the names file for /f "Tokens=1* delims=:" %%a in ('findstr /n "^" "%Names%"') DO ( :: If the line counter matches the line number If %%a==%~2 ( :: Rename the Folder Move "%~1" "%~1 %%b") ) ::Return to the Primary
Goto:EOF
Set Rootfolder= Set Names= Set linecount= Set Drive=
Endlocal
The trick is we can't just use the create directories (and subdirectories) batch file, as there are some folders existing in the original format that have data in them that we need sitting in the new structures subfolders... and moving them manually would be an option... if there were not 900+ student folders to do this too...
I hope makes some form of sense... Thanks guys!
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Enter into working directory
cd C:/Test
rem Process the file with original and new names
for /F "skip=1 tokens=1-3 delims=," %%a in (studentData.txt) do (
rem Get studentNewName end eliminate spaces
set "studentNewName=%%b"
set "studentNewName=!studentNewName: =!"
rem If the new folder not exist, create it
if not exist "%%c/!studentNewName!" md "%%c/!studentNewName!"
rem Move files from old folder
move "%%a/*.*" "%%c/!studentNewName!"
rem And delete old empty folder
rd "%%a"
)
Antonio
Related
There is really 2 parts to this question:
I have a folder full of files that I need to temporarily move to a
new location.
However I will later need to move the files back to their original
location(s).
I have a single folder, full of files and folders that looks like this:
C:\VIDEO\My Video 1\My Video 1.mkv
C:\VIDEO\MyVideo2\MyVideo2.mkv
C:\VIDEO\My.Video.3\My.Video.3.mkv
I need to:
1. Recursively find all *.mkv files within C:\VIDEO folder
2. Output a list of the existing Dir structure/file and folder names/paths to a .txt file
3. Then Move all *.mkv files from C:\VIDEO to another folder C:\Temp
(I do not want to retain the original folder structure during this move)
At a later time I then need to:
4. Search for all *.mkv files in C:\Temp
5. Use the .txt file to help move each *.mkv file back into their original location
I guess this will probably require 2 separate batch files.
Here is my current progress:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Setup
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
:: Output Items To Txt File
for /f "delims=\" %%A in ('dir "%SourcePath%"\*.mkv') DO echo "%SourcePath%">>"%DestPath%"\output.txt
:: Move Matching Items
for /f "tokens=*" %%a IN ('dir "%SourcePath%"\*.mkv') DO move /y "%SourcePath%\%%a" "%DestPath%"
Can somebody please help?
You want to move a tree to a flat destination (knowing you later want to move it back and have to recreate the tree)? Why on earth would one do that...
But ok:
#echo off
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
echo #echo off > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
ECHO move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
echo done. To move them back, execute MoveBack.bat
Instead of logging the moved files to a text file and later iterate over that file, it's easier to just build a "restore" script.
For a description of the %%~ modifiers, read the output of for /?
NOTE: I disarmed the move command for security reasons. If you are sure it works as intended, remove the ECHO.
Note: it's possible to have duplicate file names in a folder tree. This script does not account for that (saying: you may lose data in this case)
The final product:
#echo on
set "SourcePath=H:\FIXED"
set "DestPath=H:\Temp"
echo #echo on > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
#echo done. To move them back, execute MoveBack.bat
is now working.
I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied
I am struggling to create a batch file that I can use in any folder, which will copy the current folder and all its contents to another location. The objective is to allow me to make very quick backups of any current folder by running the batch file. Batch files are new to me, but programming isn't. Here is my code.
#echo off
title Copy This Directory
:: get current directory
set startdir=%cd%
echo.startdir = %startdir%
:: copy the test folder
xcopy "%startdir%" "F:\Temp Backup\" /s /y
pause
This copies the files and folders that are inside the source folder to the new destination, but I want to copy the folder itself together with its contents. ie if the batch file is in a folder called "FromFolder", I want "FromFolder" to appear as a folder in "F:\Temp Backup\" and all its contents in "F:\Temp Backup\FromFolder". I've found lots of info about copying files, but nothing about copying a single directory. I can copy a single file, but when I use the same code and change the file name to a folder name, it copies the folder's contents and not the folder itself. Could someone let me know what I have missed please.
#echo off
rem set destination dir
set destDir=C:\tmp\fizz bang
rem get the current dir
set currDir=%cd%
rem set current dir to parent
pushd ..
rem get the parent dir path
set parDir=%cd%
rem replace the parent path part of the dir with the destination
rem see also: https://stackoverflow.com/a/5821366
rem also note the trailing backslash to tell xcopy it's a directory
CALL set destPath=%%currDir:%parDir%=%destDir%%%\
rem copy
xcopy /e "%currDir%" "%destPath%"
rem go back where we started
popd
pause
I decided to go with creating a new folder at the destination and as a second step copying the contents to it. Here's what I did.
#setlocal enableextensions enabledelayedexpansion
#echo off
title Copy This Directory
:: get current directory
#echo off
set startdir=%cd%
set temp=%startdir%
set folder=
:loop
if not "x%temp:~-1%"=="x\" (
set folder=!temp:~-1!!folder!
set temp=!temp:~0,-1!
goto :loop
)
echo.startdir = %startdir%
echo.folder = %folder%
:: Create new folder if needed
md "F:\Temp Backup\%folder%"
:: copy the contents
xcopy "%startdir%\*.*" "F:\Temp Backup\%folder%\" /s /y
pause
endlocal && set folder=%folder%
If you are running the batch file from the directory you want to backup you can literally do this in a handful of lines of code.
#echo off
title Copy This Directory
for %%G in (".") do set folder=%%~nxG
md "F:\Temp Backup\%folder%" 2>nul
xcopy "*.*" "F:\Temp Backup\%folder%\" /s /y
I have a folder where lots of .tif files drop into.
They all start with MW_ or SW_ and later also NSSW. So I need to be able to expand from the first two later to NSSW.
I have a batch file already that first moves the files into the folder MW or SW based on the first 2 characters of the file name. Here is my current batch file and it works just fine. But I think I need a second batch file or addition to this to do the below 1 & 2 steps. Please see below, after this code.
REM Sort by First name.
REM This script creates a folder for either the full file name,
REM or if it contains an underscore, the part before the underscore.
REM TODO - Don't copy over existing files.
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif
#echo off
REM Needed because you are working with variables that are immediately called
setlocal enabledelayedexpansion
REM Start of the loop to get all files with a psd or jpg Extension
for %%A in (*.tif *.jpg *.pdf) do (
echo file found %%A
REM Grabs only the file name
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
REM Grabs only the extension
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
REM Moves the file to the folder
echo Moving file %%A to folder !folname!
REM if not exist "%%D\%%A" move "%%A" "!folname!"
if not exist "%%D\%%A" copy "%%A" "!folname!"
REM add the date DDMMYYYY to the end of each file. Name can be 80 characters long.
rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif"
)
echo Finished
pause
So here is what I need I guess. A second or 3rd batch file to do the below. I hope, someone can help.
Note: Please keep in mind that if a file exists it renames the copy when moving with xxxx(1).tif, xxx(2).tif, etc. on the end of file name.
Move files now listed in MW folder into new or existing subfolders based on the name of the file from the 4th to 10th character of the file name or from the 4th character to the second or next "_".
Move file in subfolder of the named folder to a sub date named folder based on last 8 characters of file name.
What I need is to then move the files from the MW folder into new or existing subfolders based on the last 7 characters of the file name "the date section".
For example we start with MW files
Files coming in to folder C:\temp\Test_Moving_Files\
MW_VRL5VF10000_6542234_01052016.TIF
MW_Flybuys_677888_01052016.TIF
MW_VRL5VF10000_333443_02052016.TIF
MW_Flybuys_555555_02052016.TIF
MW_goodguys_534535_02052016.TIF
MW_goodguys_222222_02052016.TIF
MW_Flybuys_123443_03052016.TIF
MW_Flybuys_3545555_03052016.TIF
MW_goodguys_444444_03052016.TIF
MW_goodguys_888888_03052016.TIF
Output to subfolders should be sorted to subfolders like below:
MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF
MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF
MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF
It looks the task can be achieved by modifying
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
to
REM Using the file name it separates it into 4 parts using "_" as a delimiter.
for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G"
echo Folder name !folname!
REM Checks for the existence of the folder path. If the folder
REM path does not exist, it creates the complete folder structure.
if not exist "!folname!\*" (
echo Folder !folname! does not exist, creating ...
md "!folname!"
) else (
echo Folder !folname! exists.
)
Instead of using just first substring of underscore delimited file name, the code above uses now first, second and fourth substring. I did not test it!
Command MD can create also multiple folders at once and therefore variable folname can be also a folder path string. Well, I suggest to rename the variable folname in your batch code for example to FolderPath.
Of course you must use folname or better FolderPath also in rest of batch script instead of %%D.
Since I cannot add a comment, I am asking a related question.
The original posting found here works quite well.
Is there a way to use this with a list of file names? I've seen where a file list can be passed to the ROBOCOPY command but I have been unable to get it to work.
Taking a step back, I have a series of folders and there are specific files inside of them that I want to copy out to a single folder. I have a text file which lists the names of these files.
I am looking for a batch routine that will look for each of the files in the text file in each of the folders and then copy the files out to a new folder.
Thank you!
Test this - the file.txt has a filename on each line.
It doesn't handle filename conflicts.
#echo off
cd /d "c:\base\folder"
for /f "usebackq delims=" %%a in ("file.txt") do (
for /f "delims=" %%b in ('dir "%%a" /b /s /a-d ') do copy "%%b" "d:\target\folder"
)
I recently had to tackle this problem, and many files that I wanted to move to from the hierarchy to a single folder had the same name as each other, and I wanted to still flatten the hierarchy without them to being over-written.
What I did was write a script that moves the file, but renames it with the old hierarchy path in the name
for example:
source files:
C:\files\somefiles\file.txt
C:\files\otherfiles\file.txt
destination is C:\newdir\
files are created as
C:\newdir\somefiles-file.txt
C:\newdir\otherfiles-file.txt
here is the code, batch file 1 goes thru the files, batch file 2 renames and moves them (could also copy instead, if you want to preserve the source:
#echo off
for /r %%f in (*.*pr) do #renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"
renameandmovefilespart2.bat
#echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem #echo shortened: %newname%
rem #echo source path: %origPathOnly% newPath: %startingDir%
rem #echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"