Solved

Avoiding Archive Attribute In Batch Files

Posted on 2009-07-06
16
436 Views
Last Modified: 2012-05-07
I use this batch file for quick data backing up in XP SR3

DEL C:\BACKUP\DATAONLY\*.* /Q /F /S
RMDIR C:\BACKUP\DATAONLY\DATA\ /Q /S

XCOPY W:\DATA\*.* C:\BACKUP\DATAONLY\DATA /I /S /E /K /R /H
==================================================
It works fine except that all the copied file have the archive attribue set to on.
I know it not really that important but I'd prefer an exact copy including attributes
Can it be done in XP DOS Batch files?
==================================================
ATTRIB -A C:\BACKUP\DATAONLY\DATA\*.* /s

This clears the archive attributes except for hidden files.
It's no good anyway as I'd still like an exact copy.
0
Comment
Question by:Eirman
  • 7
  • 4
  • 3
  • +1
16 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 200 total points
ID: 24786033
Consider using ROBOCOPY instead.  I believe the /MIR (mirror) function preserves attributes (but I admit, I'm not positive - it's overall a FAR more flexibile utility - see:
http://www.ss64.com/nt/robocopy.html
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24786328
I'm positive with RoboCopy. It is preserving the file attributes by default (/COPY:DAT = Data, Attributes, Timestamps).
0
 
LVL 23

Author Comment

by:Eirman
ID: 24786412
Many thanks - The ss64 website was a revelation to me and definitely warrents points.

I'll leave the question open for a week or so to see if anyone can come up with a windows/dos solution
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24786442
You will not succeed. All available integrated commands besides backup und restore do not retain Archive bit.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 24786447
Robocopy is a utility by Microsoft in the resource kit.  It is included as part of the OS in 2008 and I believe Vista.  Is there a reason you don't want to use it?
0
 
LVL 3

Assisted Solution

by:M_Intuit
M_Intuit earned 200 total points
ID: 24787050
The completely DOS solution requested:

First thing you need to be aware of is that if the "DEL" command cannot find the destination path, then "DEL" command will act on the current directory; potentially wiping out what you never intended.  I've had it happen.  So I've modified the script entirely with this in mind.  I recommend always making use of "if EXIST" when using the "/Q" parameter on DEL.

Second thing is "*.*" while necessary for Win9x and below, can lead to missing files in XP and above.  (command dependant, system may skip files that don't have an extension)

NOTE:  You'll need to remove extra "%" symbol before testing it directly on the commandline as opposed to batch or command script file.

The basic idea:
1) remove the archive attribute from all files on C: (preserving H and S attributes)
2) Read which files have the archive attribute applied on W:
3) Apply the archive attribute to select files on C:

MD "C:\BACKUP\DATAONLY"

@if not EXIST "C:\Backup" (@echo ABORTING: Cannot find "C:\Backup" directory.&@pause&@goto :EOF)

cd /d "C:\Backup"
 

DEL /q /f /s "C:\BACKUP\DATAONLY\*"

RD /s /q "C:\BACKUP\DATAONLY\DATA"
 

%SystemRoot%\System32\XCopy.Exe "W:\DATA\*" "C:\BACKUP\DATAONLY\DATA" /I /S /E /K /R /H

REM ATTRIB -A C:\BACKUP\DATAONLY\DATA\* /s
 

REM 1) remove the archive attribute from all files on C: (preserving H and S attributes)

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aa /b /s') do attrib -a "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aah /b /s') do attrib -a +h "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aas /b /s') do attrib -a +s "%%~a"
 

REM 2) Read which files have the archive attribute applied on W:

REM 3) Apply the archive attribute to select files on C:

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aa /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a "C:\BACKUP\DATAONLY\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aah /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +h "C:\BACKUP\DATAONLY\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aas /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +s "C:\BACKUP\DATAONLY\%%~b"
 

REM !! The only thing that prevents this from working is a Microsoft BUG.  FOR command is ignoring the asterisk so "tokens=3*" is treated exactly the same as "tokens=3".

REM A work-around is possible but that will require more time.  (which I don't have at the moment)

Open in new window

0
 
LVL 3

Expert Comment

by:M_Intuit
ID: 24787279
Minor correction on the above script.  Add "\DATA" to "C:\BACKUP\DATAONLY\%%~b" so that it becomes "C:\BACKUP\DATAONLY\DATA\%%~b".  Similar goes for the three prior FOR commands as well.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 24787854
M_Intuit,

If using no delims, "tokens=1*" does not make sense. As there are no delimiters, the first token is the complete line. "tokens=1 delims=" is the same as "tokens=1* delims=" and "delims=".

I can not see any bug in the FOR interpretation. However, you should STRICTLY avoid overlapping for variables.
for /F "tokens=1* delims=" %%a ... would allow for %%a and %%b to be set. Using %%b in the next FOR might confuse the interpreter, and give all kind of strange bugs. It's a good idea to separate both FOR variable sets by case, or start the second with a non-overlapping name.
E.g.
for /f "delims=" %%A in ('dir W:\Data /a:a /b /s') do @for /f "tokens=2* delims=\" %%c in ("%%~A") do attrib +a "C:\BACKUP\DATAONLY\DATA\%%~d"

Eirman,
The tokens=3* resp. tokens=2* are hardcoded and depending on the target folder start depth (C:\BACKUP\DATAONLY == 3 directory name parts).

I would not take that path, as it is error-prone, and performs bad:
* Files already copied in a previous run are copied again
* The target and source folders are traversed 6 times each, plus the delete and copy.

RoboCopy was implemented by Microsoft for those purposes. It's fast, reliable and robust, even with open files. And it is included (in a expanded version) with Vista and 2008.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:M_Intuit
ID: 24787965
Greetings Glemo.  If you simply excise the buggy FOR statement and place it on a single line as I already did, you can confirm the bug.

Regarding "delims=1* delims=" there is no problem there.  THe purpose of the asterisk is to tell the command interpreter to place all following text under the specified variable.

So if specified variable is "%a" then "%b" means nothing when then asterisk is used.  Without the asterisk, "%b" then suddenly has meaning.

The above addresses and applies to your misperception on overlapping variables.  Reference "FOR /?" for more details.

Again, if you simply excise the buggy FOR statement and place it on a single line as I already did, you can confirm the bug.

Also understand that files aren't being "COPIED AGAIN" when the end of the FOR statements contain an "ATTRIB" command as opposed to "XCOPY" or "COPY" etc.

My post wasn't intended to counter your potentially helpful suggestion to use ROBOCOPY, only address the author's preference for a solution that doesn't involve changing the default configuration for every machine that the script is run on by installing a Resource Kit or adding the ROBOCOPY program file.
0
 
LVL 23

Author Comment

by:Eirman
ID: 24788129
Thanks for all your contributions. I'll live with minor "archive" flaw for the moment and try out robocopy when I get the time.
I'll also experiment with the interesting script above .

I only have XP disks - I presume I can download robocopy and all the other utilities from the MS website
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 24788194
If you simply excise the buggy FOR statement and place it on a single line as I already did, you can confirm the bug.
No, I can't. I tried before.

Regarding "delims=1* delims=" there is no problem there.  THe purpose of the asterisk is to tell the command interpreter to place all following text under the specified variable.
Wrong. "Tokens=1*" uses TWO variables, one for token 1, one for the remainder. Try
for /F "tokens=1* delims=:" %A in ("%time%") do @echo %A -- %B
and you will see TWO tokens.

Also understand that files aren't being "COPIED AGAIN" when the end of the FOR statements contain an "ATTRIB" command as opposed to "XCOPY" or "COPY" etc.
With "plus the delete and copy" I meant the commands at start of the batch (del, rd, xcopy). Of course does attrib not perform a copy. With "copied again" I referred to the fact that since all files are deleted in advance, all files have to be copied again; since XCopy is lacking a mirror capability this is required.

It's understood that the solution you provided was intended as an "do it with DOS only" approach. As such, it's near the "perfect" solution, but with some issues.

Robocopy is part of the free set of Resource Kit tools for Windows 2003: http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd

0
 
LVL 23

Author Closing Comment

by:Eirman
ID: 31600201
Thanks again
0
 
LVL 3

Expert Comment

by:M_Intuit
ID: 24788842
Definitely some issues; and this is one of several limitations I've run into over the years.  This would likely be better done via VBS.  

In the past there seemed to be a difference between how "tokens=3*" and "tokens=3,*" were interpreted.  Now they appear to mean precisely the same.   "tokens=3" of course has always meant the same; that only one variable is assigned meaning.

With the comma (tokens=3,*) it would simply be an automatted continuance of letter variable assigments... essentially the same as specifying 'delims=3,4,5,6,7, .... 52'.  Not the case now.

Without the comma (tokens=3*) {I THOUGHT} it meant that %a would contain everything including the delimiter in one variable.  Per "FOR /?" it is indeed %b that gets everything, not %a.    Now that I've gone back over it I vaguelyremember running into this limitation some years ago.

===================
21:06:19.60
Wed 07/01/2009
Windows Environment
E:\Tmp\Command>for /?
............
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

    would parse each line in myfile.txt, ignoring lines that begin with
    a semicolon, passing the 2nd and 3rd token from each line to the for
    body, with tokens delimited by commas and/or spaces.  Notice the for
    body statements reference %i to get the 2nd token, %j to get the
    3rd token, and %k to get all remaining tokens after the 3rd.  For
    file names that contain spaces, you need to quote the filenames with
    double quotes.  In order to use double quotes in this manner, you also
    need to use the usebackq option, otherwise the double quotes will be
    interpreted as defining a literal string to parse.

    %i is explicitly declared in the for statement and the %j and %k
    are implicitly declared via the tokens= option.  You can specify up
    to 26 tokens via the tokens= line, provided it does not cause an
    attempt to declare a variable higher than the letter 'z' or 'Z'.
    Remember, FOR variables are single-letter, case sensitive, global,
    and you can't have more than 52 total active at any one time.
..............................
========================

BEHAVIOR ON FOR SYNTAX:
 

16:03:52.13

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=3 delims=\" %a in ('dir "E:\Tmp" /b /aas /s') do @

echo attrib +a +s "C:\BACKUP\DATAONLY\%~a" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Cookies" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\History" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c
 
 

16:04:12.90

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=3* delims=\" %a in ('dir "E:\Tmp" /b /aas /s') do

@echo attrib +a +s "C:\BACKUP\DATAONLY\%~a" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Cookies"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\History"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909" FSum_MD5_27065.Chk %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" test-attrib.txt %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" TEST_ATTRIB\TEST_FILE_ATTRIB.Txt %~c

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons" Thumbs.db %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" cmldr %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" ntldr %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" NTDETECT.COM %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" BootSect.DOS %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" Boot.Ini %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" IO.SYS %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" MSDOS.SYS %~c
 
 

16:04:19.27

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=3,* delims=\" %a in ('dir "E:\Tmp" /b /aas /s') do

 @echo attrib +a +s "C:\BACKUP\DATAONLY\%~a" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Cookies"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\History"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db"  %~c

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909" FSum_MD5_27065.Chk %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" test-attrib.txt %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" TEST_ATTRIB\TEST_FILE_ATTRIB.Txt %~c

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons" Thumbs.db %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" cmldr %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" ntldr %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" NTDETECT.COM %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" BootSect.DOS %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" Boot.Ini %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" IO.SYS %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" MSDOS.SYS %~c

Open in new window

0
 
LVL 3

Expert Comment

by:M_Intuit
ID: 24788846
But anyway, here's the proof on a work-around...

Just means you'll have to double-up on the commands and expect that a bunch of errors be returned on the second command.

Simply clean it up and make the necessary reflective changes to the script.  That would be a 100% native solution you asked for.


16:15:42.42

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=3 delims=\" %a in ('dir "E:\Tmp" /b /aas /s') do @

echo attrib +a +s "C:\BACKUP\DATAONLY\%~a" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Cookies" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\History" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip" %~b %~c
 
 
 
 

16:14:32.72

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=3* delims=\" %a in ('dir "E:\Tmp" /b /aas /s') do

@echo attrib +a +s "C:\BACKUP\DATAONLY\%~a\%~b" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Cookies\" %~c

attrib +a +s "C:\BACKUP\DATAONLY\History\" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1\" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files\" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db\" %~c

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909\FSum_MD5_27065.Chk" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command\test-attrib.txt" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Command\TEST_ATTRIB\TEST_FILE_ATTRIB.Txt" %~c

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons\Thumbs.db" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\cmldr" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\ntldr" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\NTDETECT.COM" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\BootSect.DOS" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\Boot.Ini" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\IO.SYS" %~c

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Z

ip\MSDOS.SYS" %~c

Open in new window

0
 
LVL 3

Expert Comment

by:M_Intuit
ID: 24789621
Final working script (just added three lines) with proof of work following post...

                                     MD "C:\BACKUP\DATAONLY"

@if not EXIST "C:\Backup" (@echo ABORTING: Cannot find "C:\Backup" directory.&@pause&@goto :EOF)

cd /d "C:\Backup"
 

DEL /q /f /s "C:\BACKUP\DATAONLY\*"

RD /s /q "C:\BACKUP\DATAONLY\DATA"
 

%SystemRoot%\System32\XCopy.Exe "W:\DATA\*" "C:\BACKUP\DATAONLY\DATA" /I /S /E /K /R /H

REM ATTRIB -A C:\BACKUP\DATAONLY\DATA\* /s
 

REM 1) remove the archive attribute from all files on C: (preserving H and S attributes)

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aa /b /s') do attrib -a "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aah /b /s') do attrib -a +h "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY /aas /b /s') do attrib -a +s "%%~a"
 

REM 2) Read which files have the archive attribute applied on W:

REM 3) Apply the archive attribute to select files on C:

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aa /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a "C:\BACKUP\DATAONLY\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aa /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a "C:\BACKUP\DATAONLY\%%~b\%%~c"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aah /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +h "C:\BACKUP\DATAONLY\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aah /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +h "C:\BACKUP\DATAONLY\%%~b\%%~c"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aas /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +s "C:\BACKUP\DATAONLY\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aas /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +s "C:\BACKUP\DATAONLY\%%~b\%%~c""
 

REM !! The only thing that prevents this from working is a Microsoft BUG.  FOR command is ignoring the asterisk so "tokens=3*" is treated exactly the same as "tokens=3".

REM A work-around is possible but that will require more time.  (which I don't have at the moment)
 
 

=========================

PROOF OF WORK

=========================

17:59:54.31

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=1* delims=" %a in ('dir E:\Tmp /as /b /s') do @for

 /f "tokens=3* delims=\" %b in ("%~a") do @echo attrib +a "C:\BACKUP\DATAONLY\%~

b"

attrib +a "C:\BACKUP\DATAONLY\Cookies"

attrib +a "C:\BACKUP\DATAONLY\History"

attrib +a "C:\BACKUP\DATAONLY\Tempor~1"

attrib +a "C:\BACKUP\DATAONLY\Temporary Internet Files"

attrib +a "C:\BACKUP\DATAONLY\Thumbs.db"

attrib +a "C:\BACKUP\DATAONLY\07y02m09d_001909"

attrib +a "C:\BACKUP\DATAONLY\Command"

attrib +a "C:\BACKUP\DATAONLY\Command"

attrib +a "C:\BACKUP\DATAONLY\DefaultEmoticons"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"
 
 
 

18:00:10.46

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=1* delims=" %a in ('dir E:\Tmp /as /b /s') do @for

 /f "tokens=3* delims=\" %b in ("%~a") do @echo attrib +a "C:\BACKUP\DATAONLY\%~

b\%~c"

attrib +a "C:\BACKUP\DATAONLY\Cookies\"

attrib +a "C:\BACKUP\DATAONLY\History\"

attrib +a "C:\BACKUP\DATAONLY\Tempor~1\"

attrib +a "C:\BACKUP\DATAONLY\Temporary Internet Files\"

attrib +a "C:\BACKUP\DATAONLY\Thumbs.db\"

attrib +a "C:\BACKUP\DATAONLY\07y02m09d_001909\FSum_MD5_27065.Chk"

attrib +a "C:\BACKUP\DATAONLY\Command\test-attrib.txt"

attrib +a "C:\BACKUP\DATAONLY\Command\TEST_ATTRIB\TEST_FILE_ATTRIB.Txt"

attrib +a "C:\BACKUP\DATAONLY\DefaultEmoticons\Thumbs.db"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

cmldr"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

ntldr"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

NTDETECT.COM"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

BootSect.DOS"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

Boot.Ini"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

IO.SYS"

attrib +a "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

MSDOS.SYS"
 
 

18:01:23.95

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>

Open in new window

0
 
LVL 3

Expert Comment

by:M_Intuit
ID: 24789680
Removed last remark; add +s on proof; add \Data to for statements.
                                     MD "C:\BACKUP\DATAONLY"

@if not EXIST "C:\Backup" (@echo ABORTING: Cannot find "C:\Backup" directory.&@pause&@goto :EOF)

cd /d "C:\Backup"
 

DEL /q /f /s "C:\BACKUP\DATAONLY\*"

RD /s /q "C:\BACKUP\DATAONLY\DATA"
 

%SystemRoot%\System32\XCopy.Exe "W:\DATA\*" "C:\BACKUP\DATAONLY\DATA" /I /S /E /K /R /H

REM ATTRIB -A C:\BACKUP\DATAONLY\DATA\* /s
 

REM 1) remove the archive attribute from all files on C: (preserving H and S attributes)

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY\DATA /aa /b /s') do attrib -a "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY\DATA /aah /b /s') do attrib -a +h "%%~a"

for /f "tokens=1* delims=" %%a in ('dir C:\BACKUP\DATAONLY\DATA /aas /b /s') do attrib -a +s "%%~a"
 

REM 2) Read which files have the archive attribute applied on W:

REM 3) Apply the archive attribute to select files on C:

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aa /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a "C:\BACKUP\DATAONLY\DATA\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aa /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a "C:\BACKUP\DATAONLY\DATA\%%~b\%%~c"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aah /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +h "C:\BACKUP\DATAONLY\DATA\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aah /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +h "C:\BACKUP\DATAONLY\DATA\%%~b\%%~c"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aas /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +s "C:\BACKUP\DATAONLY\DATA\%%~b"

for /f "tokens=1* delims=" %%a in ('dir W:\Data /aas /b /s') do @for /f "tokens=3* delims=\" %%b in ("%%~a") do attrib +a +s "C:\BACKUP\DATAONLY\DATA\%%~b\%%~c""
 
 
 
 
 
 

=========================

PROOF OF WORK

=========================

17:59:54.31

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=1* delims=" %a in ('dir E:\Tmp /as /b /s') do @for

 /f "tokens=3* delims=\" %b in ("%~a") do @echo attrib +a +s "C:\BACKUP\DATAONLY\%~

b"

attrib +a +s "C:\BACKUP\DATAONLY\Cookies"

attrib +a +s "C:\BACKUP\DATAONLY\History"

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files"

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db"

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909"

attrib +a +s "C:\BACKUP\DATAONLY\Command"

attrib +a +s "C:\BACKUP\DATAONLY\Command"

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip"

 

 

 

18:00:10.46

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>for /f "tokens=1* delims=" %a in ('dir E:\Tmp /as /b /s') do @for

 /f "tokens=3* delims=\" %b in ("%~a") do @echo attrib +a +s "C:\BACKUP\DATAONLY\%~

b\%~c"

attrib +a +s "C:\BACKUP\DATAONLY\Cookies\"

attrib +a +s "C:\BACKUP\DATAONLY\History\"

attrib +a +s "C:\BACKUP\DATAONLY\Tempor~1\"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Internet Files\"

attrib +a +s "C:\BACKUP\DATAONLY\Thumbs.db\"

attrib +a +s "C:\BACKUP\DATAONLY\07y02m09d_001909\FSum_MD5_27065.Chk"

attrib +a +s "C:\BACKUP\DATAONLY\Command\test-attrib.txt"

attrib +a +s "C:\BACKUP\DATAONLY\Command\TEST_ATTRIB\TEST_FILE_ATTRIB.Txt"

attrib +a +s "C:\BACKUP\DATAONLY\DefaultEmoticons\Thumbs.db"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

cmldr"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

ntldr"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

NTDETECT.COM"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

BootSect.DOS"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

Boot.Ini"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

IO.SYS"

attrib +a +s "C:\BACKUP\DATAONLY\Temporary Directory 1 for RootFiles_08y01m17d.Zip\

MSDOS.SYS"

 

 

18:01:23.95

Mon 07/06/2009

Windows Environment

E:\Tmp\Command>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now