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
Related
I want to be able to use the "Send to" function (When right clicking a file) with this batch file.
It needs to create a folder, with the name of the file, for each of the selected files, in the same directory as the file itself. (No moving of the file needed)
The following code has helped, but this creates folders for all files in the directory and places it in the directory of the batch file.
#echo off
pushd %~dp0
for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
)
)
popd
I believe using the following function will be needed for the directory of the files but not sure about how to call it.
%CD%
I am rather new to batch files so any extra explanation would be helpful, but not necessary.
Even if it can only run on one file at a time, that will be great since it needs to be no a chosen file basis.
Here goes to learning on the go and thanks for any help!
This should do what you are looking for. Give this script a try in your Send To menu:
#ECHO OFF
SETLOCAL
:ProcessFile
REM Check if there are any files to process.
IF "%~1"=="" GOTO :EOF
REM Process the current file.
SET NewDir="%~dpn1\"
REM Create the directory if it doesn't already exist.
IF NOT EXIST %NewDir% MKDIR %NewDir%
REM Move to the next selected file.
SHIFT /1
REM Recurse.
GOTO ProcessFile
ENDLOCAL
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%"
I have a parent folder with lots of folders (movies) underneath. Each folder has 1 file (the actual movie).
I would like some advice on a batch script that I can run to rename the folders to the file (movie) within excluding the extension (.avi)
e.g.
BEFORE
Parent (folder)
Folder 1
Movie 1.avi
Folder 2
Movie 2.avi
AFTER
Parent (folder)
Movie 1
Movie 1.avi
Movie 2
Movie 2.avi
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*" '
) DO (
FOR %%b IN ("%sourcedir%\%%a\*") DO IF /i "%%~na" NEQ "%%~nb" ECHO(REN "%sourcedir%\%%a" "%%~nb"
)
GOTO :EOF
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
You would need to set your directory as sourcedir. I set my testing directory.
The following batch file does the job:
#echo off
if exist "%TEMP%\RenameFolders.bat" del "%TEMP%\RenameFolders.bat" >nul
cd /D "Complete\path\to\parent\folder"
for /R "Complete\path\to\parent\folder" %%I in (*.avi) do echo ren "%%~dpI" "%%~nI">>"%TEMP%\RenameFolders.bat"
if exist "%TEMP%\RenameFolders.bat" (
call "%TEMP%\RenameFolders.bat"
del "%TEMP%\RenameFolders.bat" >nul
)
"Complete\path\to\parent\folder" must be modified twice in the batch file by real parent folder path.
The main job is done by command FOR. Run in a command prompt window for /? to get information about option
/R ... recursive folder/file scan, and
%%~dpI ... drive and path of file, and
%%~nI ... name of file without path and file extension.
For each *.avi file the command echo is executed to create the rename command used later to rename the folder containing the *.avi file to the name of the *.avi file.
It is not possible to do the folder rename directly within the FOR loop as it is not possible to rename a folder while it is the current working directory for a running process. Each folder is the current working directory for the batch process while FOR is running.
The command CD in third line just makes sure that the working directory is set to parent folder of all the subfolders to rename to avoid that renaming a folder fails because the batch file is started from one of the subfolders.
The second line just makes sure there is no file RenameFolders.bat in the folder for termporary files for example when terminating this batch file once by click on button X while command FOR is currently running.
After command FOR processed recursively all *.avi files and a line with command ren was appended to file RenameFolders.bat for each file, this created batch file is called which renames now the folders.
Last the batch file temporarily created is deleted as not needed anymore.
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
I am trying to copy a set of files and folders recursively from a directory. The set of files are listed in a text file. Here is my script:
set src_folder=C:\Users\mmhuqx\HW
set dst_folder=C:\Unix2Windows
set filelist=C:\Unix2Windows\filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
for /f "delims=" %%i in (%filelist%) do (
xcopy /s /y "%src_folder%\%%i" "%dst_folder%"
)
But When I run the script it copies the entire Directory contents including the files and folders not listed in the text file.
How can I make it work using XCOPY, or is it not possible?
Your filelist-tm.txt seems to be wrong. It contains for example an entry TOP_LEVEL\MODEM_DEBUG. So that directory is copied completely with all its files.
It seems that you only want TOP_LEVEL\MODEM_DEBUG\AENEAS_FW.fls to be copied. So your filelist-tm.txt should only contain this entry.