Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch file for Autobackup


13 Jul 2011   #1

XP Professional, 7 Professional
 
 
Batch file for Autobackup

I have a question regarding a batch file I've been working on to automate some of my daily tasks. I am copying a list of specific network database files from the server onto an external HD. Up until now I've just been manually Ctrl-clicking and dragging them over as a group. Not too difficult, but I thought it might be clever to handle it with an automated task.

I've figured out how to COPY the files from one location to another, but I'm running into problems when I try and create a dated folder to drop them into. I understand that the "mkdir" command will make a file, and I was reading about how to have that file's name be "ddmmyy". My question is how do I write one code which creates the dated folder (preferably with dashes mm-dd-yy) and then copies the files into that newly created folder.

Right now, my code has a static location to copy the files to. I need it to adapt to whatever the file name for that day is. I hope that makes sense.

My System SpecsSystem Spec
.

13 Jul 2011   #2

Microsoft Community Contributor Award Recipient

Windows 7 x64
 
 

Code:
@ECHO OFF
 FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%J%%K%%L 
ECHO The value is "%mydate%"
will create a value that is %mydate% and will always contain the current date (the format you want but no dashes)
I don't know if this can be used in conjuction with the mkdir command in naming the folder though.

mkdir just doesn't have any switches or variables associated with it.
All it does is make directories.
This is actually one command linux has win beat on
My System SpecsSystem Spec
13 Jul 2011   #3

XP Professional, 7 Professional
 
 
Huzzah!!

Even though your code wasn't exactly what I needed, it contained the missing puzzle piece. A light bulb went off in my head, and suddenly the world's simplest workaround was right in front of me. I use the "md" function to create a folder called "temp" then copy all the files to the static folder, then rename the folder to the current date using a function quite similar to yours. Here is the finished product, I've replaced all of the user specific information with descriptions in red parenthesis:

@echo off
md "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename1.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename2.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename3.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename4.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename5.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename6.type)" "(drive):\(path)\temp"
COPY "(drive):\(path)\(filename7.type)" "(drive):\(path)\temp"
for /f "tokens=1-4 delims=/ " %%d in ("%date%") do rename "(drive):\(path)\temp" %%e-%%f-%%g

Works like a charm, saves me time, and makes me look like a computer whiz to my boss. Thanks to Maguscreed and Windows 7 forums as always. Every time I've posted here I've had nothing but good experiences.
My System SpecsSystem Spec
.


13 Jul 2011   #4

Microsoft Community Contributor Award Recipient

Windows 7 x64
 
 

Nice, I figured that would push you in the right direction.
Creative solution I was sitting there trying to figure out how to drop %mydate% info into mkdir.
...don't think it can be done that way at all though.
My System SpecsSystem Spec
19 Jul 2011   #5

Microsoft Community Contributor Award Recipient

Windows 7 x64
 
 

You know I just had another thought about this.
You could set up a autoplay on the usb drive so it automatically copies whenever you plug in the back device.
My System SpecsSystem Spec
19 Jul 2011   #6

XP Professional, 7 Professional
 
 

True, I never thought of that, I'll try it today.

**update**
Didn't have much time to mess with this, but my initial attempts were unsuccessful. I created an autorun.inf file and placed it in the external drive. I thought the code would be a piece of cake, ([autorun] open=*), but no matter how I try and format the text, I get no result. The autorun definitely boots when I turn on the external, but the batch file doesn't run. I tried using shellexecute instead of open, but to no avail.

I'm guessing it's because the .bat isn't an .exe. So maybe if I run a modified setup.exe, or some other intermediary program I'll have some more luck. Let me know what your take is, I'd be interested to know all my options before I troubleshoot further.

Is it wrong that I find this terribly interesting? LoL
My System SpecsSystem Spec
20 Jul 2011   #7

XP Professional, 7 Professional
 
 

Hmmm.. a bunch of threads online are implying that autorun functions have been disabled for external drives. Not sure if I fully believe that or not. Can anyone confirm or deny?
My System SpecsSystem Spec
22 Jul 2011   #8

XP Professional, 7 Professional
 
 

I don't seem to be able to run the batch file with an autoplay function, everywhere I look, people say that the code is super simple, and should look something like this:

[autorun]
open=filename.bat

or

[autorun]
open=filepath\filename.bat

I've also tried substituting "shell" and "shellexecute" for open. It's weird, because when I type the file path into "run" it works, and I thought the shellexecute function would work the same. When the autorun.inf file activiates, it pops up a flashlight and attempts to run something, but the batch never runs, and the files aren't copied.

This isn't a huge deal, because I can just keep a copy of the .bat on my desktop and double click it once I've plugged the drive in. But now I'm seriously curious as to what steps I've missed here. Any help would be greatly appreciated.
My System SpecsSystem Spec
22 Jul 2011   #9

Microsoft Community Contributor Award Recipient

Windows 7 x64
 
 

try this way

Code:
[AutoRun]
OPEN=backup.bat
ICON=MYAPP.EXE
ACTION=Run Backup Routine
just put any icon file on the usb root and change target to suit.
I think that's what it's looking for.
My System SpecsSystem Spec
22 Jul 2011   #10

XP Professional, 7 Professional
 
 
Still not working

So, I've made some progress, but I'm still stumped. I'm starting to think that it has something to do with the drive itself. It's a Verbatim 500GB external desktop drive. It's formatted to NTSB. When I originally bought it, I had set it up to run as a multiboot drive, in order to run .ISO programs.

In order to bypass the potential conflict between autorun and .bat files, I even tried creating a self extracting .exe, using IExpress.exe. When I double click the .exe it runs the .bat with no issues. But a simple autorun function to run the .exe gives no significant response.

I tried your format, with both the .bat file, and the .exe file. It won't open with either. The autorun is functioning, because the icon I used replaced the default one. Also, it's searching the drive, (theoretically trying to find the file to open), but it never actually locates it. I'm checking the root path now, but to no avail. I feel like I'm missing a simple detail, but I've tried so many combinations, that it seems unlikely.
My System SpecsSystem Spec
Reply

 Batch file for Autobackup




Thread Tools




Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 04:35 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33