Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

CMD based folder copying automation


  • Please log in to reply
5 replies to this topic

#1 mysticduck

mysticduck

  • Members
  • 35 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Southern Oregon
  • Local time:07:02 AM

Posted 14 February 2013 - 01:19 PM

Wow that is a monster of a topic title.  Anyways.

 

I run a computer shop and do a lot of drive backups, usually through an e-sata connection.  I'm trying to automate some of this backup process to save my time.  Something that I would either just give a drive letter and it would parse folders and grab what I want or I give it a profile folder and it grabs the folders I want within the profile.  I deal with XP through 8, so I don't know if I would need multiple scripts or if I could make one do it.  I sometimes deal with failing drives, have had good luck with xcopy and the /c flag, hoping to use xcopy or robocopy.  My scripting knowledge is basic, I can hack together .BAT files when needed.  I don't know if CMD can handle that kind of variables, I am open to other solutions.  To give some examples of what I grab, on XP I copy Desktop, My Docs and Favs from each user profile folder, and also check the All Users folder for any Intuit or Kodak stuff stored there.  On Vista and up I grab all the stuff normally found in the user folder.  I am trying to avoid copying the entire drive or profile/user folder, dont need local settings or appdata or anything.  The machine running these scripts is 7 Home 64 at the moment.  I am willing to hack this together myself if someone could point me in the right direction, but if someone is REALLY bored and wants to script it that would pretty much rock.


Stultorum infinitus est numerus

BC AdBot (Login to Remove)

 


#2 tellus

tellus

  • Members
  • 24 posts
  • OFFLINE
  •  
  • Local time:09:02 AM

Posted 15 February 2013 - 10:29 AM

Hi mysticduck!

 

I recently started writing a little .bat program that may meet your needs.  It's unfinished.  I'll warn you that I also have a rudimentary knowledge of programming and learn more on a need-to-know basis.  Happily, most of my scripts run on win7 and previous without elevated permissions.  Like you, I also use robocopy for copy/move in almost all of my scripts...

 

Since 1) the program is unfinished and 2) it's a bad idea to post code on forums, PM me it to you if you'd like to see it.  Anyone reading this topic in the future should know that if code is posted here they use it at their own risk :-)

 

Here are some of the things I hope the program will do reliably when it's done:

  • Program automatically does these things
    • dynamically detect windows version to determine storage locations
    • place backup sources into a logical hierarchical folder structure, such as %dest_drive%/backup/%computername%/%username%/%source_name_or_drive%
    • log all backup/archive operations in a logical place
      • such as %dest_drive%/backup/%computername%_%username%.log
  • Using a menu, or on a schedule
    • backup common areas of the computer such as %userprofile%
      • include Outlook and Firefox (etc.) profiles and other things that are buried inside large folders while ignoring the rest of the stuff
    • run custom backup scripts
    • backup entire drives
    • use erunt (or other program) to backup the registry
  • Using a menu only
    • archive (erase original) data to logical hierarchical folder structure
      • such as %dest_drive%/archive/%computername%/%username%/%source_name_or_drive%-%YYYYMMDD%
  • Using drag-and-drop (onto the batch program)
    • create/append custom backup scripts

Also, the file contains a 'common' menu and other settings that can be copied and pasted into different batch files, which I find makes editing them a ton easier.  I can't remember what stage that menu is at in this script or how much of it works, but the nice thing is that as the 'common' menu evolves you can copy and replace the code areas in your other scripts and their menus get updated, too (of course, I could probably just make a common menu file and make all the script menus call it if common.bat was in the development folder... hmmn...)



#3 hispaladin

hispaladin

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:09:02 AM

Posted 08 April 2013 - 08:00 AM

I have a similar request and as well I think this will be fairly simple to do (I think).  I want to make a batch file that will copy a file to a new folder but i need to append the name to include the date so that we can have a record of when the backup was made.  So basically we start out with a file named grntkpow.fil and we need to make a copy of it to keep as a backup but we need it named something like grntkpow040813.fil.  It needs to work like this because we have several files being dumped into the same folder that have slightly different names but are the same file type and are all made daily.  Any ideas on how to do this? 

 

Edit: Also I will not be the one using the .bat file so it will need to be rather easy to do.  The employee using the file does not have a lot of skill when it comes to computer use on this level.  They are good at using the programs they use but not much more.


Edited by hispaladin, 08 April 2013 - 08:02 AM.


#4 mysticduck

mysticduck
  • Topic Starter

  • Members
  • 35 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Southern Oregon
  • Local time:07:02 AM

Posted 13 April 2013 - 04:58 PM

So I actually just hacked together something that works for me.  It is ugly, it could definitely be refined, but it works.  Posting the code below for perusal, with the following disclaimer:  DO NOT USE THIS CODE UNLESS YOU ARE OK BREAKING EVERYTHING IN YOUR COMPUTER.  Ahem.  In other words, this was for my own personal use and works great for me, YMMV.

:date
set _date=%date:~-4%-%date:~4,2%
if exist e:\%_date% (goto define) else (mkdir e:\%_date%)
:define
set /p _0=Client name
set /p _1=User folder
if defined _1 (set /p _2=User folder) else (goto copy)
if defined _2 (set /p _3=User folder) else (goto copy)
if defined _3 (set /p _4=User folder) else (goto copy)
if defined _4 (set /p _5=User folder) else (goto copy)
:copy
mkdir e:\%_date%\"%_0%"
cd /d "%_1%"
for %%A in (%_1%) do set lastdir=%%~nA
for %%I in (%_1%) do set _dir=%%~dI 
mkdir e:\%_date%\"%_0%"\"%lastdir%"
cd /d e:\%_date%\"%_0%"\"%lastdir%"
robocopy %_1% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
set _back=%_1%
call:back
if defined _2 (cd /d "%_2%") else (goto:eof)
for %%A in (%_2%) do set lastdir=%%~nA
mkdir e:\%_date%\"%_0%"\"%lastdir%"
cd /d e:\%_date%\"%_0%"\"%lastdir%"
robocopy %_2% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
set _back=%_2%
call:back
if defined _3 (cd /d "%_3%") else (goto:eof)
for %%A in (%_3%) do set lastdir=%%~nA
mkdir e:\%_date%\"%_0%"\"%lastdir%"
cd /d e:\%_date%\"%_0%"\"%lastdir%"
robocopy %_3% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
set _back=%_3%
call:back
if defined _4 (cd /d "%_4%") else (goto:eof)
for %%A in (%_4%) do set lastdir=%%~nA
mkdir e:\%_date%\"%_0%"\"%lastdir%"
cd /d e:\%_date%\"%_0%"\"%lastdir%"
robocopy %_4% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
set _back=%_4%
call:back
if defined _5 (cd /d "%_5%") else (goto:eof)
for %%A in (%_5%) do set lastdir=%%~nA
mkdir e:\%_date%\"%_0%"\"%lastdir%"
cd /d e:\%_date%\"%_0%"\"%lastdir%"
robocopy %_5% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
set _back=%_5%
call:back
goto:eof
:back
set _ff=
set _bm=
set _tb=
set _ol=
set _wm=
set _wlm=
set _oe=
set _ad=
if exist %_back%\appdata\roaming\mozilla\firefox set _ff=%_back%\appdata\roaming\mozilla\firefox
if exist %_back%\"application data\mozilla\firefox" set _ff=%_back%\"application data\mozilla\firefox"
if defined _ff (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\FF
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\FF
	)
if defined _ff set _bm=%_ff%\"profiles\*.default\bookmarkbackups\"
if defined _bm cd /d %_bm%	
if defined _bm robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist %_back%\"appdata\roaming\thunderbird" set _tb=%_back%\"appdata\roaming\thunderbird\profiles"
if exist %_back%\"application data\thunderbird" set _tb=%_back%\"application data\thunderbird\profiles"
if defined _tb (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\TB
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\TB
	)
if defined _tb cd /d %_tb%
if defined _tb robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist %_back%\"appdata\local\microsoft\outlook" set _ol=%_back%\"appdata\local\microsoft\outlook"
if defined _ol (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\Outlook
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\Outlook
	)
if defined _ol cd /d %_ol%
if defined _ol robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist "%_back%\appdata\local\microsoft\windows mail" set _wm="%_back%\appdata\local\microsoft\windows mail"
if defined _wm (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\WM
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\WM
	)
if defined _wm cd /d %_wm%
if defined _wm robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist "%_back%\appdata\local\microsoft\windows live mail" set _wlm="%_back%\appdata\local\microsoft\windows live mail"
if defined _wlm (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\WLM
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\WLM
	)
if defined _wlm cd /d %_wlm%
if defined _wlm robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist %_back%\"local settings\application data\identities" set _oe=%_back%\"local settings\application data\identities\{*\microsoft\outlook express"
if defined _oe (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\OE
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\OE
	)
if defined _oe cd /d %_oe%
if defined _oe robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
if exist %_back%\"application data\microsoft\address book" set _ad=%_back%\"application data\microsoft\address book"
if defined _ad (
	mkdir e:\%_date%\"%_0%"\"%lastdir%"\AD
	cd /d e:\%_date%\"%_0%"\"%lastdir%"\AD
	)
if defined _ad cd /d %_ad%
if defined _ad robocopy %_dir% e: /s /z /r:3 /w:5 /xa:sh /xd AppData Cookies "Local Settings" "Application Data"
goto:eof
Pause

So using this, I give it the client name, dragndrop the user folder I want backed up into the CMD window, hit enter so it sets it, when it notices an empty set it starts backing up, also checks each user folder for FF, OE and other data in wierd places.  Feel free to ask questions.


Stultorum infinitus est numerus

#5 mysticduck

mysticduck
  • Topic Starter

  • Members
  • 35 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Southern Oregon
  • Local time:07:02 AM

Posted 13 April 2013 - 05:09 PM

I have a similar request and as well I think this will be fairly simple to do (I think).  I want to make a batch file that will copy a file to a new folder but i need to append the name to include the date so that we can have a record of when the backup was made.  So basically we start out with a file named grntkpow.fil and we need to make a copy of it to keep as a backup but we need it named something like grntkpow040813.fil.  It needs to work like this because we have several files being dumped into the same folder that have slightly different names but are the same file type and are all made daily.  Any ideas on how to do this? 

 

Edit: Also I will not be the one using the .bat file so it will need to be rather easy to do.  The employee using the file does not have a lot of skill when it comes to computer use on this level.  They are good at using the programs they use but not much more.

A variation on what I use.

mkdir %date:~4,2%-%date:~7,2%-%date:~10,4%

The above will create a folder in the current directory with the MM-DD-YYYY format.  You could add a name like backup or whatever you wanted to have in front of the date code, just make sure if you are using spaces to enclose everything after mkdir in quotes.  Example:

mkdir "Backup %date:~4,2%-%date:~7,2%-%date:~10,4%"

Forgot to mention this, if you are calling the date code a lot in a batch file, just make a variable out of it.

set _date=%date:~4,2%-%date:~7,2%-%date:~10,4%
mkdir %_date%

The above will get you the same result as the first code example.


Edited by mysticduck, 13 April 2013 - 05:21 PM.

Stultorum infinitus est numerus

#6 hispaladin

hispaladin

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:09:02 AM

Posted 17 April 2013 - 08:01 AM

Thanks for the feedback, I got a batch file together that does what I need.  I am fairly sure it does the same thing you are doing in your suggestions just in a different way.  Here is what I have

 

 

copy "p:\grain tickets\grntkpow.fil" "p:\grain tickets\backups\grntkpow.fil"
copy "p:\grain tickets\padreggn.fil" "p:\grain tickets\backups\padreggn.fil"
copy "p:\grain tickets\grntkden.fil" "p:\grain tickets\backups\grntkden.fil"
copy "p:\grain tickets\grntktop.fil" "p:\grain tickets\backups\grntktop.fil"
copy "p:\grain tickets\grntktop.fil" "p:\grain tickets\backups\grntkwil.fil"
//pause
for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\grntkden.fil" grntkden%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\grntkden*.filbk" grntkden????????-%%A%%B.fil
//pause
for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\grntkpow.fil" grntkpow%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\grntkpow*.filbk" grntkpow????????-%%A%%B.fil
//pause
for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\padreggn.fil" padreggn%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\padreggn*.filbk" padreggn????????-%%A%%B.fil
//pause
for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\grntktop.fil" grntktop%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\grntktop*.filbk" grntktop????????-%%A%%B.fil
//pause
for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\grntkwil.fil" grntkwil%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\grntkwil*.filbk" grntkwil????????-%%A%%B.fil
//pause
 
I put the pause commands in for troubleshooting and to check to make sure everything was working at each step.  This puts both a date stamp on the files as well as a time stamp because sometimes we need to make a backup of the files more than once a day and if I don't have the time stamp then it will overwrite the first file of the day.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users